-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference_classifier.py
100 lines (78 loc) · 3.41 KB
/
inference_classifier.py
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
96
97
98
99
100
import pickle
import cv2
import mediapipe as mp
import numpy as np
# Load the trained model from the pickle file
model_dict = pickle.load(open('./model.p', 'rb'))
model = model_dict['model']
# Initialize webcam capture
cap = cv2.VideoCapture(0)
# Set up mediapipe for hand tracking
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
# Initialize hand detector
hands = mp_hands.Hands(static_image_mode=True, min_detection_confidence=0.3)
# Labels for prediction (26 alphabets A-Z)
labels_dict = {i: chr(65 + i) for i in range(26)} # {'0': 'A', '1': 'B', ..., '25': 'Z'}
while True:
data_aux = [] # List to store features
x_ = [] # Temporary list for x coordinates
y_ = [] # Temporary list for y coordinates
# Read the frame from the webcam
ret, frame = cap.read()
if not ret:
print("Failed to grab frame.")
break
# Get frame dimensions
H, W, _ = frame.shape
# Convert frame to RGB for hand detection
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Process the frame with mediapipe to detect hands
results = hands.process(frame_rgb)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
# Draw hand landmarks on the frame
mp_drawing.draw_landmarks(
frame,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
for hand_landmarks in results.multi_hand_landmarks:
# Extract x and y coordinates from hand landmarks
for i in range(len(hand_landmarks.landmark)):
x = hand_landmarks.landmark[i].x
y = hand_landmarks.landmark[i].y
# Collect x and y values in separate lists for normalization
x_.append(x)
y_.append(y)
# Normalize and append features to data_aux
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_)) # Normalize x
data_aux.append(y - min(y_)) # Normalize y
# Define bounding box for hand detection (optional)
x1 = int(min(x_) * W) - 10
y1 = int(min(y_) * H) - 10
x2 = int(max(x_) * W) + 10
y2 = int(max(y_) * H) + 10
# Predict the character from the model
prediction = model.predict([np.asarray(data_aux)])
# If the model returns the character directly, no need to convert it to an integer
predicted_character = prediction[0] # Directly use the predicted character
# Draw bounding box and predicted text on the frame
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, cv2.LINE_AA)
else:
# If no hands are detected, show "Undetected" text
cv2.putText(frame, "Undetected", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0, 0, 255), 3, cv2.LINE_AA)
# Display the frame
cv2.imshow('frame', frame)
# Exit the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release resources and close windows
cap.release()
cv2.destroyAllWindows()