[Python] OpenCV OCR with AutoHotkey Continues Clicking even after Text Disappears

Discussão em 'Python' iniciado por Stack, Julho 2, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on a simple autoclicker using AutoHotkey (AHK) v1, OpenCV Python, and Tesseract. The script works perfectly when I test it, for example, when I type "blood wolf" in Notepad, it successfully detects and clicks the text. However, when I remove the text, the script continues to click. Does anyone have insights into why this might be happening?

    ocr_scan.py

    import cv2
    import numpy as np
    import pytesseract
    from PIL import ImageGrab
    import sys
    import os

    target = sys.argv[1].strip().lower() if len(sys.argv) > 1 else "blood wolf"
    memory_file = "last_coords.txt"

    # Grab full screen
    img = np.array(ImageGrab.grab())

    # Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # OCR full text
    full_text = pytesseract.image_to_string(gray).strip().lower()

    # Only continue if full target phrase is present
    if target in full_text:
    data = pytesseract.image_to_data(gray, output_type=pytesseract.Output.DICT)
    for i in range(len(data['text'])):
    word = data['text'].strip().lower()
    if word == target.split()[0]: # use first word to locate
    x = data['left'] + data['width'] // 2
    y = data['top'] + data['height'] // 2
    coords = f"{x},{y}"

    # Save to file temporarily
    with open(memory_file, "w") as f:
    f.write(coords)

    print(coords)

    # Immediately clear memory after returning
    os.remove(memory_file)
    sys.exit(0)

    # If not found, clear memory just in case
    if os.path.exists(memory_file):
    os.remove(memory_file)

    print("not_found")


    and AUTOCLICKER.ahk

    #SingleInstance, Force
    SetWorkingDir %A_ScriptDir%
    SetMouseDelay, 500 ; adds 500ms delay after each Click

    target := "blood wolf"

    Home::
    SetTimer, ScanLoop, 2000
    ToolTip, Started scanning every 2 seconds...
    return

    End::
    SetTimer, ScanLoop, Off
    ToolTip, Stopped scanning.
    Sleep, 1000
    ToolTip
    ExitApp
    return

    ScanLoop:
    FileDelete, result.txt ; clear old result
    RunWait, %ComSpec% /c python ocr_scan.py "%target%" > result.txt,, Hide

    FileRead, coords, result.txt
    StringTrimRight, coords, coords, 1

    if (coords != "" && coords != "not_found" && InStr(coords, ","))
    {
    StringSplit, coord, coords, `,
    x := coord1
    y := coord2

    if (x is number and y is number) {
    Click, %x%, %y%
    sleep, 2000
    ToolTip, Found...
    return
    }
    }

    ToolTip, ❌ Not found...
    return

    Continue reading...

Compartilhe esta Página