【发布时间】:2020-04-08 13:52:52
【问题描述】:
我 3 个月前才开始学习编程,如果这个问题听起来很愚蠢,我很抱歉。 我在这里尝试做的是让我的计算机每 5 秒单击一次左键,而不会暂停 while 循环,以便它继续读取屏幕。
我尝试使用 time.sleep(5)(但它会暂停该功能),而这个 'py.click(x, y, clicks=2, interval=5)' 也可以做同样的事情。
import numpy as np
from PIL import ImageGrab
import cv2
import time
import pyautogui as py
def screen_record():
while True:
printscreen1 = np.array(ImageGrab.grab(bbox=(0, 40, 800, 600)))
printscreen2 = np.array(ImageGrab.grab(bbox=(0, 40, 800, 600)))
diff = cv2.absdiff(printscreen1, printscreen2)
grey = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(grey, (5, 5), 0)
_, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
dilated = cv2.dilate(thresh, None, iterations=3)
contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
(x, y, w, h) = cv2.boundingRect(contour)
if cv2.contourArea(contour) < 700:
continue
else:
# py.click(x, y, clicks=2, interval=5)
# time.sleep(5)
cv2.rectangle(printscreen1, (x, y), (x + w, y + h), (0, 255, 0), 2)
# cv2.drawContours(printscreen1, contours, -1, (0, 255, 0), 2)
cv2.imshow('feed', printscreen1)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
screen_record()
【问题讨论】:
标签: python-3.x opencv