forked from amfang/MAX-Facial-Age-Estimator-Web-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·485 lines (382 loc) · 16.9 KB
/
app.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#
# Copyright 2018 IBM Corp. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import base64
import cv2
import json
import numpy as np
import requests
import threading
import time
from concurrent.futures import ThreadPoolExecutor
from flask import Flask, render_template, Response
from gevent import monkey
from io import BytesIO
from PIL import Image
try:
from flask.ext.socketio import SocketIO, emit
except ImportError:
from flask_socketio import SocketIO, emit
################################################################################
# GLOBALS
monkey.patch_all()
app = Flask(__name__)
app.config.from_object('config')
# Condition variable for passing incoming frames to the video processing thread
app.condition_var = threading.Condition()
# Zero or one-element list holding the most recent video frame, if available.
# Guarded by app.condition_var.
app.latest_frame_list = []
# Time that the most recent iteration of the main image processing loop began.
# Used for calculating and printing FPS and latency.
app.start_time = time.time()
socketio = SocketIO(app)
################################################################################
# HANDLERS
@app.route("/")
def index():
"""Video streaming home page."""
return render_template('index.html')
@socketio.on('netin', namespace='/streaming')
def msg(dta):
emit('response', {'data': dta['data']})
@socketio.on('connected', namespace='/streaming')
def connected():
emit('response', {'data': 'OK'})
@socketio.on('streamingvideo', namespace='/streaming')
def webdata(dta):
print("{:5.3f} Image received".format(time.time() - app.start_time))
with app.condition_var:
# Clear stale frames. In the future we may retain some of these frames
# to aid in object tracking.
app.latest_frame_list.clear()
app.latest_frame_list.append(dta['data'])
app.condition_var.notify()
@app.route('/video_feed')
def video_feed():
"""Video streaming route. Put this in the src attribute of an img tag."""
return Response(gen(),
mimetype='multipart/x-mixed-replace; boundary=frame')
################################################################################
# MAIN LOOP
def gen():
# FPS now regulated in client.
# TARGET_FPS = 30.0
# FRAME_TIME_INTERVAL = 1.0 / TARGET_FPS
# Width of the images we send to the expensive model for inference
INFERENCE_IMAGE_WIDTH_PX = 1024
# Width of the images we use for local object tracking
TRACKING_IMAGE_WIDTH_PX = 256
# Width of the images we send back to the browser
DISPLAY_IMAGE_WIDTH_PX = 1024
# If True, skip all the machine learning stuff to help debug end-to-end
# latency issues.
SKIP_INFERENCE = False
# Factor to use for exponentially decaying averages.
# 0.0 => ignore new values, 1.0 => ignore old values
EXP_DECAY_FACTOR = 0.1
# Number of frames since the last time the expensive model was run. Used
# for choosing box color.
frames_since_update = 0
tracker = cv2.MultiTracker_create()
executor = ThreadPoolExecutor(max_workers=1)
future = None
# The image submitted for the most recent inference request, downsampled
# to a width of TRACKING_IMAGE_WIDTH_PX pixels
last_inference_image = None
# List of images captured since the last time an image was submitted to
# the expensive backend model. Image size determined by
# TRACKING_IMAGE_WIDTH_PX.
images_since_submit = []
# Bounding boxes of faces in the most recent frame, relative to
# TRACKING_IMAGE_WIDTH_PX.
bounding_boxes = []
# Age estimates corresponding to the bounding boxes.
# When more than one age has been received, these are exponentially
# decaying averages
age_results = []
# Timestamp of the most recent frame processed
frame_ts = 0.
while True:
with app.condition_var:
# Wait for the browser to send an image
while len(app.latest_frame_list) == 0:
app.condition_var.wait()
img_data = app.latest_frame_list.pop()
last_frame_ts = frame_ts
frame_ts = time.time()
print("{:5.3f} ==> Image dequeued ({:4.1f} FPS)"
"".format(time.time() - app.start_time,
1.0 / (frame_ts - last_frame_ts)))
input_img = base64_to_pil_image(img_data.split('base64')[-1])
raw_img_np_frame = np.array(input_img)
# Mirror effect
raw_img_np_frame = cv2.flip(raw_img_np_frame, 1)
if SKIP_INFERENCE:
print("{:5.3f} ==> Image sent"
"".format(time.time() - app.start_time))
yield(gen_result_bytes(raw_img_np_frame))
# regulate_fps(start, FRAME_TIME_INTERVAL)
continue
# Create versions of the image at different sizes for different
# purposes.
inference_np_frame = resize_image(raw_img_np_frame, INFERENCE_IMAGE_WIDTH_PX)
tracking_np_frame = resize_image(raw_img_np_frame, TRACKING_IMAGE_WIDTH_PX)
display_np_frame = resize_image(raw_img_np_frame, DISPLAY_IMAGE_WIDTH_PX)
# Start by handling any outstanding results from previous model
# invocations.
if future is not None and future.done():
# Remember the previous results so we can connect them with the
# new results.
last_inference_image_bounding_boxes = bounding_boxes
last_inference_image_ages = age_results
predict_results = future.result()
bounding_boxes = [entry['face_box'] for entry in predict_results]
age_results = [entry['age_estimation'] for entry in predict_results]
# Scale the bounding boxes to the image size we use for tracking.
bounding_boxes = scale_bounding_boxes(bounding_boxes,
INFERENCE_IMAGE_WIDTH_PX,
TRACKING_IMAGE_WIDTH_PX)
tracker = update_trackers(last_inference_image, bounding_boxes)
# Play back the video that has happened since the image was
# submitted for inference, updating the bounding boxes as we go
for img in images_since_submit:
_, _ = tracker.update(img)
# Match faces from the previous match with the current match
bbox_mapping = match_bounding_boxes(last_inference_image_bounding_boxes,
bounding_boxes)
for old_ix, new_ix in bbox_mapping:
old_age = last_inference_image_ages[old_ix]
new_age = age_results[new_ix]
exp_decay_average_age = (new_age * EXP_DECAY_FACTOR
+ old_age * (1.0 - EXP_DECAY_FACTOR))
age_results[new_ix] = exp_decay_average_age
frames_since_update = 0
else:
frames_since_update += 1
# Start a new inference request if it is appropriate to do so.
if future is None or future.done():
future = executor.submit(predict_age_local, inference_np_frame)
last_inference_image = tracking_np_frame
del images_since_submit[:]
else:
images_since_submit.append(tracking_np_frame)
# Use CV2 MultiTracker to track faces and pair ages to face
# For now, every box gets the same color.
color_tuple = box_color(frames_since_update)
success, bounding_boxes = tracker.update(tracking_np_frame)
scaled_bounding_boxes = scale_bounding_boxes(bounding_boxes,
TRACKING_IMAGE_WIDTH_PX,
DISPLAY_IMAGE_WIDTH_PX)
for i, (box, age) in enumerate(zip(scaled_bounding_boxes, age_results)):
display_np_frame = draw_boxes_and_label(display_np_frame, str(int(age)),
box, color_tuple)
print("{:5.3f} ==> Annotated image sent"
"".format(time.time() - app.start_time))
yield(gen_result_bytes(display_np_frame))
# regulate_fps(start, FRAME_TIME_INTERVAL)
################################################################################
# SUBROUTINES
def box_color(frames_since_update):
"""
Compute the color of the bounding box.
The color fades from red to yellow as we get further from an inference
result.
Args:
frames_since_update: How many frames have been displayed since
updating the age
"""
HOT_COLOR = np.array([255., 0., 0.])
COLD_COLOR = np.array([255., 255., 0.])
DECAY_TIME_FRAMES = 10
if frames_since_update > DECAY_TIME_FRAMES:
return tuple(COLD_COLOR)
else:
cold_weight = frames_since_update / DECAY_TIME_FRAMES
hot_weight = 1.0 - cold_weight
color = hot_weight * HOT_COLOR + cold_weight * COLD_COLOR
return tuple(color)
def draw_label(image, point, label, font=cv2.FONT_HERSHEY_SIMPLEX,
font_scale=1, thickness=2):
size = cv2.getTextSize(label, font, font_scale, thickness)[0]
x, y = point
cv2.rectangle(image, (x, y - size[1]), (x + size[0], y),
(255, 0, 0), cv2.FILLED)
cv2.putText(image, label, point, font, font_scale,
(255, 255, 255), thickness)
def draw_boxes_and_label(image, label, box, color=(255, 255, 0)):
"""
Modify an image by inserting a labeled bounding box.
Args:
image: The original image as a numpy ndarray
label: Text label string to apply to the box
box: list of integers (x1, y1, x2, y2) that describe the
coordinates of the upper left corner and the width
and height of the box
color: Tuple of RGB values to use as the color of the box
Returns the original image, with the indicated box drawn
"""
x1, y1, x2, y2 = (int(c) for c in box)
p1 = (x1, y1)
p2 = (x1 + x2, y1 + y2)
cv2.rectangle(image, p1, p2, color, 2, 1)
draw_label(image, p1, label)
return image
def draw_FPS(image, fps):
cv2.putText(image, "FPS: {}".format("%.4f" % fps), (20, 20),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
def base64_to_pil_image(base64_img):
return Image.open(BytesIO(base64.b64decode(base64_img)))
def convert_to_JPEG(np_image_frame):
# np_image_color = cv2.cvtColor(np_image_frame, cv2.COLOR_BGR2RGB)
image = Image.fromarray(np_image_frame)
with BytesIO() as f:
image.save(f, format='JPEG', quality=95)
return f.getvalue()
def resize_image(img_np, target_width_px):
"""
Subroutine to resize an image using OpenCV.
Args:
img_np: Numpy array containing image pixels
target_width_px: Target width for the image, in pixels.
Image height will be scaled byt he same factor.
Returns:
resized image as a numpy array
"""
img_h, img_w, _ = img_np.shape
if img_w == target_width_px:
return img_np # Resize not necessary
else:
return cv2.resize(img_np, (target_width_px,
int(target_width_px * img_h / img_w)))
def gen_result_bytes(np_frame):
# draw_FPS(display_np_frame, frames_per_second)
result_image = convert_to_JPEG(np_frame)
return (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + result_image + b'\r\n')
def regulate_fps(start_time, frame_time_interval):
"""
CURRENTLY UNUSED.
Delay processing to meet a target loop time. Call this at the
end of a tight loop.
Args:
start_time: Time that the current iteration started
frame_time_interval: Target loop time
"""
loop_process_time = time.time() - start_time
if loop_process_time < frame_time_interval:
time.sleep(frame_time_interval - loop_process_time)
actual_loop_time = time.time() - start_time
frames_per_second = 1.0 / actual_loop_time
print("Processing time {:4.3f} sec; FPS {:4.2f}"
"".format(loop_process_time, frames_per_second))
def predict_age_local(np_image):
image = convert_to_JPEG(np_image)
my_files = {'image': image,
'Content-Type': 'multipart/form-data',
'accept': 'application/json'}
r = requests.post('https://localhost:5000/model/predict',
files=my_files, json={"key": "value"})
json_str = json.dumps(r.json())
data = json.loads(json_str)
return data['predictions']
def update_trackers(image, bounding_boxes):
tracker = cv2.MultiTracker_create()
for box in bounding_boxes:
# Old code was:
# tracker.add(cv2.TrackerKCF_create(), image, tuple(box))
# We use MedianFlow tracker now because it is faster. Even though the
# algorithm is less accurate, results are more accurate because we drop
# fewer frames.
tracker.add(cv2.TrackerMedianFlow_create(), image, tuple(box))
return tracker
def scale_bounding_boxes(bounding_boxes, orig_width, new_width):
"""
Scale a list of bounding boxes to reflect a change in image size.
Args:
bounding_boxes: List of lists of [x1, y1, x2, y2], where
(x1, y1) is the upper left corner of the box, x2 is the width
of the box, and y2 is the height of the box.
orig_width: Width of the images to which bounding_boxes apply
new_width: Width of the target images to which the bounding boxes
should be translated
Returns:
A new list of bounding boxes with the appropriate scaling factor
applied.
"""
scale_factor = new_width / orig_width
ret = []
# Use a for loop because OpenCV doesn't play well with generators
for bbox in bounding_boxes:
new_bbox = []
for elem in bbox:
new_elem = round(float(elem) * scale_factor)
new_bbox.append(new_elem)
ret.append(new_bbox)
return ret
def match_bounding_boxes(old_bounding_boxes, new_bounding_boxes):
"""
Find matches between two sets of bounding boxes.
We currently match based on distance between the centers of the boxes.
Args:
old_bounding_boxes: List of lists of bounding box coords, where
each entry is in the format [x1, y1, x2, y2] and describes the
upper-left corner and width and height of the box
new_bounding_boxes: Second list of bounding boxes in the same format
as the first
Returns a list of tuples: (old_ix, new_ix), where old_ix and new_ix are
offsets into the old and new bounding boxes and each pair indicates a
match between the bounding boxes at the indicated offsets.
"""
DISTANCE_THRESH = 200
BIG_DISTANCE = 1e6
BIG_DISTANCE_MINUS_EPSILON = 9e5
def center(box):
x1, y1, x2, y2 = tuple(box)
return [x1 + (x2 / 2), y1 + (y2 / 2)]
# O(n^2) comparison operation, so use numpy to scale n as far as we can
old_centers = np.array([center(b) for b in old_bounding_boxes])
new_centers = np.array([center(b) for b in new_bounding_boxes])
if old_centers.shape[0] == 0 or new_centers.shape[0] == 0:
# Special-case: One of input lists is empty
return []
# Generate table of euclidean distances, indexed by (old, new)
all_diffs = np.sqrt(np.sum(np.square(old_centers[:, None] - new_centers), axis=2))
# Replace everything over the threshold with a large number
all_diffs[all_diffs > DISTANCE_THRESH] = BIG_DISTANCE
# Pick the best match for each NEW bounding box.
matches = np.argmin(all_diffs, axis=0)
# Filter out the matches that didn't satisfy our threshold constraint
min_distances = np.min(all_diffs, axis=0)
matches[np.argwhere(min_distances > BIG_DISTANCE_MINUS_EPSILON)] = -1
results_as_dict = {}
for new_ix in range(matches.shape[0]):
old_ix = matches[new_ix]
if old_ix == -1:
pass
elif old_ix not in results_as_dict:
results_as_dict[old_ix] = new_ix
else:
# Break ties by distance
my_distance = all_diffs[old_ix, new_ix]
their_distance = all_diffs[old_ix, results_as_dict[old_ix]]
if my_distance < their_distance:
results_as_dict[old_ix] = new_ix
ret = [(k, v) for k, v in results_as_dict.items()]
return ret
################################################################################
# main function
if __name__ == "__main__":
socketio.run(app, host='0.0.0.0', port=7000)