-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_count.py
More file actions
63 lines (45 loc) · 1.86 KB
/
final_count.py
File metadata and controls
63 lines (45 loc) · 1.86 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
import cv2
import mediapipe as mp
import pygame
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
pygame.mixer.init()
sounds = [
pygame.mixer.Sound("#fa.wav"), # Indice Izquierdo 0
pygame.mixer.Sound("la.wav"), # Medio Izquierdo 1
pygame.mixer.Sound("re.wav"), # Anular Izquierdo 2
pygame.mixer.Sound("#do.wav"), # Indice Derecho 3
pygame.mixer.Sound("#sol.wav"), # Medio Derecho 4
pygame.mixer.Sound("si.wav"), # Anualar Derecho 5
]
def is_finger_down(landmaks, finger_tip, finger_mcp):
return landmaks[finger_tip].y > landmaks[finger_mcp].y
cap = cv2.VideoCapture(1)
with mp_hands.Hands(min_detection_confidence = 0.5, min_tracking_confidence = 0.5,
max_num_hands = 2) as hands:
finger_state = [False]*6
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)
if results.multi_hand_landmarks:
for h, hand_landmarks in enumerate(results.multi_hand_landmarks):
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
finger_tips = [8,12,16]
finger_mpc=[5,9,13]
for i in range(3):
finger_index = i + h * 3
if is_finger_down(hand_landmarks.landmark, finger_tips[i], finger_mpc[i]):
if not finger_state[finger_index]:
sounds[finger_index].play()
finger_state[finger_index] = True
else:
finger_state[finger_index] = False
cv2.imshow('Hand detection', frame)
if cv2.waitKey(1) & 0xFF ==27:
break
cap.release()
cv2.destroyAllWindows()