-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deepface.py
281 lines (201 loc) · 7.52 KB
/
Deepface.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
import argparse
import os
import sys
import time
import uuid
from deepface import DeepFace
from flask import Flask, request, jsonify
from retinaface import RetinaFace
from retinaface.RetinaFace import build_model
from yolov7 import YOLOv7, utils
app = Flask(__name__)
# ------------------------------
# Service API Interface
@app.route('/')
def index():
return '<h1>Hello, deepface!</h1>'
@app.route('/analyze', methods=['POST'])
def analyze():
tic = time.time()
req = request.get_json()
trx_id = uuid.uuid4()
# ---------------------------
resp_obj = analyzeWrapper(req)
# ---------------------------
toc = time.time()
resp_obj["trx_id"] = trx_id
resp_obj["seconds"] = toc - tic
return resp_obj
def analyzeWrapper(req):
instances = []
if "img" in list(req.keys()):
raw_content = req["img"] # list
for item in raw_content: # item is in type of dict
instances.append(item)
if len(instances) == 0:
return jsonify({'success': False, 'error': 'you must pass at least one img object in your request'}), 205
print("Analyzing ", len(instances), " instances")
actions = ['emotion', 'age', 'gender', 'race']
if "actions" in list(req.keys()):
actions = req["actions"]
results = DeepFace.analyze(instances[0], actions=actions, enforce_detection=False,
detector_backend="retinaface")
i = 1
resp_obj = {}
for result in results:
resp_obj["instance_" + str(i)] = result
i = i + 1
return resp_obj
@app.route('/verify', methods=['POST'])
def verify():
tic = time.time()
req = request.get_json()
trx_id = uuid.uuid4()
resp_obj = jsonify({'success': False})
resp_obj = verifyWrapper(req, trx_id)
# --------------------------
toc = time.time()
resp_obj["trx_id"] = trx_id
resp_obj["seconds"] = toc - tic
return resp_obj, 200
def verifyWrapper(req, trx_id=0):
resp_obj = jsonify({'success': False})
model_name = "VGG-Face";
distance_metric = "cosine";
detector_backend = "opencv"
if "model_name" in list(req.keys()):
model_name = req["model_name"]
if "distance_metric" in list(req.keys()):
distance_metric = req["distance_metric"]
if "detector_backend" in list(req.keys()):
detector_backend = req["detector_backend"]
# ----------------------
instances = []
if "img" in list(req.keys()):
raw_content = req["img"] # list
for item in raw_content: # item is in type of dict
instance = []
img1 = item["img1"];
img2 = item["img2"]
validate_img1 = False
if len(img1) > 11 and img1[0:11] == "data:image/":
validate_img1 = True
validate_img2 = False
if len(img2) > 11 and img2[0:11] == "data:image/":
validate_img2 = True
if validate_img1 != True or validate_img2 != True:
return jsonify(
{'success': False, 'error': 'you must pass both img1 and img2 as base64 encoded string'}), 205
instance.append(img1);
instance.append(img2)
instances.append(instance)
# --------------------------
if len(instances) == 0:
return jsonify({'success': False, 'error': 'you must pass at least one img object in your request'}), 205
print("Input request of ", trx_id, " has ", len(instances), " pairs to verify")
# --------------------------
try:
resp_obj = DeepFace.verify(instances
, model_name=model_name
, distance_metric=distance_metric
, detector_backend=detector_backend
)
if model_name == "Ensemble": # issue 198.
for key in resp_obj: # issue 198.
resp_obj[key]['verified'] = bool(resp_obj[key]['verified'])
except Exception as err:
resp_obj = jsonify({'success': False, 'error': str(err)}), 205
return resp_obj
@app.route('/represent', methods=['POST'])
def represent():
tic = time.time()
req = request.get_json()
trx_id = uuid.uuid4()
resp_obj = jsonify({'success': False})
resp_obj = representWrapper(req, trx_id)
# --------------------------
toc = time.time()
resp_obj["trx_id"] = trx_id
resp_obj["seconds"] = toc - tic
return resp_obj, 200
@app.route('/persons', methods=["POST"])
def persons():
if request.method != "POST":
return
instances = request.get_json()['img']
results = []
if instances and len(instances) > 0:
for instance in instances:
boxes, scores, class_ids = yolov7_detector(instance)
output = utils.format_output(boxes, scores, class_ids)
count_persons = sum(1 for item in output if item.get('name') == 'person')
results.append(count_persons)
return results
@app.route('/objects', methods=["POST"])
def objects():
if request.method != "POST":
return
instances = request.get_json()['img']
results = []
if instances and len(instances) > 0:
for instance in instances:
boxes, scores, class_ids = yolov7_detector(instance)
results.append(utils.format_output(boxes, scores, class_ids))
return results
def representWrapper(req, trx_id=0):
resp_obj = jsonify({'success': False})
# -------------------------------------
# find out model
model_name = "VGG-Face";
distance_metric = "cosine";
detector_backend = 'opencv'
if "model_name" in list(req.keys()):
model_name = req["model_name"]
if "detector_backend" in list(req.keys()):
detector_backend = req["detector_backend"]
# -------------------------------------
# retrieve images from request
img = ""
if "img" in list(req.keys()):
img = req["img"] # list
# print("img: ", img)
validate_img = False
if len(img) > 11 and img[0:11] == "data:image/":
validate_img = True
if validate_img != True:
print("invalid image passed!")
return jsonify({'success': False, 'error': 'you must pass img as base64 encoded string'}), 205
# -------------------------------------
# call represent function from the interface
try:
embedding = DeepFace.represent(img
, model_name=model_name
, detector_backend=detector_backend
)
except Exception as err:
print("Exception: ", str(err))
resp_obj = jsonify({'success': False, 'error': str(err)}), 205
# -------------------------------------
# print("embedding is ", len(embedding)," dimensional vector")
resp_obj = {}
resp_obj["embedding"] = embedding
# -------------------------------------
return resp_obj
if __name__ == "__main__":
print("人脸识别v2.0.0")
script_path = os.path.abspath(sys.argv[0])
script_folder = os.path.dirname(script_path)
DeepFace.build_model("Race")
DeepFace.build_model("Age")
DeepFace.build_model("Gender")
DeepFace.build_model("Emotion")
build_model()
yolov7_detector = YOLOv7(os.path.join(script_folder, "yolov5s.onnx"), conf_thres=0.2, iou_thres=0.3)
parser = argparse.ArgumentParser()
parser.add_argument(
'-p', '--port',
type=int,
default=1234,
help='Port of serving api')
args = parser.parse_args()
app.run(host='0.0.0.0', port=args.port)