-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhands_detector.py
More file actions
94 lines (73 loc) · 3.12 KB
/
hands_detector.py
File metadata and controls
94 lines (73 loc) · 3.12 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
import cv2
import mediapipe as mp
# Importamos las Librerías a usar.
mp_drawing = mp.solutions.drawing_utils # Dibuja los resultados de las detecciones.
mp_hands = mp.solutions.hands # Es un modelo para la detección de las manos.
# Llamamos a los métodos de mediapipe.
cap = cv2.VideoCapture(0) # Para hacer uso de la webcam.
with mp_hands.Hands(
# Entramos a la configuración de mediapipe-hands.
static_image_mode=False, # Puede tener valores de True, o False. False para Streaming y True para imágenes estáticas, o cambiantes.
max_num_hands=2, # El número máximo de manos a detectar.
min_detection_confidence=0.5 # Valor mínimo de confianza. Nos ayuda a saber si la detección es exitosa.
) as hands:
while True:
# Comenzamos con el ciclo para leer los fotogramas.
ret, frame = cap.read()
if not ret:
break
height, width, _ = frame.shape # Pedimos el alto y ancho del fotograma.
frame = cv2.flip(frame, 1) # Lo volteamos para efecto tipo espejo.
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Transformamos los fotogramas a RGB.
results = hands.process(frame_rgb)
if results.multi_hand_landmarks is not None:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
frame,
hand_landmarks,
mp_hands.HAND_CONNECTIONS
)
cv2.imshow("Frame", frame) # Mostramos el vídeo.
q = cv2.waitKey(1)
if q == 27: # 27 es el valor ASCII para ESC.
break # Función para salir de la aplicación con Esc.
cap.release()
#cv2.destroyAllWindows()
# import cv2
# import mediapipe as mp
# mp_drawing = mp.solutions.drawing_utils
# mp_hands = mp.solutions.hands
# cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
# with mp_hands.Hands(
# static_image_mode=False,
# max_num_hands=2,
# min_detection_confidence=0.5
# ) as hands:
# while True:
# ret, frame = cap.read()
# if not ret:
# break
# height, width, _ = frame.shape
# frame = cv2.flip(frame, 1)
# frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# results = hands.process(frame_rgb)
# if results.multi_hand_landmarks is not None:
# for hand_landmarks in results.multi_hand_landmarks:
# mp_drawing.draw_landmarks(
# frame,
# hand_landmarks,
# mp_hands.HAND_CONNECTIONS
# )
# cv2.imshow("Frame", frame)
# q = cv2.waitKey(1)
# if q == 27:
# break
# # Puntos de control:
# print("Number of hands detected:", len(results.multi_hand_landmarks))
# if results.multi_hand_landmarks:
# for i, hand_landmarks in enumerate(results.multi_hand_landmarks):
# print(f"Hand {i+1} landmarks:")
# for idx, landmark in enumerate(hand_landmarks.landmark):
# print(f"Landmark {idx}: ({landmark.x}, {landmark.y}, {landmark.z})")
# cap.release()
# cv2.destroyAllWindows()