-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect.py
More file actions
46 lines (34 loc) · 1.15 KB
/
detect.py
File metadata and controls
46 lines (34 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import cv2
import pyautogui
import numpy as np
import time
def capture_screen_region(x, y, width=100, height=100):
screenshot = pyautogui.screenshot(region=(x - width // 2, y - height // 2, width, height))
frame = np.array(screenshot)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
return frame
def detect_green(frame):
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_green = np.array([35, 50, 50])
upper_green = np.array([85, 255, 255])
mask = cv2.inRange(hsv, lower_green, upper_green)
green_pixels = cv2.countNonZero(mask)
return green_pixels > 50
def left_click():
pyautogui.click()
print("Move cursor near green objects...")
detected = False
try:
while True:
x, y = pyautogui.position()
frame = capture_screen_region(x, y)
if detect_green(frame):
if not detected:
print("Green detected! Clicking...")
left_click()
detected = True
else:
detected = False
time.sleep(0.1)
except KeyboardInterrupt:
print("\nProgram exited.")