-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface_classifier.py
More file actions
70 lines (56 loc) · 2.32 KB
/
interface_classifier.py
File metadata and controls
70 lines (56 loc) · 2.32 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
import cv2
import mediapipe as mp
import pickle
import numpy as np
model_dict = pickle.load(open('./model.p', 'rb'))
model = model_dict['model']
cap = cv2.VideoCapture(0)
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: 'K', 10: 'L', 11: 'M', 12: 'N',13: 'O', 14 : 'P', 15: 'Q',16: 'R',17: 'S',18: 'T',19: 'U',20: 'V',21: 'W',22: 'X',23: 'Y',24: '0',25: '1',26: '2',27: '3',28: '4',29: '5',30: '6',31: '7',32: '8',33: '9'}
while True:
data_aux = []
x_ = []
y_ = []
ret, frame = cap.read()
height, width, _ = frame.shape
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(frame_rgb)
if results.multi_hand_landmarks:
# One Hand
landmarks = results.multi_hand_landmarks[0]
if len(landmarks.landmark) == 21:
for lm in landmarks.landmark:
mp_drawing.draw_landmarks(
frame,
landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style()
)
for lm in landmarks.landmark:
# for landmarks in results.multi_hand_landmarks:
# for i in range(len(landmarks.landmark)):
# x = landmarks.landmark[i].x
# y = landmarks.landmark[i].y
data_aux.append(lm.x)
data_aux.append(lm.y)
x_.append(lm.x)
y_.append(lm.y)
x1 = int(min(x_) * width) - 10
y1 = int(min(y_) * height) - 10
x2 = int(max(x_) * width) - 10
y2 = int(max(y_) * height) - 10
prediction = model.predict([np.asarray(data_aux)])
predicted = labels_dict[int(prediction[0])]
# print(predicted)
cv2.rectangle(frame, (x1, y1), (x2, y2), (0,0,0), 3)
cv2.putText(frame, predicted, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 1.3, (0,0,0), 3, cv2.LINE_AA)
cv2.imshow('frame',frame)
cv2.waitKey(1)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()