OCR

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

OCR

Post by Felix Siano » 01 Dec 2023, 10:11

Is it possible to leave this @Descolada code running in the background, put an Alt+b shortcut to only activate OCR and when copying to the clipboard for it, but it continues running in the background to use Alt+b again?

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)
}
Lbutton::
{
A_Clipboard := RegExReplace(currentText, "[\s\-\.,/_]", "")
}

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))
}

User avatar
mikeyww
Posts: 27686
Joined: 09 Sep 2014, 18:38

Re: OCR

Post by mikeyww » 01 Dec 2023, 13:28

Yes. You could change the loop so that the OCR happens only when the hotkey is triggered (instead of continuous looping).

Your script is persistent and runs "in the background" by default.

Felix Siano
Posts: 94
Joined: 23 Apr 2023, 13:03

Re: OCR

Post by Felix Siano » 01 Dec 2023, 13:51

I tried several ways, but without success

User avatar
mikeyww
Posts: 27686
Joined: 09 Sep 2014, 18:38

Re: OCR

Post by mikeyww » 01 Dec 2023, 14:17

Code: Select all

#Requires AutoHotkey v2.0
on := False

!b:: {
 Global on
 If on := !on ; Toggle the flag
  go
 Else SetTimer(doSomeOCR, 0), ToolTip()
}

LButton:: {
 If !on
  go
}

go() {
 Global on := True
 SetTimer(doSomeOCR, 25), doSomeOCR()
}

doSomeOCR() {
 If on {
  ToolTip "Holy cow, I'm doing some OCR now."
  ; Do OCR here
 }
}

Felix Siano
Posts: 94
Joined: 23 Apr 2023, 13:03

Re: OCR

Post by Felix Siano » 02 Dec 2023, 18:35

Thanks

mandus
Posts: 14
Joined: 27 Oct 2016, 01:33
Contact:

Re: OCR

Post by mandus » 03 Dec 2023, 04:13

can you try this one

Code: Select all

#include OCR.ahk

CoordMode, Mouse, Screen
CoordMode, ToolTip, Screen

ocrActive := false

!b::  ; Alt+B to toggle OCR functionality
    ocrActive := !ocrActive
    if (ocrActive)
        SetTimer, PerformOCR, 100  ; Run PerformOCR every 100 ms when active
    else
        SetTimer, PerformOCR, Off  ; Turn off OCR when not active
return

PerformOCR:
    MouseGetPos, X, Y
    Highlight(X-75, Y-25, 150, 50)
    ToolTip % OCR.FromRect(X-75, Y-25, 150, 50,,2).Text,, Y+40
return

LButton::  ; Left click handler
    if (ocrActive)
    {
        currentText := OCR.FromRect(X-75, Y-25, 150, 50,,2).Text
        A_Clipboard := RegExReplace(currentText, "[\s\-\.,/_]", "")
    }
return

Highlight(x, y, w, h, showTime:=0, color:="Red", d:=2) {
    ; [Existing Highlight Function Code]
}

ExitApp  ; Exit the script when needed
i need your feedback please.

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

Re: OCR

Post by Descolada » 03 Dec 2023, 04:23

@mandus, ChatGPT produces v1 code and doesn't understand AHK code flow very well, meaning your code is 100% broken.

Felix Siano
Posts: 94
Joined: 23 Apr 2023, 13:03

Re: OCR

Post by Felix Siano » 03 Dec 2023, 09:39

Does not work. I even gave up, I'm going to use it as is

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

Re: OCR

Post by Descolada » 03 Dec 2023, 10:32

@Felix Siano, I'm not sure I understood what you wish to do exactly, but this OCR's only when you press Alt+b

Code: Select all

#Requires AutoHotkey v2
#include OCR.ahk

CoordMode "Mouse", "Screen"

!b::
{
    MouseGetPos(&X, &Y)
    result := OCR.FromRect(X-75, Y-25, 150, 50,,2) ; OCR area near mouse
    Highlight(x-75, y-25, 150, 50, -500) ; Highlight for 500ms without pausing
    A_Clipboard := result.Text ; Assign to Clipboard
    ToolTip('Copied "' result.Text '" into clipboard')
    SetTimer(ToolTip, -2000)
}

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))
}
Last edited by Descolada on 03 Dec 2023, 12:05, edited 1 time in total.

Felix Siano
Posts: 94
Joined: 23 Apr 2023, 13:03

Re: OCR

Post by Felix Siano » 03 Dec 2023, 11:39

This is my idea:
just show the rectangle on the screen when pressing "!b" and pressing "Lbutton" for the loop without the rectangle remaining on the screen. But the script is running waiting for user interaction
I KNOW THIS WAY IS WRONG

Code: Select all

#Requires AutoHotkey v2.0
#include OCR.ahk
CoordMode "Mouse", "Screen"
CoordMode "ToolTip", "Screen"
Width := 50, Height := 25


!b::
{
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 - Height-25)


Lbutton::
{
A_Clipboard := RegExReplace(currentText, "[\s\-\.,/_]", "")
break 
}
~Ctrl & Lbutton::
{
A_Clipboard := currentText
break 
}



}
}


Shift & WheelUp::global height += 6
Shift & WheelDown::global height -= 6
WheelDown::global width -= 6
WheelUp::global width += 6
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: 1273
Joined: 23 Dec 2021, 02:30

Re: OCR

Post by Descolada » 03 Dec 2023, 12:05

@Felix Siano, then you need to create a toggle to switch the loop on or off, just as mikeyww suggested. Example:

Code: Select all

#Requires AutoHotkey v2.0
#include OCR.ahk
CoordMode "Mouse", "Screen"
CoordMode "ToolTip", "Screen"
Width := 50, Height := 25, g_Toggle := 0, g_CurrentText := ""

!b::
{
	global g_Toggle := !g_Toggle, g_CurrentText
	While g_Toggle {
		MouseGetPos(&X, &Y)
		Highlight(X - Width // 2, Y - Height // 2, Width, Height)
		ToolTip(g_CurrentText := OCR.FromRect(X - Width // 2, Y - Height // 2, Width, Height, , 2).text, , Y - Height-25)
	}
	Highlight(), ToolTip()
}

#HotIf g_Toggle
Lbutton::
{
	global g_Toggle := 0, g_CurrentText
	A_Clipboard := RegExReplace(g_CurrentText, "[\s\-\.,/_]", "")
}
~Ctrl & Lbutton::
{
	global g_Toggle := 0, g_CurrentText
	A_Clipboard := g_CurrentText
}

Shift & WheelUp::global height += 6
Shift & WheelDown::global height -= 6
WheelDown::global width -= 6
WheelUp::global width += 6
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: 94
Joined: 23 Apr 2023, 13:03

Re: OCR

Post by Felix Siano » 03 Dec 2023, 12:55

That's what I wanted. It is perfect. I would never be able to do that

mandus
Posts: 14
Joined: 27 Oct 2016, 01:33
Contact:

Re: OCR

Post by mandus » 03 Dec 2023, 13:52

Descolada wrote:
03 Dec 2023, 04:23
@mandus, ChatGPT produces v1 code and doesn't understand AHK code flow very well, meaning your code is 100% broken.
trying to build custom GPT, but yeah i will have to train an open-source model.

User avatar
boiler
Posts: 17710
Joined: 21 Dec 2014, 02:44

Re: OCR

Post by boiler » 03 Dec 2023, 22:57

Please note that posting AI-generated code in answering or asking for help is against the forum rules.

Post Reply

Return to “Ask for Help (v2)”