Ocr.ahk

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Felix Siano
Posts: 90
Joined: 23 Apr 2023, 13:03

Ocr.ahk

21 Sep 2023, 11:19

Is it possible to modify the code to be activated when pressing F1 and when clicking with the left mouse it copies it to the clipboard?

Code: Select all

#include OCR.ahk

CoordMode "Mouse", "Screen"
CoordMode "ToolTip", "Screen"

Loop {
    MouseGetPos(&X, &Y)
    Highlight(x-75, y-25, 150, 50)
    ToolTip(OCR.FromRect(X-75, Y-25, 150, 50,,2).Text, , Y+40)
	
	
}

Highlight(x?, y?, w?, h?, showTime:=0, color:="Red", d:=2) {
	static guis := []

	if !IsSet(x) {
        for _, r in guis
            r.Destroy()
        guis := []
		return
    }
    if !guis.Length {
        Loop 4
            guis.Push(Gui("+AlwaysOnTop -Caption +ToolWindow -DPIScale +E0x08000000"))
    }
	Loop 4 {
		i:=A_Index
		, x1:=(i=2 ? x+w : x-d)
		, y1:=(i=3 ? y+h : y-d)
		, w1:=(i=1 or i=3 ? w+2*d : d)
		, h1:=(i=2 or i=4 ? h+2*d : d)
		guis[i].BackColor := color
		guis[i].Show("NA x" . x1 . " y" . y1 . " w" . w1 . " h" . h1)
	}
	if showTime > 0 {
		Sleep(showTime)
		Highlight()
	} else if showTime < 0
		SetTimer(Highlight, -Abs(showTime))
}

Descolada
Posts: 1202
Joined: 23 Dec 2021, 02:30

Re: Ocr.ahk

21 Sep 2023, 23:41

Code: Select all

#include OCR.ahk

CoordMode "Mouse", "Screen"
CoordMode "ToolTip", "Screen"

Loop {
    MouseGetPos(&X, &Y)
    Highlight(x-75, y-25, 150, 50)
    ToolTip(currentText := OCR.FromRect(X-75, Y-25, 150, 50,,2).Text, , Y+40)
}

F1 & LButton::A_Clipboard := currentText

Highlight(x?, y?, w?, h?, showTime:=0, color:="Red", d:=2) {
	static guis := []

	if !IsSet(x) {
        for _, r in guis
            r.Destroy()
        guis := []
		return
    }
    if !guis.Length {
        Loop 4
            guis.Push(Gui("+AlwaysOnTop -Caption +ToolWindow -DPIScale +E0x08000000"))
    }
	Loop 4 {
		i:=A_Index
		, x1:=(i=2 ? x+w : x-d)
		, y1:=(i=3 ? y+h : y-d)
		, w1:=(i=1 or i=3 ? w+2*d : d)
		, h1:=(i=2 or i=4 ? h+2*d : d)
		guis[i].BackColor := color
		guis[i].Show("NA x" . x1 . " y" . y1 . " w" . w1 . " h" . h1)
	}
	if showTime > 0 {
		Sleep(showTime)
		Highlight()
	} else if showTime < 0
		SetTimer(Highlight, -Abs(showTime))
}
Felix Siano
Posts: 90
Joined: 23 Apr 2023, 13:03

Re: Ocr.ahk

22 Sep 2023, 13:48

perfect! Thanks
Felix Siano
Posts: 90
Joined: 23 Apr 2023, 13:03

Re: Ocr.ahk

22 Sep 2023, 19:21

I'm trying to add to increase or decrease the size of the red highlight.
I'm putting it inside the loop. Am I doing something wrong
Last question! You helped me a lot!

Code: Select all


#include OCR.ahk
#Requires AutoHotkey v2.0
CoordMode "Mouse", "Screen"
CoordMode "ToolTip", "Screen"
Width := 70
Height := 70
Loop {
MouseGetPos(&X, &Y)
Highlight(X - Width / 2, Y - Height / 2, Width, Height)
ToolTip(currentText := OCR.FromRect(X - Width / 2, Y - Height / 2, Width, Height, , 2).Text, , Y + 40)
}
~LButton::
{
A_Clipboard := currentText
ExitApp
}


Up::
{
width := Width + 3
Height := Height +3 
Return
}


Down::
{
width := Width - 3
Height := Height - 3 
Return
}


Highlight(x?, y?, w?, h?, showTime := 0, color := "Blue", d := 2) {
static guis := []
if !IsSet(x) {
for _, r in guis
r.Destroy()
guis := []
return
}
if !guis.Length {
Loop 4
guis.Push(Gui("+AlwaysOnTop -Caption +ToolWindow -DPIScale +E0x08000000"))
}
Loop 4 {
i := A_Index
x1 := (i = 2 ? x + w : x - d)
y1 := (i = 3 ? y + h : y - d)
w1 := (i = 1 or i = 3 ? w + 2 * d : d)
h1 := (i = 2 or i = 4 ? h + 2 * d : d)
guis[i].BackColor := color
guis[i].Show("NA x" . x1 . " y" . y1 . " w" . w1 . " h" . h1)
}
if showTime > 0 {
Sleep(showTime)
Highlight()
} else if showTime < 0
SetTimer(Highlight, -Abs(showTime))
}



Descolada
Posts: 1202
Joined: 23 Dec 2021, 02:30

Re: Ocr.ahk

23 Sep 2023, 00:28

Global-scope variables (defined inside the auto-execute section) can be by default from a local scope (hotkey) only read. To change the variable you need to define it as global:

Code: Select all

#include OCR.ahk
#Requires AutoHotkey v2.0
CoordMode "Mouse", "Screen"
CoordMode "ToolTip", "Screen"
Width := 70, Height := 70
Loop {
	MouseGetPos(&X, &Y)
	Highlight(X - Width // 2, Y - Height // 2, Width, Height)
	ToolTip(currentText := OCR.FromRect(X - Width // 2, Y - Height // 2, Width, Height, , 2).Text, , Y + 40)
}
~LButton::
{
	A_Clipboard := currentText
	ExitApp
}

Up::global height += 3
Down::global height -= 3
Left::global width += 3
Right::global width -= 3

Highlight(x?, y?, w?, h?, showTime := 0, color := "Blue", d := 2) {
	static guis := []
	if !IsSet(x) {
		for _, r in guis
			r.Destroy()
		guis := []
		return
	}
	if !guis.Length {
		Loop 4
			guis.Push(Gui("+AlwaysOnTop -Caption +ToolWindow -DPIScale +E0x08000000"))
	}
	Loop 4 {
		i := A_Index
		x1 := (i = 2 ? x + w : x - d)
		y1 := (i = 3 ? y + h : y - d)
		w1 := (i = 1 or i = 3 ? w + 2 * d : d)
		h1 := (i = 2 or i = 4 ? h + 2 * d : d)
		guis[i].BackColor := color
		guis[i].Show("NA x" . x1 . " y" . y1 . " w" . w1 . " h" . h1)
	}
	if showTime > 0 {
		Sleep(showTime)
		Highlight()
	} else if showTime < 0
		SetTimer(Highlight, -Abs(showTime))
}
Felix Siano
Posts: 90
Joined: 23 Apr 2023, 13:03

Re: Ocr.ahk

23 Sep 2023, 00:37

PERFECT! THANKS! BREAKING MY HEAD HERE
Felix Siano
Posts: 90
Joined: 23 Apr 2023, 13:03

Re: Ocr.ahk

24 Sep 2023, 12:21

one last doubt. Is it possible to copy with the original line break?
Descolada
Posts: 1202
Joined: 23 Dec 2021, 02:30

Re: Ocr.ahk

25 Sep 2023, 06:19

Yes it is possible. OCR.FromRect(...).Text outputs the text without linebreaks, but you can loop over OCR.FromRect(...).Lines, get Line.Text and concatenate them with a linebreak.
emp00
Posts: 165
Joined: 15 Apr 2023, 12:02

Re: Ocr.ahk

24 May 2024, 10:53

Descolada wrote:
25 Sep 2023, 06:19
Yes it is possible. OCR.FromRect(...).Text outputs the text without linebreaks, but you can loop over OCR.FromRect(...).Lines, get Line.Text and concatenate them with a linebreak.

I am trying to achieve exactly this (getting the OCR'ed text with line breaks from an image) --> can you show me how this "loop" must look like? I just need a simple string variable including the linebreaks...

Code: Select all

OCRText_Lines := OCR.FromBitmap(hBitmap_Clipboard, ).Lines ; This contains the OCR'ed lines
OCRText_with_Linebreaks := xxxxxx ???
Descolada
Posts: 1202
Joined: 23 Dec 2021, 02:30

Re: Ocr.ahk

24 May 2024, 13:08

@emp00

Code: Select all

OCRText_Lines := OCR.FromBitmap(hBitmap_Clipboard, ).Lines
OCRText_with_Linebreaks := ""
for line in OCRText_Lines
    OCRText_with_Linebreaks .= line.Text "`n"
OCRText_with_Linebreaks := Trim(OCRText_with_Linebreaks, "`n")
MsgBox OCRText_with_Linebreaks

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 61 guests