-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgesture_mouse.py
More file actions
161 lines (133 loc) · 4.49 KB
/
gesture_mouse.py
File metadata and controls
161 lines (133 loc) · 4.49 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Index tip movement → Mouse movement
# One pinch (thumb + index) → Left click
# Two pinches (double pinch) → Right click
# Pinch + move up/down → Scroll
# Closed fist for 2 seconds → Exit
# On-screen indicators enabled
# Smooth movement
# Response within ~1 second
# 🖱 Mouse movement
# Moves ONLY when index finger is UP
# If fist / pinch / scroll gesture → mouse is frozen
# 👆 Left Click
# Thumb + Index pinch
# Improved using debounce + minimum hold time
# 👉 Right Click
# Double pinch (two pinches within 0.6s)
# 🔄 Scroll (CHANGED AS YOU ASKED)
# Index + Middle finger up
# Move up/down to scroll
# No pinch involved
# ✊ Exit
# Fist held for 3 seconds
# On-screen prompt:
# “Hold fist 3s → Press Y to exit / N to cancel”
import cv2
import mediapipe as mp
import pyautogui
import time
import numpy as np
pyautogui.FAILSAFE = False
# -------- 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
cap = cv2.VideoCapture(0)
# -------- Variables --------
prev_x, prev_y = None, None
mouse_speed = 0.8 # 🔥 increased speed
scroll_speed = 10 # smooth scroll
pinch_state = False
pinch_count = 0
last_pinch_time = 0
PINCH_TIMEOUT = 0.7
fist_start = None
# -------- Helpers --------
def distance(a, b):
return np.linalg.norm(np.array(a) - np.array(b))
# -------- Main Loop --------
while True:
ret, frame = cap.read()
frame = cv2.flip(frame, 1)
h, w, _ = frame.shape
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
result = hands.process(rgb)
if result.multi_hand_landmarks:
hand = result.multi_hand_landmarks[0]
lm = hand.landmark
# Key landmarks
wrist = (int(lm[0].x * w), int(lm[0].y * h))
thumb = (int(lm[4].x * w), int(lm[4].y * h))
index = (int(lm[8].x * w), int(lm[8].y * h))
middle = (int(lm[12].x * w), int(lm[12].y * h))
# Finger states
index_up = lm[8].y < lm[6].y
middle_up = lm[12].y < lm[10].y
thumb_up = lm[4].x > lm[3].x
# -------- EXIT (FIST) --------
if not index_up and not middle_up and not thumb_up:
if fist_start is None:
fist_start = time.time()
elif time.time() - fist_start >= 3:
cv2.putText(frame, "Exit? Press Y / N",
(50, 80), cv2.FONT_HERSHEY_SIMPLEX, 1,
(0, 0, 255), 2)
key = cv2.waitKey(1)
if key == ord('y'):
break
elif key == ord('n'):
fist_start = None
else:
fist_start = None
# -------- PINCH DETECTION --------
pinch_dist = distance(thumb, index)
hand_size = distance(wrist, index)
pinch_threshold = hand_size * 0.20
if pinch_dist < pinch_threshold:
if not pinch_state:
pinch_state = True
pinch_count += 1
last_pinch_time = time.time()
prev_x, prev_y = None, None
else:
pinch_state = False
# -------- PINCH ACTION --------
if pinch_count > 0 and time.time() - last_pinch_time > PINCH_TIMEOUT:
if pinch_count == 1:
pyautogui.leftClick()
print("Left Click")
elif pinch_count >= 2:
pyautogui.rightClick()
print("Right Click")
pinch_count = 0
# -------- SCROLL --------
if index_up and middle_up:
pyautogui.scroll(scroll_speed)
prev_x, prev_y = None, None
continue
if not index_up and not middle_up and thumb_up:
pyautogui.scroll(-scroll_speed)
prev_x, prev_y = None, None
continue
# -------- MOUSE MOVE (RELATIVE) --------
if index_up and not middle_up:
if prev_x is None:
prev_x, prev_y = index
else:
dx = (index[0] - prev_x) * mouse_speed
dy = (index[1] - prev_y) * mouse_speed
pyautogui.moveRel(dx, dy)
prev_x, prev_y = index
else:
prev_x, prev_y = None, None
mp_draw.draw_landmarks(frame, hand, mp_hands.HAND_CONNECTIONS)
cv2.imshow("Gesture Mouse Control", frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
#everything is perfectly fixed except scrolling