-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference.py
More file actions
97 lines (75 loc) · 3.13 KB
/
inference.py
File metadata and controls
97 lines (75 loc) · 3.13 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
96
97
import pickle
import cv2
import mediapipe as mp
import numpy as np
import pyautogui
model_dict = pickle.load(open('./model.p', 'rb'))
model = model_dict['model']
cap = cv2.VideoCapture(0)
#
# # Keep the camera window on top
# cv2.namedWindow("model", cv2.WND_PROP_TOPMOST)
# cv2.setWindowProperty("model", cv2.WND_PROP_TOPMOST, 1)
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
hands = mp_hands.Hands(static_image_mode=True, min_detection_confidence=0.3)
labels_dict = {
0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', 5: 'F', 6: 'G', 7: 'H', 8: 'I', 9: 'J',
10: 'K', 11: 'L', 12: 'M', 13: 'N', 14: 'O', 15: 'P', 16: 'Q', 17: 'R', 18: 'S', 19: 'T',
20: 'U', 21: 'V', 22: 'W', 23: 'X', 24: 'Y', 25: 'Z',
26: '1', 27: '2', 28: '3', 29: '4', 30: '5', 31: '6', 32: '7', 33: '8', 34: '9', 35: '0',
36: 'A', 37: 'B', 38: 'C', 39: 'D'
}
expected_features = 100
current_frame = 0
# alphabet = []
while True:
current_frame += 1
ret, frame = cap.read()
if not ret:
print("Failed to read frame from the camera.")
continue
H, W, _ = frame.shape
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(frame_rgb)
if results.multi_hand_landmarks:
data_aux = []
x_ = []
y_ = []
for hand_landmarks in results.multi_hand_landmarks:
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 landmark in hand_landmarks.landmark:
x_.append(landmark.x)
y_.append(landmark.y)
for landmark in hand_landmarks.landmark:
data_aux.append(landmark.x - min(x_))
data_aux.append(landmark.y - min(y_))
# Adjust data_aux to match the expected number of features
if len(data_aux) < expected_features:
data_aux.extend([0] * (expected_features - len(data_aux)))
if len(data_aux) == expected_features:
prediction = model.predict([np.asarray(data_aux)])
predicted_character = labels_dict[int(prediction[0])]
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,
cv2.LINE_AA)
if current_frame % 30 == 0:
alphabet = predicted_character
# print(alphabet)
pyautogui.write(alphabet)
else:
print(f"Warning: Unexpected number of features: {len(data_aux)}")
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'): # Press 'q' to quit
break
cap.release()
cv2.destroyAllWindows()