-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediapipe_hand_gesture.py
50 lines (38 loc) · 1.92 KB
/
mediapipe_hand_gesture.py
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
import cv2
import mediapipe as mp
# Initialize Mediapipe Hand module
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
# Initialize OpenCV webcam capture
cap = cv2.VideoCapture(0)
# Initialize Hand module
with mp_hands.Hands(static_image_mode=False, max_num_hands=1, min_detection_confidence=0.5) as hands:
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Convert BGR image to RGB
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Process the frame to detect hand landmarks
results = hands.process(rgb_frame)
if results.multi_hand_landmarks:
for landmarks in results.multi_hand_landmarks:
# Calculate finger states
thumb_tip = landmarks.landmark[mp_hands.HandLandmark.THUMB_TIP]
index_tip = landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP]
middle_tip = landmarks.landmark[mp_hands.HandLandmark.MIDDLE_FINGER_TIP]
ring_tip = landmarks.landmark[mp_hands.HandLandmark.RING_FINGER_TIP]
pinky_tip = landmarks.landmark[mp_hands.HandLandmark.PINKY_TIP]
finger_states = [index_tip, middle_tip, ring_tip, pinky_tip]
up_fingers = sum(1 for tip in finger_states if tip.y < thumb_tip.y)
finger_count = min(up_fingers + 1, 5)
# Display the detected number on the frame
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, f'Number: {finger_count}', (20, 40), font, 1, (0, 255, 0), 2, cv2.LINE_AA)
# Draw landmarks on the frame
mp_drawing.draw_landmarks(frame, landmarks, mp_hands.HAND_CONNECTIONS)
cv2.imshow('Finger Count', frame)
if cv2.waitKey(1) & 0xFF == 27: # Press 'Esc' to exit
break
cap.release()
cv2.destroyAllWindows()