victorisgeek commited on
Commit
6c49922
·
verified ·
1 Parent(s): d574e36

Upload 2 files

Browse files
dofaker/face_det/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ from .face_analysis import FaceAnalysis
dofaker/face_det/face_analysis.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
+ The following code references:: https://github.com/deepinsight/insightface
3
+ '''
4
+
5
+ import glob
6
+ import os.path as osp
7
+
8
+ import onnxruntime
9
+
10
+ from insightface import model_zoo
11
+ from insightface.utils import ensure_available
12
+ from insightface.app.common import Face
13
+
14
+ from dofaker.utils import download_file, get_model_url
15
+
16
+ __all__ = ['FaceAnalysis']
17
+
18
+
19
+ class FaceAnalysis:
20
+
21
+ def __init__(self,
22
+ name='buffalo_l',
23
+ root='weights',
24
+ allowed_modules=None,
25
+ **kwargs):
26
+ self.model_dir, _ = download_file(get_model_url(name),
27
+ save_dir=root,
28
+ overwrite=False)
29
+ onnxruntime.set_default_logger_severity(3)
30
+
31
+ self.models = {}
32
+ print(self.model_dir)
33
+ onnx_files = glob.glob(osp.join(self.model_dir, '*.onnx'))
34
+ onnx_files = sorted(onnx_files)
35
+ for onnx_file in onnx_files:
36
+ model = model_zoo.get_model(onnx_file, **kwargs)
37
+ if model is None:
38
+ print('model not recognized:', onnx_file)
39
+ elif allowed_modules is not None and model.taskname not in allowed_modules:
40
+ print('model ignore:', onnx_file, model.taskname)
41
+ del model
42
+ elif model.taskname not in self.models and (allowed_modules is None
43
+ or model.taskname
44
+ in allowed_modules):
45
+ print('find model:', onnx_file, model.taskname,
46
+ model.input_shape, model.input_mean, model.input_std)
47
+ self.models[model.taskname] = model
48
+ else:
49
+ print('duplicated model task type, ignore:', onnx_file,
50
+ model.taskname)
51
+ del model
52
+ assert 'detection' in self.models
53
+ self.det_model = self.models['detection']
54
+
55
+ def prepare(self, ctx_id, det_thresh=0.5, det_size=(640, 640)):
56
+ self.det_thresh = det_thresh
57
+ assert det_size is not None, "det_size can't be None."
58
+ self.det_size = det_size
59
+ for taskname, model in self.models.items():
60
+ if taskname == 'detection':
61
+ model.prepare(ctx_id,
62
+ input_size=det_size,
63
+ det_thresh=det_thresh)
64
+ else:
65
+ model.prepare(ctx_id)
66
+
67
+ def get(self, img, max_num=0):
68
+ bboxes, kpss = self.det_model.detect(img,
69
+ max_num=max_num,
70
+ metric='default')
71
+ if bboxes.shape[0] == 0:
72
+ return []
73
+ ret = []
74
+ for i in range(bboxes.shape[0]):
75
+ bbox = bboxes[i, 0:4]
76
+ det_score = bboxes[i, 4]
77
+ kps = None
78
+ if kpss is not None:
79
+ kps = kpss[i]
80
+ face = Face(bbox=bbox, kps=kps, det_score=det_score)
81
+ for taskname, model in self.models.items():
82
+ if taskname == 'detection':
83
+ continue
84
+ model.get(img, face)
85
+ ret.append(face)
86
+ return ret
87
+
88
+ def draw_on(self, img, faces):
89
+ import cv2
90
+ dimg = img.copy()
91
+ for i in range(len(faces)):
92
+ face = faces[i]
93
+ box = face.bbox.astype('int')
94
+ color = (0, 0, 255)
95
+ cv2.rectangle(dimg, (box[0], box[1]), (box[2], box[3]), color, 2)
96
+ if face.kps is not None:
97
+ kps = face.kps.astype('int')
98
+ for l in range(kps.shape[0]):
99
+ color = (0, 0, 255)
100
+ if l == 0 or l == 3:
101
+ color = (0, 255, 0)
102
+ cv2.circle(dimg, (kps[l][0], kps[l][1]), 1, color, 2)
103
+ if face.gender is not None and face.age is not None:
104
+ cv2.putText(dimg, '%s,%d' % (face.sex, face.age),
105
+ (box[0] - 1, box[1] - 4), cv2.FONT_HERSHEY_COMPLEX,
106
+ 0.7, (0, 255, 0), 1)
107
+ return dimg