-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhand_control_gripper.py
More file actions
56 lines (44 loc) · 1.59 KB
/
hand_control_gripper.py
File metadata and controls
56 lines (44 loc) · 1.59 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
import serial
import time
# Connect to Arduino (update COM port if needed)
arduino = serial.Serial('COM3', 9600)
time.sleep(2)
# Mediapipe hands setup
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(max_num_hands=1)
mp_draw = mp.solutions.drawing_utils
# Webcam
cap = cv2.VideoCapture(0)
def is_fist(landmarks):
# Fingers folded = fist
return landmarks[8].y > landmarks[6].y and landmarks[12].y > landmarks[10].y
def is_open_palm(landmarks):
# Fingers extended = open hand
return landmarks[8].y < landmarks[6].y and landmarks[12].y < landmarks[10].y
last_state = None
while True:
ret, frame = cap.read()
if not ret:
break
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(rgb)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_draw.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
lm = hand_landmarks.landmark
if is_fist(lm) and last_state != 'fist':
arduino.write(b'O') # Fist now OPENS gripper
last_state = 'fist'
print(" Open (Fist Detected)")
elif is_open_palm(lm) and last_state != 'open':
arduino.write(b'G') # Open hand now GRABS
last_state = 'open'
print(" Grab (Open Hand Detected)")
cv2.imshow("Hand Gesture Control", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
arduino.close()
cv2.destroyAllWindows()