-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhand_mouse.py
More file actions
126 lines (85 loc) · 2.68 KB
/
Copy pathhand_mouse.py
File metadata and controls
126 lines (85 loc) · 2.68 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
115
116
117
118
119
120
121
122
123
124
125
126
import cv2
import mediapipe as mp
import pyautogui
import numpy as np
import time
import math
# ---------------- SCREEN SIZE ----------------
screen_w, screen_h = pyautogui.size()
# ---------------- MEDIAPIPE ----------------
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(
max_num_hands=1,
min_detection_confidence=0.7,
min_tracking_confidence=0.7
)
mp_draw = mp.solutions.drawing_utils
# ---------------- CAMERA ----------------
cap = cv2.VideoCapture(0)
cam_w, cam_h = 640, 480
# ---------------- SMOOTHING ----------------
prev_x, prev_y = 0, 0
smoothening = 0.2
# ---------------- CLICK CONTROL ----------------
click_cooldown = 0
CLICK_DELAY = 0.5
# ---------------- MAIN LOOP ----------------
while True:
ret, frame = cap.read()
if not ret:
break
frame = cv2.flip(frame, 1)
frame = cv2.resize(frame, (cam_w, cam_h))
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
result = hands.process(rgb)
fingertip = None
pinch_dist = None
if result.multi_hand_landmarks:
for hand in result.multi_hand_landmarks:
mp_draw.draw_landmarks(
frame,
hand,
mp_hands.HAND_CONNECTIONS
)
h, w, _ = frame.shape
# Index fingertip (cursor)
ix = int(hand.landmark[8].x * w)
iy = int(hand.landmark[8].y * h)
# Thumb tip (for click)
tx = int(hand.landmark[4].x * w)
ty = int(hand.landmark[4].y * h)
fingertip = (ix, iy)
# draw cursor point
cv2.circle(frame, fingertip, 10, (0,255,0), -1)
# pinch distance
pinch_dist = math.hypot(ix - tx, iy - ty)
# ---------------- MOVE CURSOR ----------------
if fingertip:
# map to screen
x = np.interp(fingertip[0], (0, cam_w), (0, screen_w))
y = np.interp(fingertip[1], (0, cam_h), (0, screen_h))
# smoothing
curr_x = prev_x + (x - prev_x) * smoothening
curr_y = prev_y + (y - prev_y) * smoothening
pyautogui.moveTo(curr_x, curr_y)
prev_x, prev_y = curr_x, curr_y
# ---------------- CLICK GESTURE ----------------
now = time.time()
if pinch_dist and pinch_dist < 30 and now > click_cooldown:
pyautogui.click()
click_cooldown = now + CLICK_DELAY
cv2.putText(
frame,
"CLICK",
(20, 60),
cv2.FONT_HERSHEY_SIMPLEX,
1.5,
(0,0,255),
3
)
# ---------------- DISPLAY ----------------
cv2.imshow("Hand Mouse Control", frame)
if cv2.waitKey(1) == 27:
break
cap.release()
cv2.destroyAllWindows()