Image recognition scripts work separately but not together Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Aleque
Posts: 17
Joined: 24 Apr 2021, 16:55

Image recognition scripts work separately but not together

12 May 2024, 21:04

Hi.

I did some extremely long back and forth with ChatGPT, trying to make this image recognition script working. Both of the scripts work fine on their own but when combined into one file, Script 2 doesn't seem to make the mouse cursor go to where the image is on the screen. Instead it goes to the same X and Y location of the Script 1's image location on the screen. I am not sure why it is, as I am still new at this thing and my brain can't function anymore trying to explain this to a robot.

I think it said something about some variables being the same in both scripts, hence making the conflict and not acquiring the right coordinates for Script 2, but ever since I tried to making it fix the scripts by combining them into one, it did nothing but create new errors. So I ran out of sanity and I hope that some human experts can actually help. Please someone help me out. Thanks again.

Code: Select all

; Script 1:

CoordMode, Pixel, Screen ; Set coordinate mode to screen pixels

; Define the path to the image you want to search for
ImagePath := "C:\COLOR.png"

; Define the offset for adjusting the mouse cursor position
OffsetX := -35
OffsetY := 15

; Define a hotkey (in this case, Control+I) to trigger the image recognition script
+Æ::
{
    ; Check if the window with class "ahk_class OWL.Dock" exists
    If !WinExist("ahk_class OWL.Dock")
    {
        
        ; Add a 100ms delay
        Sleep, 100
        
        ; Press F4 key
        SendInput, {F4}
        
        ; Add another 100ms delay
        Sleep, 100
    }
    
    
    ; Search for the image on the screen
    ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %ImagePath%
    
    ; If an error occurs during image search
    If ErrorLevel
    {
        MsgBox, Image search failed with ErrorLevel: %ErrorLevel%
    }
    else
    {
        ; Calculate the center coordinates of the found image with offset
        ImageWidth := ErrorLevel ; Width of the found image
        ImageHeight := ErrorLevel ; Height of the found image
        ImageCenterX := FoundX + (ImageWidth / 2) + OffsetX
        ImageCenterY := FoundY + (ImageHeight / 2) + OffsetY
        
        ; Move the mouse cursor to the adjusted center of the found image
        MouseMove, ImageCenterX, ImageCenterY
        
        ; Add a short delay before clicking
        Sleep, 100 ; Delay for 100 milliseconds
        
        ; Left click on the image
        Click
        
        ; Add another 100ms delay
        Sleep, 100
        
        ; Get the current mouse position (position1)
        MouseGetPos, posX1, posY1
        
        ; Add another 100ms delay
        Sleep, 100
        
        ; Calculate the new position (position2)
        posX2 := posX1 + 490
        posY2 := posY1 + 100
        
        ; Move the mouse cursor to the new position (position2)
        MouseMove, posX2, posY2
    
        ; Left click
        Click
    }
    
    return
}

Code: Select all

; Script 2

CoordMode, Pixel, Screen  ; Set coordinate mode to screen pixels

; Define the path to the image you want to search for
ImagePath := "C:\ROTATE.png"

; Define the offset for adjusting the mouse cursor position
OffsetX := 25
OffsetY := 25

; Define a hotkey (in this case, Control+I) to trigger the image recognition script
F10::
{
    ; Search for the image on the screen
    ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %ImagePath%
    
    ; If an error occurs during image search
    If ErrorLevel
    {
        MsgBox, Image search failed with ErrorLevel: %ErrorLevel%
    }
    else
    {
        ; Calculate the center coordinates of the found image with offset
        ImageWidth := ErrorLevel  ; Width of the found image
        ImageHeight := ErrorLevel  ; Height of the found image
        ImageCenterX := FoundX + (ImageWidth / 2) + OffsetX
        ImageCenterY := FoundY + (ImageHeight / 2) + OffsetY
        
        ; Move the mouse cursor to the adjusted center of the found image
        MouseMove, ImageCenterX, ImageCenterY
        
        ; Add a short delay before clicking
        Sleep, 15  ; Delay for 15 milliseconds
        
        ; Left click on the image
        Click, 2
        
		; Debug
        ; MsgBox, Image clicked at X:%ImageCenterX% Y:%ImageCenterY%
    }
}
return
User avatar
CoffeeChaton
Posts: 32
Joined: 11 May 2024, 10:50

Re: Image recognition scripts work separately but not together

12 May 2024, 22:41

Code: Select all


; #1
^i::Script1() ; Define a hotkey (in this case, Control+I) to trigger the image recognition script
F10:: Script2() ; #4 Define a hotkey (in this case, Control+I) to trigger the image recognition script

Script1() {
    CoordMode, Pixel, Screen
    ImagePath := "C:\COLOR.png"
    OffsetX := -35
    OffsetY := 15
    If !WinExist("ahk_class OWL.Dock") {
        Sleep, 100
        SendInput, {F4}
        Sleep, 100
    }

    ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %ImagePath%
    ; #2 
    If (ErrorLevel = 1) {
        MsgBox, % "not find img at`n`n" ImagePath
        Return
    } 
    If (ErrorLevel = 2) {
        MsgBox, % "such as failure to open the image file or a badly formatted option of`n`n" ImagePath
        Return
    } 

    ; #3
    gui, add,picture,hwndmypic,% ImagePath ; https://www.autohotkey.com/board/topic/59258-get-imagesize-how-solved/
    controlgetpos, ,,ImageWidth,ImageHeight,,ahk_id %mypic%
    ; ImageWidth := ErrorLevel ; Width of the found image
    ; ImageHeight := ErrorLevel ; Height of the found image
    ImageCenterX := FoundX + (ImageWidth / 2) + OffsetX
    ImageCenterY := FoundY + (ImageHeight / 2) + OffsetY
    MouseMove, ImageCenterX, ImageCenterY
    Sleep, 100
    Click
    Sleep, 100
    MouseGetPos, posX1, posY1
    Sleep, 100
    posX2 := posX1 + 490
    posY2 := posY1 + 100
    MouseMove, posX2, posY2
    Click
}

Script2() {
    CoordMode, Pixel, Screen
    ImagePath := "C:\ROTATE.png"
    OffsetX := 25
    OffsetY := 25
    ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %ImagePath%

    If (ErrorLevel = 1) {
        MsgBox, % "not find img at`n`n" ImagePath
        Return
    } 
    If (ErrorLevel = 2) {
        MsgBox, % "such as failure to open the image file or a badly formatted option of`n`n" ImagePath
        Return
    } 

    ; #3
    gui, add,picture,hwndmypic,% ImagePath ; https://www.autohotkey.com/board/topic/59258-get-imagesize-how-solved/
    controlgetpos, ,,ImageWidth,ImageHeight,,ahk_id %mypic%
    ; ImageWidth := ErrorLevel ; Width of the found image
    ; ImageHeight := ErrorLevel ; Height of the found image
    ImageCenterX := FoundX + (ImageWidth / 2) + OffsetX
    ImageCenterY := FoundY + (ImageHeight / 2) + OffsetY

    MouseMove, ImageCenterX, ImageCenterY
    Sleep, 15
    Click, 2
}
#1 https://www.autohotkey.com/docs/v1/Hotkeys.htm
; Define a hotkey (in this case, Control+I) to trigger the image recognition script
+Æ::
what is Æ ?
if you need Ctrl+I, just ^i or ~^i

#2 https://www.autohotkey.com/docs/v1/lib/ImageSearch.htm#Error_Handling
ErrorLevel is set to 0 if the image was found in the specified region, 1 if it was not found, or 2 if there was a problem that prevented the command from conducting the search (such as failure to open the image file or a badly formatted option).
There doesn't seem to be a big problem here, I just made the error message more clear

#3 https://www.autohotkey.com/board/topic/59258-get-imagesize-how-solved/
ImageSearch ErrorLevel does not contain the width and height information of the image,I freely use the methods on the forum, but I am more accustomed to the GPI+ method. Please try it yourself to see if there are any problems.

#4
Your comment says that both of your scripts use ctrl+i keystrokes to call, but the code written in script2 uses `F10`.

#5 https://www.autohotkey.com/docs/v1/Language.htm#auto-execute-section
After the script has been loaded, it begins executing at the top line, continuing until a Return, Exit, the script's first hotkey/hotstring label, or the physical end of the script is encountered (whichever comes first).
I wrap the automatic execution part into a function and then call it to avoid automatic execution.
Aleque
Posts: 17
Joined: 24 Apr 2021, 16:55

Re: Image recognition scripts work separately but not together

13 May 2024, 07:22

Hi and thank you for your reply.

I can't seem to reply by quoting on each individual thing you say for some reason so I will use arrows like this: <--

what is Æ ? <-- It's a keyboard letter on a Scandinavian keyboard to the right of letter L. I would still like to use that letter/key (in combination with Shift) but it gives me an error, which goes like this without " ":

"Error at line <number>.

Line Text: +Æ::Script1()

Error: invalid hotkey.

The program will exit."


Small "æ" shows like this in the error: +æ
Capital "Æ" shows like this in the error: +Æ


I just made the error message more clear <-- I appreciate it.


I am more accustomed to the GPI+ method <-- Sorry I'm not familiar with GPI+ or its advantages. I just tried Googling it and it seems as though it is for version 2 of AHK? Or maybe I am misunderstanding something?


Your comment says that both of your scripts use ctrl+i keystrokes to call, but the code written in script2 uses `F10`. <-- Yeah, sorry, my bad. I was so tired yesterday, that I forgot to double check the descriptions and correct them.


I wrap the automatic execution part into a function and then call it to avoid automatic execution. <-- Again, I really appreciate it but I'm not sure what that means in regard to this script.


This is why it turned to ChatGPT yesterday - coding is not my strong suit. I did my best to describe what I wanted it to do, and sometimes it did a great job. But every time I wanted it to fix a little thing or improve upon it, it did something to the code that rendered it useless. So that things that worked previously stopped working and it just felt hopeless to continuing this discussion with a bot. Like, I asked it to fix my "Æ" issue, and it rewrote the code in a way that just didn't work anymore.

I tested the code you provided and it seems to be working, so I'm really grateful for that. If only there was a way for me to fix the Æ-issue. Unless perhaps I could make AHK think that Æ key is another key and then bind the script that thát. Or maybe some other creative solution.

Edit: It looks like I found a fix here: viewtopic.php?t=79309
"To save as UTF-8 in Notepad, select UTF-8 or UTF-8 with BOM from the Encoding drop-down in the Save As dialog"

Now it's going to be exciting to see if everything will work as planned


EDIT2: It works so far, but I would like to add a bit of code, but I am not sure how.

If !WinExist("ahk_class OWL.Dock") {
Sleep, 100
SendInput, {F4}
Sleep, 100
}

Similar to the code above, I would like to add another condition. If the window "ahk_class OWL.Dock" DOES exist, but the image provided here:

ImagePath := "C:\COLOR.png"

...is NOWHERE to be found on the screen, then press P and then run the script as usual by moving the mouse cursor, clicking, etc. Could you please at least point me in the right direction, how I could set up my code to make this work?

My guess it would be something like this (just writing pseudo code, because that is all I know):

If WinExist("ahk_class OWL.Dock") AND there is no image on screen that matches ImagePath := "C:\COLOR.png", then press P and continue Script1, else run Script1 (as usual)

I tried asking ChatGPT again but it always gives some weird "solutions" that cause more problems. I don't know, maybe I am not that good at explaining the problem to it.
User avatar
CoffeeChaton
Posts: 32
Joined: 11 May 2024, 10:50

Re: Image recognition scripts work separately but not together  Topic is solved

13 May 2024, 09:54

#1 about Æ

try it to get your key ,Press [F5] to refresh.

Code: Select all

#Requires AutoHotkey v1.1.33.11
KeyHistory
MsgBox, % "Please do not close this MSGBOX before obtaining the key information you want, otherwise the script will be closed"
#2 about GPI+

https://github.com/marius-sucan/AHK-GDIp-Library-Compilation
GPIP has ahk-v1.1 and ahk-v2
For me, GDIP has always worked well with matching images from within the game, while the commands provided natively by AHK failed to match images from some in-game... Anyway I was hoping to express that there is more to this ideas.

#3 auto-execute-section
also can read https://www.autohotkey.com/docs/v1/Scripts.htm#auto

#4 UTF-8

Because yours was published in Ask for Help (v1), I infer that you are using the v1.1 version, so it is best to paste the following line at the top of the code I gave you last time to avoid ahk using the v2 version.
v1 version, please select utf-8 with Bom for encoding.

Code: Select all

#Requires AutoHotkey v1.1.33.11

#5 If WinExist("ahk_class OWL.Dock") AND there is no image on screen that matches ImagePath := "C:\COLOR.png", then press P and continue Script1, else run Script1 (as usual)

Code: Select all

#Requires AutoHotkey v1.1.33.11

^i:: Script1(10) ; ##1
F10:: Script2()

/**
* @param {number} max_calls_number need > 0 number
*/
Script1(max_calls_number) {
    CoordMode, Pixel, Screen
    ImagePath := "C:\COLOR.png"
    OffsetX := -35
    OffsetY := 15
    If !WinExist("ahk_class OWL.Dock") {
        Sleep, 100
        SendInput, {F4}
        Sleep, 100
    }

    ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %ImagePath%
    If (ErrorLevel = 1) {
        calls_number := max_calls_number -1
        if (calls_number > 0) {
            ; ##2
            SendInput, P
            Sleep, 100 ; Need to wait for the program to refresh the screen
            Script1(calls_number) ;##3
        } else {
            MsgBox, % "can't find img `n" ImagePath
        }
        Return
    }

    If (ErrorLevel = 2) {
        MsgBox, % "such as failure to open the image file or a badly formatted option of`n`n" ImagePath
        Return
    }

    gui, add,picture,hwndmypic,% ImagePath ; https://www.autohotkey.com/board/topic/59258-get-imagesize-how-solved/
    controlgetpos, ,,ImageWidth,ImageHeight,,ahk_id %mypic%

    ImageCenterX := FoundX + (ImageWidth / 2) + OffsetX
    ImageCenterY := FoundY + (ImageHeight / 2) + OffsetY
    MouseMove, ImageCenterX, ImageCenterY
    Sleep, 100
    Click
    Sleep, 100
    MouseGetPos, posX1, posY1
    Sleep, 100
    posX2 := posX1 + 490
    posY2 := posY1 + 100
    MouseMove, posX2, posY2
    Click
}

Script2() {
    CoordMode, Pixel, Screen
    ImagePath := "C:\ROTATE.png"
    OffsetX := 25
    OffsetY := 25
    ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, %ImagePath%

    If (ErrorLevel = 1) {
        MsgBox, % "not find img at`n`n" ImagePath
        Return
    }
    If (ErrorLevel = 2) {
        MsgBox, % "such as failure to open the image file or a badly formatted option of`n`n" ImagePath
        Return
    }

    gui, add,picture,hwndmypic,% ImagePath ; https://www.autohotkey.com/board/topic/59258-get-imagesize-how-solved/
    controlgetpos, ,,ImageWidth,ImageHeight,,ahk_id %mypic%

    ImageCenterX := FoundX + (ImageWidth / 2) + OffsetX
    ImageCenterY := FoundY + (ImageHeight / 2) + OffsetY

    MouseMove, ImageCenterX, ImageCenterY
    Sleep, 15
    Click, 2
}
##1 Try up to 10 times
##2 then press P and continue Script1, else run Script1
##3 if not max_calls_number and calls_number lint, It will run indefinitely until Stack Overflow

...the function call self, often called a recursive function.
User avatar
V0RT3X
Posts: 243
Joined: 20 May 2023, 21:59
Contact:

Re: Image recognition scripts work separately but not together

13 May 2024, 15:39

Aleque wrote:
12 May 2024, 21:04
I did some extremely long back and forth with ChatGPT
Just a FYI: ChatGPT output is currently against the forum rules.
(See #4 of section 'Inappropriate content')
https://www.autohotkey.com/forumrules/

Reason being it usually generates imaginary made up syntax if you ask it to do much more that toggling your number lock key.
Aleque
Posts: 17
Joined: 24 Apr 2021, 16:55

Re: Image recognition scripts work separately but not together

14 May 2024, 05:26

Sorry it took me some time to reply. It was a lot of information to digest and I needed some time to collect my thoughts to do a proper reply.



#1

#Requires AutoHotkey v1.1.33.11 <-- I forgot to mention that my AHK is version 1.1.33.08 and I am a bit skeptical to update it to a higher version and risk breaking something that works. But maybe I am just being paranoid? Could any of my current scripts (that work in 1.1.33.08) stop working in 1.1.33.11?

But it's good to know that this kind of script (KeyHistory) exists for the future reference. Right now Æ key works as intended, because I saved my script as UTF-8 with BOM (using regular Windows Notepad) before saving it back to an *.AHK file.



#2 about GPI+ <-- I still don't understand what GPI+ is used for exactly and what it's advantages are. Can you please provide some examples? I would like to know more about it but the link that you provided is not very beginner-friendly.


#4 UTF-8 <-- See #1 above.

##1 Try up to 10 times <-- Why 10?

By the way, the script that you provided works and I'm super glad. I have encountered some error in the beginning, and I didn't understand what it was. It has to do with the part where the script presses "P". I didn't understand why it was pressing shift+p. Then I realized that because "P" in the script was specified as a capital letter, then technically it was pressing Shift+p, due to that. I wasn't aware of that behavior. I discovered this when the Script opened another window which was assigned to Shift+p.


##3 if not max_calls_number and calls_number lint, It will run indefinitely until Stack Overflow <-- This sounds a bit technical.

called a recursive function <-- That is a term I haven't heard before.


---

@V0RT3X

ChatGPT output is currently against the forum rules. <-- Sorry, I didn't know that.

Reason being it usually generates imaginary made up syntax if you ask it to do much more that toggling your number lock key. <-- Can you please provide me an example so I know that for future reference?

This is the first time that I used ChatGPT to generate a script of this magnitude. My other scripts are much simpler and shorter. A few days ago I asked it to help me with a much shorter script and it did so successfully, after a bit of trial and error. But I have also learned that when it comes to a bit more complex and longer scripts then it's probably best to ask on forums. So this is what I intend to do in the future.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], mikeyww, Spawnova and 128 guests