-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunAlphabet.py
149 lines (123 loc) · 4.25 KB
/
runAlphabet.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import cv2
import pickle as pkl
import time
import xgboost as xgb
import math
import numpy as np
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
width = 640
height = 480
hands = mp_hands.Hands(min_detection_confidence=0.6, min_tracking_confidence=0.75)
model = pkl.load(open('./models/xgboost-model-alphabets-24', 'rb'))
labels = {
"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",
}
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
videoWriter = cv2.VideoWriter('bahrain.mp4', fourcc, 30, (width,height))
rightHandFirst = False
finalLabel = ''
finalProb = 0
connections = [
(1, 2), (2, 3), (3, 4),
(5, 6), (6, 7), (7, 8),
(9, 10), (10, 11), (11, 12),
(13, 14), (14, 15), (15, 16),
(17, 18), (18, 19), (19, 20),
(0, 5), (0, 17)
]
null_vector = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]
def generatePointVectors(points):
vectors = []
for num, connection in enumerate(connections):
x0, y0 = points[connection[0]]
x1, y1 = points[connection[1]]
x_final = x1 - x0
y_final = y1 - y0
mag = math.sqrt((x_final)**2+(y_final)**2)
x = round((x_final/mag),5)
y = round((y_final/mag),5)
vectors.append(x)
vectors.append(y)
vectors.extend(null_vector)
return vectors
def classify(vectors):
arr = np.array(vectors)
inputData = xgb.DMatrix(arr.data)
prob_list = model.predict(inputData)[0]
max_prob = np.amax(prob_list)
out_label = labels["{}".format(np.argmax(prob_list, axis=0))]
return out_label, max_prob
while cap.isOpened():
success, image = cap.read()
if not success:
break
# Flip the image horizontally for a later selfie-view display, and convert the BGR image to RGB.
image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
# To improve performance, optionally mark the image as not writeable to pass by reference.
image.flags.writeable = False
results = hands.process(image)
if(results.multi_handedness):
# results.multi_handedness[0] is first detected hand
if(results.multi_handedness[0].classification[0].index == 0): # Index 0 is Left, 1 is Right
rightHandFirst = False
else:
rightHandFirst = True
# Draw the hand annotations on the image.
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.multi_hand_landmarks:
rightHandPoints = []
leftHandPoints = []
for hand, hand_landmarks in enumerate(results.multi_hand_landmarks):
if(rightHandFirst): # First hand (0) is Right, Second hand (1) is Left
if(hand == 0):
for idx, landmark in enumerate(hand_landmarks.landmark):
rightHandPoints.append((landmark.x, landmark.y))
else: # First hand (0) is Left, Second hand (1) is Right
if(hand == 1):
for idx, landmark in enumerate(hand_landmarks.landmark):
rightHandPoints.append((landmark.x, landmark.y))
if(len(rightHandPoints) != 0):
finalVectors = generatePointVectors(rightHandPoints)
finalLabel, finalProb = classify(finalVectors)
mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
cv2.putText(image, finalLabel, (width - 200, height - 10), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2, 1)
cv2.putText(image, str(finalProb), (10, height - 10), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2, 1)
cv2.imshow('MediaPipe Hands', image)
if cv2.waitKey(5) & 0xFF == 27:
break
videoWriter.write(image)
hands.close()
videoWriter.release()
cap.release()