-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse2.py
More file actions
114 lines (96 loc) · 3.31 KB
/
mouse2.py
File metadata and controls
114 lines (96 loc) · 3.31 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import cv2
import numpy as np
import pyautogui
# --- Mouse control state ---
last_mouse_pos = None
mouse_down = False
def is_equilateral_triangle(pts, tolerance=0.4):
if len(pts) != 3:
return False
sides = []
for i in range(3):
pt1 = pts[i][0]
pt2 = pts[(i+1)%3][0]
sides.append(np.linalg.norm(pt1 - pt2))
avg = sum(sides) / 3
return all(abs(s - avg)/avg < tolerance for s in sides)
def get_center(contour):
M = cv2.moments(contour)
if M["m00"] == 0:
return None
cx = int(M["m10"] / M["m00"])
cy = int(M["m01"] / M["m00"])
return (cx, cy)
def preprocess_image(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.adaptiveThreshold(blurred, 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY_INV, 11, 4)
kernel = np.ones((3, 3), np.uint8)
return cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=2)
def detect_triangles(frame):
preprocessed = preprocess_image(frame)
contours, _ = cv2.findContours(preprocessed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
triangles = []
for contour in contours:
approx = cv2.approxPolyDP(contour, 0.02 * cv2.arcLength(contour, True), True)
if len(approx) == 3 and is_equilateral_triangle(approx):
area = cv2.contourArea(approx)
x, y, w, h = cv2.boundingRect(approx)
if area > 500 and w > 30 and h > 30:
triangles.append((approx, x, y))
# Sort by x (left to right)
triangles.sort(key=lambda t: t[1])
return triangles[:2]
def control_mouse(triangles):
global last_mouse_pos, mouse_down
if len(triangles) == 2:
center = get_center(triangles[0][0])
if center:
if last_mouse_pos:
dx = center[0] - last_mouse_pos[0]
dy = center[1] - last_mouse_pos[1]
pyautogui.moveRel(dx, dy)
last_mouse_pos = center
if mouse_down:
pyautogui.mouseUp()
mouse_down = False
elif len(triangles) == 1:
center = get_center(triangles[0][0])
if center:
if last_mouse_pos:
dx = center[0] - last_mouse_pos[0]
dy = center[1] - last_mouse_pos[1]
pyautogui.moveRel(dx, dy)
last_mouse_pos = center
if not mouse_down:
pyautogui.mouseDown()
mouse_down = True
else:
last_mouse_pos = None
if mouse_down:
pyautogui.mouseUp()
mouse_down = False
def draw_triangles(frame, triangles):
for tri, x, y in triangles:
cv2.drawContours(frame, [tri], -1, (0, 255, 255), 3)
cv2.circle(frame, get_center(tri), 5, (0, 255, 0), -1)
# OpenCV camera capture
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Could not open webcam")
exit()
while True:
ret, frame = cap.read()
if not ret:
print("Failed to grab frame")
break
triangles = detect_triangles(frame)
control_mouse(triangles)
draw_triangles(frame, triangles)
cv2.imshow("Triangle Mouse Controller", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()