in the mss library, frames stop updating (or 1 frame is updated every 2-3 seconds) after connecting pydirectimpit.moveTo

I am making a bot that should detect the blue object I need and click on it. Everything works correctly until I tell the bot to move the mouse to these blue objects. That is, the image simply stops updating or 1 frame is updated in 2-3 seconds

import numpy as np
import cv2
import mss
import pydirectinput as pyautogu
import time
from PIL import Image
import mss.tools

time.sleep(0)

sct = mss.mss()

#click func
def click():
    time.sleep(0)
    click()

#object centre pos detect
def BoxPosit():
    pyautogu.moveTo(cx + 1200, cy + 400)
    click()


last_time = time.time()


while True:                
    #capturing size
    monitor = {"top": 400, "left": 1200, "width": 600 , "height": 400}
    #grab the data
    img = sct.grab(monitor)

    #create imgee
    img = Image.frombytes("RGB", img.size, img.bgra, "raw", "BGRX")
    imge = "monitor-{}.jpg".format(1)

    #save imgee
    img.save(imge)


    #converting to numpy array
    imge = np.array(sct.grab(monitor))

    #converting to HSV
    hsv = cv2.cvtColor(imge, cv2.COLOR_BGR2HSV)
    lower_dark_blue = np.array([104, 50 , 50])
    upper_dark_blue = np.array([130, 255, 255])
    #creating mask
    mask = cv2.inRange(hsv, lower_dark_blue, upper_dark_blue)


    #saving mask to a folder
    maskname = 'secondimgee.jpg'    #filename
    cv2.imwrite(maskname, mask)


    #converting to grayscale

    img2 = cv2.imread(maskname, cv2.IMREAD_COLOR)  
    image = cv2.imread(maskname, cv2.IMREAD_GRAYSCALE)

    #converting image to a binary image
    _, threshold = cv2.threshold(image, 110, 255, cv2.THRESH_BINARY)

    #detection contours
    contours, _= cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    #checking every contours
    for cnt in contours :
        area = cv2.contourArea(cnt)

        approx = cv2.approxPolyDP(cnt, 0.009 * cv2.arcLength(cnt, True), True)
    

        M = cv2.moments(cnt)


        if M["m00"] != 0:
            cx = int(M["m10"] / M["m00"])
            cy = int(M["m01"] / M["m00"])
        else:
            cx, cy = 0, 0


        if area > 50:
            cv2.drawContours(img2, [approx], 0, (0, 0, 255), 5)
            BoxPosit()   #here frames stop updating
            click()
            
    cv2.imshow('ewa', img2)

    if cv2.waitKey(1) & 0xFF == ord('q'): 
        cv2.destroyAllWindows()
        break
    

how i can fix this?

thanks in advance


Ответы (0 шт):