-
Notifications
You must be signed in to change notification settings - Fork 2
/
ASL_detection_landmark.py
342 lines (299 loc) · 11 KB
/
ASL_detection_landmark.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
from utils import detector_utils as detector_utils
import cv2
import numpy as np
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import multiprocessing
from multiprocessing import Queue, Pool
import time
from utils.detector_utils import WebcamVideoStream
import datetime
import argparse
import os
import keras
# install: pip install --upgrade arabic-reshaper
import arabic_reshaper
# install: pip install python-bidi
from bidi.algorithm import get_display
import dlib
import imutils
from imutils import face_utils
# install: pip install Pillow
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
frame_processed = 0
score_thresh = 0.18
# Create a worker thread that loads graph and
# does detection on images in an input queue and puts it on an output queue
res, score = '', 0.0
sequence = ''
fontFile = "fonts/Sahel.ttf"
font = ImageFont.truetype(fontFile, 70)
categories=[
["ain",'ع'],
["al","ال"],
["aleff",'أ'],
["bb",'ب'],
["dal",'د'],
["dha",'ط'],
["dhad","ض"],
["fa","ف"],
["gaaf",'ج'],
["ghain",'غ'],
["ha",'ه'],
["haa",'ه'],
["jeem",'ج'],
["kaaf",'ك'],
["la",'لا'],
["laam",'ل'],
["meem",'م'],
["nun","ن"],
["ra",'ر'],
["saad",'ص'],
["seen",'س'],
["sheen","ش"],
["ta",'ت'],
["taa",'ط'],
["thaa","ث"],
["thal","ذ"],
["toot",'ت'],
["waw",'و'],
["ya","ى"],
["yaa","ي"],
["zay",'ز']]
def process_image(img):
img = cv2.flip(img, 1)
img = cv2.resize(img, (64, 64))
img = np.array(img, dtype=np.float32)
img = np.reshape(img, (-1, 64 , 64 , 3))
img = img.astype('float32') / 255.
return img
def worker(input_q, output_q, cropped_output_q, inferences_q, landmark_ouput_q,cap_params, frame_processed):
print(">> loading frozen model for worker")
detection_graph, sess = detector_utils.load_inference_graph()
sess = tf.Session(graph=detection_graph)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("landmarks/shape_predictor_68_face_landmarks.dat")
print(">> loading keras model for worker")
try:
model = tf.keras.models.load_model('models/asl_model.h5', compile=False)
except Exception as e:
print(e)
while True:
frame = input_q.get()
if (frame is not None):
boxes, scores = detector_utils.detect_objects(
frame, detection_graph, sess)
# get region of interest
res = detector_utils.get_box_image(cap_params['num_hands_detect'], cap_params["score_thresh"],
scores, boxes, cap_params['im_width'], cap_params['im_height'], frame)
# draw bounding boxes
detector_utils.draw_box_on_image(cap_params['num_hands_detect'], cap_params["score_thresh"],
scores, boxes, cap_params['im_width'], cap_params['im_height'], frame)
# classify hand
if res is not None:
class_res = ""
try:
proba = model.predict(process_image(res))[0]
mx = np.argmax(proba)
score = proba[mx] * 100
sequence = categories[mx][1]
class_res = str(score) + "/" + sequence
except:
score = 0.0
sequence = ""
class_res = "empty"
inferences_q.put(class_res)
image_np1 = imutils.resize(frame, width=400)
gray = cv2.cvtColor(image_np1, cv2.COLOR_BGR2GRAY)
#lanmarking
# detect faces in the grayscale frame
rects = detector(gray, 0)
# loop over the face detections
for rect in rects:
# determine the facial landmarks for the face region, then
# convert the facial landmark (x, y)-coordinates to a NumPy
# array
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
# loop over the (x, y)-coordinates for the facial landmarks
# and draw them on the image
for (x, y) in shape:
cv2.circle(image_np1, (x, y), 1, (0, 0, 255), -1)
# add frame annotated with bounding box to queue
landmark_ouput_q.put(image_np1)
cropped_output_q.put(res)
output_q.put(frame)
frame_processed += 1
else:
output_q.put(frame)
sess.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'-src',
'--source',
dest='video_source',
type=int,
default=0,
help='Device index of the camera.')
parser.add_argument(
'-nhands',
'--num_hands',
dest='num_hands',
type=int,
default=1,
help='Max number of hands to detect.')
parser.add_argument(
'-fps',
'--fps',
dest='fps',
type=int,
default=1,
help='Show FPS on detection/display visualization')
parser.add_argument(
'-wd',
'--width',
dest='width',
type=int,
default=800,
help='Width of the frames in the video stream.')
parser.add_argument(
'-ht',
'--height',
dest='height',
type=int,
default=600,
help='Height of the frames in the video stream.')
parser.add_argument(
'-ds',
'--display',
dest='display',
type=int,
default=1,
help='Display the detected images using OpenCV. This reduces FPS')
parser.add_argument(
'-num-w',
'--num-workers',
dest='num_workers',
type=int,
default=4,
help='Number of workers.')
parser.add_argument(
'-q-size',
'--queue-size',
dest='queue_size',
type=int,
default=5,
help='Size of the queue.')
args = parser.parse_args()
input_q = Queue(maxsize=args.queue_size)
output_q = Queue(maxsize=args.queue_size)
cropped_output_q = Queue(maxsize=args.queue_size)
inferences_q = Queue(maxsize=args.queue_size)
landmark_ouput_q = Queue(maxsize=args.queue_size)
video_capture = WebcamVideoStream(
src=args.video_source, width=args.width, height=args.height).start()
cap_params = {}
frame_processed = 0
cap_params['im_width'], cap_params['im_height'] = video_capture.size()
print(cap_params['im_width'], cap_params['im_height'])
cap_params['score_thresh'] = score_thresh
# max number of hands we want to detect/track
cap_params['num_hands_detect'] = args.num_hands
print(cap_params, args)
# spin up workers to paralleize detection.
pool = Pool(args.num_workers, worker,
(input_q, output_q, cropped_output_q,inferences_q, landmark_ouput_q,cap_params, frame_processed))
start_time = datetime.datetime.now()
num_frames = 0
fps = 0
index = 0
cv2.namedWindow('ASL', cv2.WINDOW_NORMAL)
while True:
frame = video_capture.read()
frame = cv2.flip(frame, 1)
index += 1
input_q.put(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
output_frame = output_q.get()
cropped_output = cropped_output_q.get()
landmark_ouput = landmark_ouput_q.get()
inferences = None
try:
inferences = inferences_q.get_nowait()
except Exception as e:
pass
if(inferences is not None):
try:
score = inferences.split('/')[0]
sequence = inferences.split('/')[1]
except:
score = 0.0
sequence = ""
elapsed_time = (datetime.datetime.now() - start_time).total_seconds()
num_frames += 1
fps = num_frames / elapsed_time
reshaped_text = arabic_reshaper.reshape(sequence)
bidi_text = get_display(reshaped_text)
if (cropped_output is not None):
cropped_output = cv2.cvtColor(cropped_output, cv2.COLOR_RGB2BGR)
img_pil = Image.fromarray(cropped_output)
draw = ImageDraw.Draw(img_pil)
draw.text((30, 100), bidi_text, (255,0,0), font=font)
cropped_output = np.array(img_pil)
if (args.display > 0):
cv2.namedWindow('Cropped', cv2.WINDOW_NORMAL)
cv2.resizeWindow('Cropped', 550, 400)
cv2.imshow('Cropped', cropped_output)
#cv2.imwrite('image_' + str(num_frames) + '.png', cropped_output)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
if (num_frames == 400):
num_frames = 0
start_time = datetime.datetime.now()
else:
print("frames processed: ", index, "elapsed time: ",
elapsed_time, "fps: ", str(int(fps)))
if (landmark_ouput is not None):
landmark_ouput = cv2.cvtColor(landmark_ouput, cv2.COLOR_RGB2BGR)
if (args.display > 0):
cv2.namedWindow('LandMark', cv2.WINDOW_NORMAL)
cv2.resizeWindow('LandMark', 550, 400)
cv2.imshow('LandMark', landmark_ouput)
#cv2.imwrite('image_' + str(num_frames) + '.png', cropped_output)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
if (num_frames == 400):
num_frames = 0
start_time = datetime.datetime.now()
else:
print("frames processed: ", index, "elapsed time: ",
elapsed_time, "fps: ", str(int(fps)))
if (output_frame is not None):
output_frame = cv2.cvtColor(output_frame, cv2.COLOR_RGB2BGR)
if (args.display > 0):
if (args.fps > 0):
detector_utils.draw_fps_on_image("FPS : " + str(int(fps)),
output_frame)
cv2.imshow('ASL', output_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
if (num_frames == 400):
num_frames = 0
start_time = datetime.datetime.now()
else:
print("frames processed: ", index, "elapsed time: ",
elapsed_time, "fps: ", str(int(fps)))
else:
print("video end")
break
elapsed_time = (datetime.datetime.now() - start_time).total_seconds()
fps = num_frames / elapsed_time
print("fps", fps)
pool.terminate()
video_capture.stop()
cv2.destroyAllWindows()