-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsign_detection.py
More file actions
80 lines (62 loc) · 2.66 KB
/
sign_detection.py
File metadata and controls
80 lines (62 loc) · 2.66 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
import pickle
import cv2
import mediapipe as mp
import numpy as np
# Load trained model
model_dict = pickle.load(open('model.p', 'rb'))
model = model_dict['model']
# Initialize MediaPipe Hands module
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
hands = mp_hands.Hands(static_image_mode=False, min_detection_confidence=0.3, min_tracking_confidence=0.3)
# Label mapping (update this if you have different classes)
labels_dict = {0: 'A', 1: 'B', 2: 'L'}
cap = cv2.VideoCapture(0)
while True:
data_aux = []
x_ = []
y_ = []
ret, frame = cap.read()
if not ret:
break
H, W, _ = frame.shape
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(frame_rgb)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
for i in range(len(hand_landmarks.landmark)):
x = hand_landmarks.landmark[i].x
y = hand_landmarks.landmark[i].y
x_.append(x)
y_.append(y)
# Normalize landmarks by subtracting the min values (as you did in training)
for i in range(len(hand_landmarks.landmark)):
x = hand_landmarks.landmark[i].x
y = hand_landmarks.landmark[i].y
data_aux.append(x - min(x_))
data_aux.append(y - min(y_))
# Debug: Print length of features
print(f"Extracted features length: {len(data_aux)}") # Should be 84 (42 landmarks * 2 for x and y)
# Ensure data has the correct length before prediction
if len(data_aux) == 84: # The model expects 84 features (42 landmarks * 2)
prediction = model.predict([np.asarray(data_aux)])
predicted_character = labels_dict[int(prediction[0])]
print(f"Predicted character: {predicted_character}")
# Convert normalized coordinates back to pixel values
x1, y1 = int(min(x_) * W) - 10, int(min(y_) * H) - 10
x2, y2 = int(max(x_) * W) - 10, int(max(y_) * H) - 10
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 0), 4)
cv2.putText(frame, predicted_character, (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 0, 0), 3)
else:
print("Feature extraction error: Expected 84 features, but got:", len(data_aux))
else:
print("No hands detected")
# Display the result
cv2.imshow('frame', frame)
# Exit the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()