-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebcam_gesture_recognition.py
More file actions
68 lines (52 loc) · 2.05 KB
/
webcam_gesture_recognition.py
File metadata and controls
68 lines (52 loc) · 2.05 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
import cv2
import mediapipe as mp
import numpy as np
import joblib
# Load your trained model and label encoder
model = joblib.load("gesture_model.pkl")
label_encoder = joblib.load("label_encoder.pkl")
# Initialize MediaPipe Hands
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
hands = mp_hands.Hands(
static_image_mode=False,
max_num_hands=1,
min_detection_confidence=0.7,
min_tracking_confidence=0.7)
cap = cv2.VideoCapture(0) # 0 = default webcam
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print("Ignoring empty camera frame.")
continue
# Flip frame horizontally for selfie-view and convert BGR to RGB
frame_rgb = cv2.cvtColor(cv2.flip(frame, 1), cv2.COLOR_BGR2RGB)
# Process the frame to detect hands
results = hands.process(frame_rgb)
# Convert back to BGR for OpenCV
frame = cv2.cvtColor(frame_rgb, cv2.COLOR_RGB2BGR)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
# Draw hand landmarks on frame
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
# Extract landmarks as a flat list [x0, y0, z0, x1, y1, z1, ...]
landmarks = []
for lm in hand_landmarks.landmark:
landmarks.extend([lm.x, lm.y, lm.z])
# Convert landmarks to numpy array for prediction
landmarks_np = np.array(landmarks).reshape(1, -1)
# Predict gesture label using your model
prediction = model.predict(landmarks_np)
predicted_label = label_encoder.inverse_transform(prediction)[0]
# Display the predicted label on the frame
cv2.putText(frame, f'Prediction: {predicted_label}', (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# Show the frame
cv2.imshow('Gesture Recognition', frame)
# Exit loop on 'ESC' key press
if cv2.waitKey(5) & 0xFF == 27:
break
# Release resources
cap.release()
cv2.destroyAllWindows()
hands.close()