-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.py
273 lines (242 loc) · 9.49 KB
/
Main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import os
import cv2
import numpy as np
from PIL import Image
from keras import models
import tensorflow as tf
import mediapipe as mp
import math
# Runs real-time ASL prediction
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands
def find_hand_period(path):
video = cv2.VideoCapture(path)
start = None
end = None
counter = 0
last_frame = None
hands_found = False
while video.isOpened():
with mp_hands.Hands(
static_image_mode=True,
max_num_hands=1,
min_detection_confidence=0.5) as hands:
while hands_found == False:
last_frame = counter
video.set(cv2.CAP_PROP_POS_FRAMES, counter)
_, frame = video.read()
results = hands.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
if counter >= video.get(cv2.CAP_PROP_FRAME_COUNT) - 14:
start = 0
end = 0
hands_found = True
else:
if results.multi_hand_landmarks:
hands_found = True
counter += 12
while start == None:
last_frame = counter
video.set(cv2.CAP_PROP_POS_FRAMES, counter)
_, frame = video.read()
results = hands.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
if not results.multi_hand_landmarks:
start = last_frame
counter += 4
else:
if counter == 0:
start = 0
else:
counter -= 4
last_frame = counter
video.set(cv2.CAP_PROP_POS_FRAMES, counter)
_, frame = video.read()
results = hands.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
if results.multi_hand_landmarks:
if counter >= video.get(cv2.CAP_PROP_FRAME_COUNT) - 6:
end = counter
video.release()
else:
counter += 4
else:
end = last_frame
video.release()
return start, end
# Used for single image prediction #
def predict_image():
model = models.load_model('Models\\model.h5')
guesses = ['A','B','C','D','E','F','G','H','I','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y']
# url = "https://www.signingsavvy.com/images/words/alphabet/2/u1.jpg"
# path = tf.keras.utils.get_file('u', url)
path = "asl_alphabet_test\\S_test.jpg"
with mp_hands.Hands(
static_image_mode=True,
max_num_hands=1,
min_detection_confidence=0.5) as hands:
image = cv2.flip(cv2.imread(path), 1)
results = hands.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
landmarks = []
for hand_landmarks in results.multi_hand_landmarks:
for landmark in hand_landmarks.landmark:
landmarks.append((landmark.x, landmark.y))
result = []
for i in range(0, 21):
for j in range(1,21):
if j > i:
result.append(math.sqrt( abs(landmarks[i][0] - landmarks[j][0])**2 + abs(landmarks[i][1] - landmarks[j][1])**2 ))
result = np.array([result])
predictions = model.predict(result)
score = tf.nn.softmax(predictions[0])
scores = list(zip(guesses, list(predictions[0])))
pred_list = []
for tup in scores:
pred_list += [tup[1]]
confidence = max(pred_list) * 100
print("This image most likely belongs to {} with a {:.2f} percent confidence."
.format(guesses[np.argmax(score)], confidence)
)
def predict_video_mac(video):
model = models.load_model('Models/Alphabet.h5')
words = []
for name in os.listdir("alphabet_video"):
words.append(name)
with mp_hands.Hands(
static_image_mode=True,
max_num_hands=1,
min_detection_confidence=0.5) as hands:
vid = cv2.VideoCapture(video)
while vid.isOpened():
frame_id = 0
collected_frames = 0
start, end = find_hand_period(video)
mesh_dur = end - start
frame_skip = mesh_dur / 4
frame_id += start
if start == None and end == None:
continue
else:
while collected_frames < 4:
vid.set(cv2.CAP_PROP_POS_FRAMES, frame_id)
_, frame = vid.read()
results = hands.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
if not results.multi_hand_landmarks:
frame_id += 1
else:
landmarks = []
for landmark in results.multi_hand_landmarks[0].landmark:
landmarks.append([landmark.x, landmark.y])
if collected_frames == 0:
result += [landmarks[3][0], landmarks[3][1], landmarks[7][0], landmarks[7][1]]
for i in range(0, 21):
for j in range(1, 21):
distance = math.sqrt( (landmarks[i][0] - landmarks[j][0])**2 + (landmarks[i][1] - landmarks[j][1])**2 )
if j > i:
result.append(distance)
collected_frames += 1
frame_id += frame_skip
vid.release()
result = np.array([result])
predictions = model.predict(result)
score = tf.nn.softmax(predictions[0])
scores = list(zip(words, list(predictions[0])))
pred_list = []
for tup in scores:
pred_list += [tup[1]]
confidence = max(pred_list) * 100
print("This video most likely portrays \"{}\" with a {:.2f}% confidence."
.format(words[np.argmax(score)], confidence)
)
return words[np.argmax(score)]
def predict_video(video):
model = models.load_model('Models\\Alphabet.h5')
words = []
result = []
for name in os.listdir("alphabet_video"):
words.append(name)
with mp_hands.Hands(
static_image_mode=True,
max_num_hands=1,
min_detection_confidence=0.5) as hands:
vid = cv2.VideoCapture(video)
while vid.isOpened():
frame_id = 0
collected_frames = 0
start, end = find_hand_period(video)
mesh_dur = end - start
frame_skip = mesh_dur / 4
frame_id += start
if start == None and end == None:
continue
else:
while collected_frames < 4:
vid.set(cv2.CAP_PROP_POS_FRAMES, frame_id)
_, frame = vid.read()
results = hands.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
if not results.multi_hand_landmarks:
frame_id += 1
else:
landmarks = []
for landmark in results.multi_hand_landmarks[0].landmark:
landmarks.append([landmark.x, landmark.y])
if collected_frames == 0:
result += [landmarks[3][0], landmarks[3][1], landmarks[7][0], landmarks[7][1]]
for i in range(0, 21):
for j in range(1, 21):
distance = math.sqrt( (landmarks[i][0] - landmarks[j][0])**2 + (landmarks[i][1] - landmarks[j][1])**2 )
if j > i:
result.append(distance)
collected_frames += 1
frame_id += frame_skip
vid.release()
result = np.array([result])
predictions = model.predict(result)
score = tf.nn.softmax(predictions[0])
scores = list(zip(words, list(predictions[0])))
pred_list = []
for tup in scores:
pred_list += [tup[1]]
confidence = max(pred_list) * 100
print("This video most likely portrays \"{}\" with a {:.2f}% confidence."
.format(words[np.argmax(score)], confidence)
)
return words[np.argmax(score)]
# Used for webcam prediction (alphabet_only)
def live_prediction():
model = models.load_model('Models\\model.h5')
guesses = ['A','B','C','D','E','F','G','H','I','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y']
cap = cv2.VideoCapture(0)
while cap.isOpened():
path2 = "asl_alphabet_test\\"
_, frame = cap.read()
with mp_hands.Hands(
static_image_mode=False,
max_num_hands=1,
min_detection_confidence=0.5) as hands:
frame=cv2.flip(frame, 1)
results = hands.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
cv2.imshow("Capturing", frame)
result = []
landmarks = []
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
for landmark in hand_landmarks.landmark:
landmarks.append((landmark.x, landmark.y))
for i in range(0, 21):
for j in range(1,21):
if j > i:
result.append(math.sqrt( (landmarks[i][0] - landmarks[j][0])**2 + (landmarks[i][1] - landmarks[j][1])**2 ))
result = np.array([result])
predictions = model.predict(result)
score = tf.nn.softmax(predictions[0])
p = guesses[np.argmax(score)]
path2 = cv2.imread(str(path2) + "{}_test.jpg".format(p))
cv2.imshow("Prediction", path2)
else:
path2 = cv2.imread(str(path2) + "nothing_test.jpg")
cv2.imshow("Prediction", path2)
key=cv2.waitKey(1)
if key == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# predict_video("recordings\\e.mp4")