-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgesture.py
More file actions
56 lines (51 loc) · 1.76 KB
/
Copy pathgesture.py
File metadata and controls
56 lines (51 loc) · 1.76 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
import cv2
import mediapipe as mp
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
hands = mp_hands.Hands(
static_image_mode=False,
max_num_hands=1,
min_detection_confidence=0.7,
min_tracking_confidence=0.7
)
cap = cv2.VideoCapture(0)
prev_x, prev_y = None, None
threshold = 40 # Minimum movement in pixels to register a gesture
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame = cv2.flip(frame, 1)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(rgb_frame)
gesture = None
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
# Index finger tip is landmark 8
h, w, _ = frame.shape
x = int(hand_landmarks.landmark[8].x * w)
y = int(hand_landmarks.landmark[8].y * h)
if prev_x is not None and prev_y is not None:
dx = x - prev_x
dy = y - prev_y
if abs(dx) > abs(dy):
if dx > threshold:
gesture = "Right"
elif dx < -threshold:
gesture = "Left"
else:
if dy > threshold:
gesture = "Down"
elif dy < -threshold:
gesture = "Up"
prev_x, prev_y = x, y
if gesture:
print(f"Gesture detected: {gesture}")
cv2.putText(frame, gesture, (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 2)
cv2.imshow('Gesture Recognition', frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
hands.close()
cv2.destroyAllWindows()