cursor moves to wrong position

Viewed 42

i have the problem only in this part pyautogui.moveTo(startX, startY)

If I need to explain the problem more, since pyautogui does not detect the cropped area, it moves according to all screen sizes and takes my cursor to the wrong coordinates. I need something like a mathematical formula for the part I have indicated above, but I don't know how to do it.

import time

import cv2
import numpy as np
import pyautogui
from windowcapture import WindowCapture
from imutils.object_detection import non_max_suppression

def detect(haystack_img):

    first = True
    needle_img = cv2.imread("Screenshot_1.jpg", cv2.TM_CCOEFF_NORMED)

    result = cv2.matchTemplate(haystack_img, needle_img, cv2.TM_CCORR_NORMED)

    needle_w = needle_img.shape[1]
    needle_h = needle_img.shape[0]

    (yCoords, xCoords) = np.where(result >= 0.95)
    rects = []
    for (x, y) in zip(xCoords, yCoords):
        rects.append((x, y, x + needle_w, y + needle_h))
    pick = non_max_suppression(np.array(rects))

    pyautogui.PAUSE = 0.004


    for (startX, startY, endX, endY) in pick:
        print(startX, startY)
        pyautogui.moveTo(startX, startY)
        cv2.rectangle(haystack_img, (startX, startY), (endX, endY), (0, 255, 0), 2)
        pyautogui.moveTo(startX, (startY))
        break





wincap = WindowCapture("Xx")


screen = wincap.get_screenshot()



crop_img = screen[550:830, 1250:1920]


detect(crop_img)
cv2.imshow("", crop_img)

cv2.waitKey(0)
0 Answers
Related