Skip to content

Commit 53d6521

Browse files
authored
Add files via upload
1 parent 396777b commit 53d6521

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed

Python_code.py

+180
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import cv2 #Libreria OpenCV
2+
import mediapipe as mp #Libreria de mediapipe
3+
from pyfirmata import Arduino, SERVO #Libreria Firmata
4+
5+
board = Arduino('com4') # Declarando puerto de arduino DUE
6+
board.digital[2].mode = SERVO #Pulgar
7+
board.digital[3].mode = SERVO #Indice
8+
board.digital[4].mode = SERVO #Mediano
9+
board.digital[5].mode = SERVO #Anular
10+
board.digital[6].mode = SERVO #Meñique
11+
board.digital[7].mode = SERVO #Muñeca
12+
board.digital[8].mode = SERVO #Pulgar Horizontal
13+
board.digital[9].mode = SERVO #Indice Horizontal
14+
board.digital[10].mode = SERVO #Mediano Horizontal
15+
board.digital[11].mode = SERVO #Anular Horizontal
16+
board.digital[12].mode = SERVO #Meñique Horizontal
17+
board.digital[13].mode = SERVO #Muñeca Horizontal
18+
19+
# Posiciones iniciales de la mano robótica
20+
board.digital[2].write(179) #Pulgar
21+
board.digital[3].write(179) #Indice
22+
board.digital[4].write(179) #Medio
23+
board.digital[5].write(1) #Anular
24+
board.digital[6].write(1) #Meñique
25+
board.digital[7].write(120) #Muñeca
26+
board.digital[8].write(60) #Pulgar Horizontal
27+
board.digital[9].write(48) #Indice Horizontal
28+
board.digital[10].write(75) #Medio Horizontal
29+
board.digital[11].write(66) #Anular Horizontal
30+
board.digital[12].write(67) #Meñique Horizontal
31+
board.digital[13].write(130) #Muñeca Horizontal
32+
33+
# Dimensiones de la pantalla
34+
screen_width = 1280
35+
screen_height = 1024
36+
37+
# Variables para las herramientas del modelo
38+
# de la mano de mediapipe
39+
mp_drawing = mp.solutions.drawing_utils
40+
mp_hands = mp.solutions.hands
41+
42+
# Parametros para la salida de video en la pantalla
43+
hands = mp_hands.Hands(min_detection_confidence=0.7,
44+
min_tracking_confidence=0.9,
45+
max_num_hands=1)
46+
cap = cv2.VideoCapture(2) # Camara Web
47+
cap.set(3, screen_width)
48+
cap.set(4, screen_height)
49+
cap_width = int(cap.get(3))
50+
cap_height = int(cap.get(4))
51+
52+
# Variables para operaciones de detección de mano
53+
no = 1000 #numero de multiplicacion
54+
new_min = int(1) #valor minimo de servomotores
55+
new_max = int(179) #valor maximo de servomotores
56+
57+
while cap.isOpened():
58+
success, image = cap.read()
59+
if not success:
60+
print("No se detecta cámara")
61+
continue
62+
# Voltea la imagen inversamente para el reflejo en la pantalla
63+
# y convierte la imagen BGR a RGB
64+
image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
65+
results = hands.process(image)
66+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
67+
if results.multi_hand_landmarks:
68+
69+
# Posiciones de los dedos en el eje X y eje Y
70+
pulgarx = int(no * (results.multi_hand_landmarks[0].landmark[mp_hands.HandLandmark.THUMB_TIP].x))
71+
pulgary = int(no * (results.multi_hand_landmarks[0].landmark[mp_hands.HandLandmark.THUMB_TIP].y))
72+
indicex = int(no * (results.multi_hand_landmarks[0].landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x))
73+
indicey = int(no * (results.multi_hand_landmarks[0].landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y))
74+
mediox = int(no * (results.multi_hand_landmarks[0].landmark[mp_hands.HandLandmark.MIDDLE_FINGER_TIP].x))
75+
medioy = int(no * (results.multi_hand_landmarks[0].landmark[mp_hands.HandLandmark.MIDDLE_FINGER_TIP].y))
76+
anularx = int(no * (results.multi_hand_landmarks[0].landmark[mp_hands.HandLandmark.RING_FINGER_TIP].x))
77+
anulary = int(no * (results.multi_hand_landmarks[0].landmark[mp_hands.HandLandmark.RING_FINGER_TIP].y))
78+
meñiquex = int(no * (results.multi_hand_landmarks[0].landmark[mp_hands.HandLandmark.PINKY_TIP].x))
79+
meñiquey = int(no * (results.multi_hand_landmarks[0].landmark[mp_hands.HandLandmark.PINKY_TIP].y))
80+
muñecax = int(no * (results.multi_hand_landmarks[0].landmark[mp_hands.HandLandmark.PINKY_MCP].x))
81+
muñecay = int(no * (results.multi_hand_landmarks[0].landmark[mp_hands.HandLandmark.WRIST].y))
82+
83+
# Movimiento de muñeca en vertical
84+
Ny_min = 870
85+
Ny_max = 730
86+
Ny = int(((muñecay - Ny_min) / (Ny_max - Ny_min)) * (new_max - new_min) + new_min)
87+
if Ny >= new_min and Ny <= new_max and muñecax >= 585 and muñecax <= 700:
88+
board.digital[7].write(Ny)
89+
90+
# Movimiento de muñeca en horizontal
91+
Nx_min = 700
92+
Nx_max = 595
93+
Nx = int(((muñecax - Nx_min) / (Nx_max - Nx_min)) * (178 - 3) + 3)
94+
if Nx >= 3 and Nx <= 178:
95+
board.digital[13].write(Nx)
96+
97+
# Condicion para efectuar movimientos de la mano robotica
98+
if muñecay >= 750 and muñecay <= 800 and muñecax >= 485 and muñecax <= 735:
99+
100+
# Pulgar
101+
Py_min = int(580)
102+
Py_max = int(410)
103+
Py = int(((pulgarx - Py_min) / (Py_max - Py_min)) * (new_max - new_min) + new_min)
104+
if Py >= new_min and Py <= new_max:
105+
board.digital[2].write(Py)
106+
107+
# Indice
108+
Iy_min = int(600)
109+
Iy_max = int(300)
110+
Iy = int(((indicey - Iy_min) / (Iy_max - Iy_min)) * (new_max - new_min) + new_min)
111+
if Iy >= new_min and Iy <= new_max:
112+
board.digital[3].write(Iy)
113+
114+
# Medio
115+
My_min = int(600)
116+
My_max = int(240)
117+
My = int(((medioy - My_min) / (My_max - My_min)) * (new_max - 1) + 1)
118+
if My >= 1 and My <= new_max:
119+
board.digital[4].write(My)
120+
121+
# Anular
122+
Ay_min = int(280)
123+
Ay_max = int(620)
124+
Ay = int(((anulary - Ay_min) / (Ay_max - Ay_min)) * (new_max - new_min) + new_min)
125+
if Ay >= new_min and Ay <= new_max:
126+
board.digital[5].write(Ay)
127+
128+
# Meñique
129+
Qy_min = int(385)
130+
Qy_max = int(620)
131+
Qy = int(((meñiquey - Qy_min) / (Qy_max - Qy_min)) * (new_max - new_min) + new_min)
132+
if Qy >= new_min and Qy <= new_max:
133+
board.digital[6].write(Qy)
134+
135+
# Pulgar Horizontal
136+
Px_min = int(480)
137+
Px_max = int(580)
138+
Px = int(((pulgary - Px_min) / (Px_max - Px_min)) * (155 - 1) + 1)
139+
if Px >= 1 and Px <= 155:
140+
board.digital[8].write(Px)
141+
142+
# Indice Horizontal
143+
Ix_min = int(460)
144+
Ix_max = int(560)
145+
Ix = int(((indicex - Ix_min) / (Ix_max - Ix_min)) * (110 - 60) + 60)
146+
if Ix >= 60 and Ix <= 110:
147+
board.digital[9].write(Ix)
148+
149+
# Medio Horizontal
150+
Mx_min = int(500)
151+
Mx_max = int(650)
152+
Mx = int(((mediox - Mx_min) / (Mx_max - Mx_min)) * (152 - 77) + 77)
153+
if Mx >= 77 and Mx <= 152:
154+
board.digital[10].write(Mx)
155+
156+
# Anular Horizontal
157+
Ax_min = int(560)
158+
Ax_max = int(645)
159+
Ax = int(((anularx - Ax_min) / (Ax_max - Ax_min)) * (90 - 45) + 45)
160+
if Ax >= 45 and Ax <= 90:
161+
board.digital[11].write(Ax)
162+
163+
# Meñique Horizontal
164+
Qx_min = int(575)
165+
Qx_max = int(700)
166+
Qx = int(((meñiquex - Qx_min) / (Qx_max - Qx_min)) * (86 - 20) + 20)
167+
if Qx >= 20 and Qx <= 86:
168+
board.digital[12].write(Qx)
169+
170+
for hand_landmarks in results.multi_hand_landmarks: #Dibujos de los puntos de referencia de la mano en la pantal
171+
mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
172+
173+
# Configuración para crear un rectángulo rojo en la pantalla
174+
cv2.rectangle(image, (500, 150), (900, 580), color=(0, 0, 255), thickness=4)
175+
cv2.imshow('Detección de gestos de la mano', image)
176+
if cv2.waitKey(1) & 0xFF == 27:
177+
break
178+
179+
hands.close()
180+
cap.release()

0 commit comments

Comments
 (0)