-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_classifier.py
More file actions
95 lines (73 loc) · 3.15 KB
/
test_classifier.py
File metadata and controls
95 lines (73 loc) · 3.15 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
95
import cv2
import mediapipe as mp
import os
import pickle
import numpy as np
import warnings
# Suprimir warnings específicos do protobuf
warnings.filterwarnings("ignore", category=UserWarning, module="google.protobuf.symbol_database")
cap = cv2.VideoCapture(0)
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
# Set max_num_hands=1 for single hand detection
hands = mp_hands.Hands(static_image_mode=False, min_detection_confidence=0.3)
model_dict = pickle.load(open('model.pickle', 'rb'))
model = model_dict['model']
# Check how many features the model expects
print(f"Model expects {model.n_features_in_} features")
labels_dict = {0: 'C', 1: 'M', 2: 'R', 3: 'V'}
while True:
data_aux = []
x_ = []
y_ = []
ret, img = cap.read()
if not ret:
break
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
result = hands.process(img_rgb)
if result.multi_hand_landmarks:
for hand_landmarks in result.multi_hand_landmarks:
mp_drawing.draw_landmarks(
img,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
# Extract features from single hand
for landmark in hand_landmarks.landmark:
x_.append(landmark.x)
y_.append(landmark.y)
data_aux.extend([landmark.x, landmark.y])
# Pad features to match training data (84 features expected)
if len(data_aux) > 0:
try:
# Pad the features to 84 (same as training)
features = np.array(data_aux)
if len(features) < 84:
# Pad with zeros to reach 84 features
padding = np.zeros(84 - len(features))
features = np.concatenate([features, padding])
elif len(features) > 84:
# Truncate to 84 features if somehow we have more
features = features[:84]
prediction = model.predict([features])
predicted_character = labels_dict[int(prediction[0])]
# Visual feedback
if x_ and y_:
H, W, _ = img.shape
x1 = int(min(x_) * W) - 10
y1 = int(min(y_) * H) - 10
x2 = int(max(x_) * W) - 10
y2 = int(max(y_) * H) - 10
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 0), 4)
cv2.putText(img, predicted_character, (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 0, 0), 3, cv2.LINE_AA)
print(f"Predicted: {predicted_character}")
except Exception as e:
print(f"Prediction error: {e}")
cv2.imshow('Sign Language Detection', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()