-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
420 lines (350 loc) · 14 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
import datetime
import hashlib
import json
import platform
from pathlib import Path
from tempfile import NamedTemporaryFile
import cv2
import dlib
import numpy as np
import rel
import websocket
from flask import Flask, abort, json, request
from omegaconf import OmegaConf
from tensorflow.keras.optimizers import SGD, Adam
from tensorflow.keras.utils import get_file
from factory import get_model
app = Flask(__name__)
pretrained_model = "https://github.com/yu4u/age-gender-estimation/releases/download/v0.6/EfficientNetB3_224_weights.11-3.44.hdf5"
modhash = "6d7f7b7ced093a8b3ef6399163da6ece"
margin = 0.4
def on_error(ws, error):
print(error)
def on_close(ws, close_status_code, close_msg):
print("### Connection closed ###")
def on_open(ws):
print("### Connection established ###")
def get_data():
analyzer_id = platform.node()
print(analyzer_id)
return analyzer_id
def load_optimizer(cfg):
if cfg.train.optimizer_name == "sgd":
return SGD(lr=cfg.train.lr, momentum=0.9, nesterov=True)
elif cfg.train.optimizer_name == "adam":
return Adam(lr=cfg.train.lr)
else:
raise ValueError("optimizer name should be 'sgd' or 'adam'")
def loaded_model():
weight_file = get_file(
"EfficientNetB3_224_weights.11-3.44.hdf5",
pretrained_model,
cache_subdir="pretrained_models",
file_hash=modhash,
cache_dir=str(Path(__file__).resolve().parent),
)
# for face detection
detector = dlib.get_frontal_face_detector()
# load model and weights
model_name, img_size = Path(weight_file).stem.split("_")[:2]
img_size = int(img_size)
cfg = OmegaConf.from_dotlist(
[f"model.model_name={model_name}", f"model.img_size={img_size}"]
)
model = get_model(cfg)
return model, detector, img_size, weight_file
def iterate(items_list):
types = []
for item in items_list:
if type(item) is int:
item_type = "Integer"
elif type(item) is float:
item_type = "Float"
elif type(item) is str:
item_type = "String"
else:
item_type = "Other"
types.append(item_type)
return types
@app.route(
"/cam_object/<cam_link>/<Privacy_Parameter>/<requestor_id>/<requestor_type>/<request_id>"
)
def cam_object_recognition(
cam_link, Privacy_Parameter, requestor_id, requestor_type, request_id
):
analyzer_id = get_data()
# Get current date and time
now = datetime.datetime.now()
# Generate a random hash using SHA-256 algorithm
hash_object = hashlib.sha256()
hash_object.update(bytes(str(now), "utf-8"))
hash_value = hash_object.hexdigest()
# Concatenate the time and the hash
analysis_id = str(analyzer_id) + str(now) + hash_value
# for age recognition
cap = cv2.VideoCapture(cam_link)
frame_id = 0
model, detector, img_size, weight_file = loaded_model()
model.load_weights(weight_file)
predicted_ages3 = []
while True:
# Read a frame from the video
ret, img = cap.read()
if not ret:
break
img = np.asarray(img)
frame_id += 1
# print("frame_id: ", frame_id)
input_img = cv2.cvtColor(
cv2.GaussianBlur(img, (Privacy_Parameter, Privacy_Parameter), 0),
cv2.COLOR_BGR2RGB,
)
private_img = cv2.cvtColor(
cv2.GaussianBlur(img, (Privacy_Parameter, Privacy_Parameter), 0),
cv2.COLOR_BGR2RGB,
)
# print(private_img.shape)
img_h, img_w, _ = np.shape(input_img)
# detect faces using dlib detector
detected = detector(input_img, 1)
faces = np.empty((len(detected), img_size, img_size, 3))
if len(detected) > 0:
for i, d in enumerate(detected):
x1, y1, x2, y2, w, h = (
d.left(),
d.top(),
d.right() + 1,
d.bottom() + 1,
d.width(),
d.height(),
)
xw1 = max(int(x1 - margin * w), 0)
yw1 = max(int(y1 - margin * h), 0)
xw2 = min(int(x2 + margin * w), img_w - 1)
yw2 = min(int(y2 + margin * h), img_h - 1)
cv2.rectangle(private_img, (x1, y1), (x2, y2), (255, 0, 0), 2)
# cv2.rectangle(img, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2)
faces[i] = cv2.resize(
private_img[yw1 : yw2 + 1, xw1 : xw2 + 1],
(img_size, img_size),
)
# predict ages and genders of the detected faces
results = model.predict(faces)
predicted_genders = results[0]
ages = np.arange(0, 101).reshape(101, 1)
predicted_ages = results[1].dot(ages).flatten()
predicted_ages2 = []
for apparent_age in predicted_ages:
if int(apparent_age) < 3:
age_text = "Toddler"
elif int(apparent_age) >= 3 and int(apparent_age) <= 12:
age_text = "Child"
elif int(apparent_age) >= 13 and int(apparent_age) <= 19:
age_text = "Teen"
elif int(apparent_age) >= 20 and int(apparent_age) <= 60:
age_text = "Adult"
elif int(apparent_age) > 60:
age_text = "Senior"
predicted_ages2.append(age_text)
# print(predicted_ages2)
predicted_ages3.append(predicted_ages2)
# print(len(predicted_ages3))
ws_req = {
"RequestPostTopicUUID": {
"topic_name": "SIFIS:Privacy_Aware_Parental_Control_Frame_Results",
"topic_uuid": "Parental_Control_Frame_Results",
"value": {
"description": "Parental Control Frame Results",
"requestor_id": str(requestor_id),
"requestor_type": str(requestor_type),
"request_id": str(request_id),
"analyzer_id": str(analyzer_id),
"analysis_id": str(analysis_id),
"Type": "CAM",
"file_name": "Empty",
"Privacy_Parameter": int(Privacy_Parameter),
"Frame": int(frame_id),
"Ages": predicted_ages3,
"length": int(len(predicted_ages3)),
},
}
}
ws.send(json.dumps(ws_req))
ws_req_final = {
"RequestPostTopicUUID": {
"topic_name": "SIFIS:Privacy_Aware_Parental_Control_Results",
"topic_uuid": "Parental_Control_Results",
"value": {
"description": "Parental Control Results",
"requestor_id": str(requestor_id),
"requestor_type": str(requestor_type),
"request_id": str(request_id),
"analyzer_id": str(analyzer_id),
"analysis_id": str(analysis_id),
"Type": "CAM",
"file_name": "Empty",
"Privacy_Parameter": int(Privacy_Parameter),
"Frames Count": int(frame_id),
"Ages": predicted_ages3,
"length": int(len(predicted_ages3)),
},
}
}
ws.send(json.dumps(ws_req_final))
return ws_req_final
@app.route(
"/file_estimation/<file_name>/<Privacy_Parameter>/<requestor_id>/<requestor_type>/<request_id>",
methods=["POST"],
)
def handler(
file_name, Privacy_Parameter, requestor_id, requestor_type, request_id
):
analyzer_id = get_data()
# Get current date and time
now = datetime.datetime.now()
# Generate a random hash using SHA-256 algorithm
hash_object = hashlib.sha256()
hash_object.update(bytes(str(now), "utf-8"))
hash_value = hash_object.hexdigest()
# Concatenate the time and the hash
analysis_id = str(analyzer_id) + str(now) + hash_value
Privacy_Parameter = int(Privacy_Parameter)
if not request.files:
# If the user didn't submit any files, return a 400 (Bad Request) error.
abort(400)
# Loop over every file that the user submitted.
for filename, handle in request.files.items():
# Create a temporary file.
# The location of the temporary file is available in `temp.name`.
temp = NamedTemporaryFile()
# Write the user's uploaded file to the temporary file.
# The file will get deleted when it drops out of scope.
handle.save(temp)
video_link = temp.name
cap = cv2.VideoCapture(video_link)
frame_id = 0
model, detector, img_size, weight_file = loaded_model()
model.load_weights(weight_file)
predicted_ages3 = []
while True:
# Read a frame from the video
ret, img = cap.read()
if not ret:
break
img = np.asarray(img)
frame_id += 1
# print("frame_id: ", frame_id)
input_img = cv2.cvtColor(
cv2.GaussianBlur(
img, (Privacy_Parameter, Privacy_Parameter), 0
),
cv2.COLOR_BGR2RGB,
)
private_img = cv2.cvtColor(
cv2.GaussianBlur(
img, (Privacy_Parameter, Privacy_Parameter), 0
),
cv2.COLOR_BGR2RGB,
)
# print(private_img.shape)
img_h, img_w, _ = np.shape(input_img)
# detect faces using dlib detector
detected = detector(input_img, 1)
faces = np.empty((len(detected), img_size, img_size, 3))
predicted_ages2 = []
if len(detected) > 0:
for i, d in enumerate(detected):
x1, y1, x2, y2, w, h = (
d.left(),
d.top(),
d.right() + 1,
d.bottom() + 1,
d.width(),
d.height(),
)
xw1 = max(int(x1 - margin * w), 0)
yw1 = max(int(y1 - margin * h), 0)
xw2 = min(int(x2 + margin * w), img_w - 1)
yw2 = min(int(y2 + margin * h), img_h - 1)
cv2.rectangle(
private_img, (x1, y1), (x2, y2), (255, 0, 0), 2
)
# cv2.rectangle(img, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2)
faces[i] = cv2.resize(
private_img[yw1 : yw2 + 1, xw1 : xw2 + 1],
(img_size, img_size),
)
# predict ages and genders of the detected faces
results = model.predict(faces)
predicted_genders = results[0]
ages = np.arange(0, 101).reshape(101, 1)
predicted_ages = results[1].dot(ages).flatten()
predicted_ages2 = []
for apparent_age in predicted_ages:
if int(apparent_age) < 3:
age_text = "Toddler"
elif int(apparent_age) >= 3 and int(apparent_age) <= 12:
age_text = "Child"
elif int(apparent_age) >= 13 and int(apparent_age) <= 19:
age_text = "Teen"
elif int(apparent_age) >= 20 and int(apparent_age) <= 60:
age_text = "Adult"
elif int(apparent_age) > 60:
age_text = "Senior"
predicted_ages2.append(age_text)
# print(predicted_ages2)
predicted_ages3.append(predicted_ages2)
# print(len(predicted_ages3))
ws_req = {
"RequestPostTopicUUID": {
"topic_name": "SIFIS:Privacy_Aware_Parental_Control_Frame_Results",
"topic_uuid": "Parental_Control_Frame_Results",
"value": {
"description": "Parental Control Frame Results",
"requestor_id": str(requestor_id),
"requestor_type": str(requestor_type),
"request_id": str(request_id),
"analyzer_id": str(analyzer_id),
"analysis_id": str(analysis_id),
"Type": "File",
"file_name": str(file_name),
"Privacy_Parameter": int(Privacy_Parameter),
"Frame": int(frame_id),
"Ages": predicted_ages2,
"length": int(len(predicted_ages2)),
},
}
}
ws.send(json.dumps(ws_req))
ws_req_final = {
"RequestPostTopicUUID": {
"topic_name": "SIFIS:Privacy_Aware_Parental_Control_Results",
"topic_uuid": "Parental_Control_Results",
"value": {
"description": "Parental Control Results",
"requestor_id": str(requestor_id),
"requestor_type": str(requestor_type),
"request_id": str(request_id),
"analyzer_id": str(analyzer_id),
"analysis_id": str(analysis_id),
"Type": "File",
"file_name": str(file_name),
"Privacy_Parameter": int(Privacy_Parameter),
"Frames Count": int(frame_id),
"Ages": predicted_ages3,
"length": int(len(predicted_ages3)),
},
}
}
ws.send(json.dumps(ws_req_final))
return ws_req_final
if __name__ == "__main__":
ws = websocket.WebSocketApp(
"ws://localhost:3000/ws",
on_open=on_open,
on_error=on_error,
on_close=on_close,
)
ws.run_forever(dispatcher=rel) # Set dispatcher to automatic reconnection
rel.signal(2, rel.abort) # Keyboard Interrupt
app.run(host="0.0.0.0", port=6060)