Yuantao Feng
commited on
Commit
·
3af1dea
1
Parent(s):
9e6c549
Update to OpenCV APIs (YuNet -> FaceDetectorYN, SFace -> FaceRecognizerSF) (#6)
Browse files
README.md
CHANGED
@@ -29,10 +29,10 @@ Hardware Setup:
|
|
29 |
-->
|
30 |
| Model | Input Size | CPU x86_64 (ms) | CPU ARM (ms) |
|
31 |
|-------|------------|-----------------|--------------|
|
32 |
-
| [YuNet](./models/face_detection_yunet) | 160x120 |
|
33 |
| [DB](./models/text_detection_db) | 640x480 | 137.38 | 2780.78 |
|
34 |
| [CRNN](./models/text_recognition_crnn) | 100x32 | 50.21 | 234.32 |
|
35 |
-
| [SFace](./models/face_recognition_sface) | 112x112 | 8.
|
36 |
| [PP-ResNet](./models/image_classification_ppresnet) | 224x224 | 56.05 | 602.58
|
37 |
| [PP-HumanSeg](./models/human_segmentation_pphumanseg) | 192x192 | 19.92 | 105.32 |
|
38 |
|
|
|
29 |
-->
|
30 |
| Model | Input Size | CPU x86_64 (ms) | CPU ARM (ms) |
|
31 |
|-------|------------|-----------------|--------------|
|
32 |
+
| [YuNet](./models/face_detection_yunet) | 160x120 | 1.45 | 6.22 |
|
33 |
| [DB](./models/text_detection_db) | 640x480 | 137.38 | 2780.78 |
|
34 |
| [CRNN](./models/text_recognition_crnn) | 100x32 | 50.21 | 234.32 |
|
35 |
+
| [SFace](./models/face_recognition_sface) | 112x112 | 8.65 | 99.20 |
|
36 |
| [PP-ResNet](./models/image_classification_ppresnet) | 224x224 | 56.05 | 602.58
|
37 |
| [PP-HumanSeg](./models/human_segmentation_pphumanseg) | 192x192 | 19.92 | 105.32 |
|
38 |
|
benchmark/config/face_detection_yunet.yaml
CHANGED
@@ -19,5 +19,4 @@ Model:
|
|
19 |
modelPath: "models/face_detection_yunet/face_detection_yunet.onnx"
|
20 |
confThreshold: 0.6
|
21 |
nmsThreshold: 0.3
|
22 |
-
topK: 5000
|
23 |
-
keepTopK: 750
|
|
|
19 |
modelPath: "models/face_detection_yunet/face_detection_yunet.onnx"
|
20 |
confThreshold: 0.6
|
21 |
nmsThreshold: 0.3
|
22 |
+
topK: 5000
|
|
benchmark/requirements.txt
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
numpy==1.21.2
|
2 |
-
opencv-python==4.5.
|
3 |
tqdm
|
4 |
pyyaml
|
5 |
requests
|
|
|
1 |
numpy==1.21.2
|
2 |
+
opencv-python==4.5.4.58
|
3 |
tqdm
|
4 |
pyyaml
|
5 |
requests
|
models/face_detection_yunet/demo.py
CHANGED
@@ -25,7 +25,6 @@ parser.add_argument('--model', '-m', type=str, default='face_detection_yunet.onn
|
|
25 |
parser.add_argument('--conf_threshold', type=float, default=0.9, help='Filter out faces of confidence < conf_threshold.')
|
26 |
parser.add_argument('--nms_threshold', type=float, default=0.3, help='Suppress bounding boxes of iou >= nms_threshold.')
|
27 |
parser.add_argument('--top_k', type=int, default=5000, help='Keep top_k bounding boxes before NMS.')
|
28 |
-
parser.add_argument('--keep_top_k', type=int, default=750, help='Keep keep_top_k bounding boxes after NMS.')
|
29 |
parser.add_argument('--save', '-s', type=str, default=False, help='Set true to save results. This flag is invalid when using camera.')
|
30 |
parser.add_argument('--vis', '-v', type=str2bool, default=True, help='Set true to open a window for result visualization. This flag is invalid when using camera.')
|
31 |
args = parser.parse_args()
|
@@ -62,8 +61,7 @@ if __name__ == '__main__':
|
|
62 |
inputSize=[320, 320],
|
63 |
confThreshold=args.conf_threshold,
|
64 |
nmsThreshold=args.nms_threshold,
|
65 |
-
topK=args.top_k
|
66 |
-
keepTopK=args.keep_top_k)
|
67 |
|
68 |
# If input is an image
|
69 |
if args.input is not None:
|
|
|
25 |
parser.add_argument('--conf_threshold', type=float, default=0.9, help='Filter out faces of confidence < conf_threshold.')
|
26 |
parser.add_argument('--nms_threshold', type=float, default=0.3, help='Suppress bounding boxes of iou >= nms_threshold.')
|
27 |
parser.add_argument('--top_k', type=int, default=5000, help='Keep top_k bounding boxes before NMS.')
|
|
|
28 |
parser.add_argument('--save', '-s', type=str, default=False, help='Set true to save results. This flag is invalid when using camera.')
|
29 |
parser.add_argument('--vis', '-v', type=str2bool, default=True, help='Set true to open a window for result visualization. This flag is invalid when using camera.')
|
30 |
args = parser.parse_args()
|
|
|
61 |
inputSize=[320, 320],
|
62 |
confThreshold=args.conf_threshold,
|
63 |
nmsThreshold=args.nms_threshold,
|
64 |
+
topK=args.top_k)
|
|
|
65 |
|
66 |
# If input is an image
|
67 |
if args.input is not None:
|
models/face_detection_yunet/yunet.py
CHANGED
@@ -10,140 +10,57 @@ import numpy as np
|
|
10 |
import cv2 as cv
|
11 |
|
12 |
class YuNet:
|
13 |
-
def __init__(self, modelPath, inputSize=[320, 320], confThreshold=0.6, nmsThreshold=0.3, topK=5000,
|
14 |
self._modelPath = modelPath
|
15 |
-
self.
|
16 |
-
|
17 |
-
self._inputNames = ''
|
18 |
-
self._outputNames = ['loc', 'conf', 'iou']
|
19 |
-
self._inputSize = inputSize # [w, h]
|
20 |
self._confThreshold = confThreshold
|
21 |
self._nmsThreshold = nmsThreshold
|
22 |
self._topK = topK
|
23 |
-
self.
|
24 |
-
|
25 |
-
self._min_sizes = [[10, 16, 24], [32, 48], [64, 96], [128, 192, 256]]
|
26 |
-
self._steps = [8, 16, 32, 64]
|
27 |
-
self._variance = [0.1, 0.2]
|
28 |
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
@property
|
33 |
def name(self):
|
34 |
return self.__class__.__name__
|
35 |
|
36 |
-
def setBackend(self,
|
37 |
-
self.
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
def setInputSize(self, input_size):
|
43 |
-
self.
|
44 |
-
|
45 |
-
# Regenerate priors
|
46 |
-
self._priorGen()
|
47 |
-
|
48 |
-
def _preprocess(self, image):
|
49 |
-
return cv.dnn.blobFromImage(image)
|
50 |
|
51 |
def infer(self, image):
|
52 |
-
assert image.shape[0] == self._inputSize[1], '{} (height of input image) != {} (preset height)'.format(image.shape[0], self._inputSize[1])
|
53 |
-
assert image.shape[1] == self._inputSize[0], '{} (width of input image) != {} (preset width)'.format(image.shape[1], self._inputSize[0])
|
54 |
-
|
55 |
-
# Preprocess
|
56 |
-
inputBlob = self._preprocess(image)
|
57 |
-
|
58 |
# Forward
|
59 |
-
self._model.
|
60 |
-
|
61 |
-
|
62 |
-
# Postprocess
|
63 |
-
results = self._postprocess(outputBlob)
|
64 |
-
|
65 |
-
return results
|
66 |
-
|
67 |
-
def _postprocess(self, outputBlob):
|
68 |
-
# Decode
|
69 |
-
dets = self._decode(outputBlob)
|
70 |
-
|
71 |
-
# NMS
|
72 |
-
keepIdx = cv.dnn.NMSBoxes(
|
73 |
-
bboxes=dets[:, 0:4].tolist(),
|
74 |
-
scores=dets[:, -1].tolist(),
|
75 |
-
score_threshold=self._confThreshold,
|
76 |
-
nms_threshold=self._nmsThreshold,
|
77 |
-
top_k=self._topK
|
78 |
-
) # box_num x class_num
|
79 |
-
if len(keepIdx) > 0:
|
80 |
-
dets = dets[keepIdx]
|
81 |
-
dets = np.squeeze(dets, axis=1)
|
82 |
-
return dets[:self._keepTopK]
|
83 |
-
else:
|
84 |
-
return np.empty(shape=(0, 15))
|
85 |
-
|
86 |
-
def _priorGen(self):
|
87 |
-
w, h = self._inputSize
|
88 |
-
feature_map_2th = [int(int((h + 1) / 2) / 2),
|
89 |
-
int(int((w + 1) / 2) / 2)]
|
90 |
-
feature_map_3th = [int(feature_map_2th[0] / 2),
|
91 |
-
int(feature_map_2th[1] / 2)]
|
92 |
-
feature_map_4th = [int(feature_map_3th[0] / 2),
|
93 |
-
int(feature_map_3th[1] / 2)]
|
94 |
-
feature_map_5th = [int(feature_map_4th[0] / 2),
|
95 |
-
int(feature_map_4th[1] / 2)]
|
96 |
-
feature_map_6th = [int(feature_map_5th[0] / 2),
|
97 |
-
int(feature_map_5th[1] / 2)]
|
98 |
-
|
99 |
-
feature_maps = [feature_map_3th, feature_map_4th,
|
100 |
-
feature_map_5th, feature_map_6th]
|
101 |
-
|
102 |
-
priors = []
|
103 |
-
for k, f in enumerate(feature_maps):
|
104 |
-
min_sizes = self._min_sizes[k]
|
105 |
-
for i, j in product(range(f[0]), range(f[1])): # i->h, j->w
|
106 |
-
for min_size in min_sizes:
|
107 |
-
s_kx = min_size / w
|
108 |
-
s_ky = min_size / h
|
109 |
-
|
110 |
-
cx = (j + 0.5) * self._steps[k] / w
|
111 |
-
cy = (i + 0.5) * self._steps[k] / h
|
112 |
-
|
113 |
-
priors.append([cx, cy, s_kx, s_ky])
|
114 |
-
self.priors = np.array(priors, dtype=np.float32)
|
115 |
-
|
116 |
-
def _decode(self, outputBlob):
|
117 |
-
loc, conf, iou = outputBlob
|
118 |
-
# get score
|
119 |
-
cls_scores = conf[:, 1]
|
120 |
-
iou_scores = iou[:, 0]
|
121 |
-
# clamp
|
122 |
-
_idx = np.where(iou_scores < 0.)
|
123 |
-
iou_scores[_idx] = 0.
|
124 |
-
_idx = np.where(iou_scores > 1.)
|
125 |
-
iou_scores[_idx] = 1.
|
126 |
-
scores = np.sqrt(cls_scores * iou_scores)
|
127 |
-
scores = scores[:, np.newaxis]
|
128 |
-
|
129 |
-
scale = np.array(self._inputSize)
|
130 |
-
|
131 |
-
# get bboxes
|
132 |
-
bboxes = np.hstack((
|
133 |
-
(self.priors[:, 0:2] + loc[:, 0:2] * self._variance[0] * self.priors[:, 2:4]) * scale,
|
134 |
-
(self.priors[:, 2:4] * np.exp(loc[:, 2:4] * self._variance)) * scale
|
135 |
-
))
|
136 |
-
# (x_c, y_c, w, h) -> (x1, y1, w, h)
|
137 |
-
bboxes[:, 0:2] -= bboxes[:, 2:4] / 2
|
138 |
-
|
139 |
-
# get landmarks
|
140 |
-
landmarks = np.hstack((
|
141 |
-
(self.priors[:, 0:2] + loc[:, 4: 6] * self._variance[0] * self.priors[:, 2:4]) * scale,
|
142 |
-
(self.priors[:, 0:2] + loc[:, 6: 8] * self._variance[0] * self.priors[:, 2:4]) * scale,
|
143 |
-
(self.priors[:, 0:2] + loc[:, 8:10] * self._variance[0] * self.priors[:, 2:4]) * scale,
|
144 |
-
(self.priors[:, 0:2] + loc[:, 10:12] * self._variance[0] * self.priors[:, 2:4]) * scale,
|
145 |
-
(self.priors[:, 0:2] + loc[:, 12:14] * self._variance[0] * self.priors[:, 2:4]) * scale
|
146 |
-
))
|
147 |
-
|
148 |
-
dets = np.hstack((bboxes, landmarks, scores))
|
149 |
-
return dets
|
|
|
10 |
import cv2 as cv
|
11 |
|
12 |
class YuNet:
|
13 |
+
def __init__(self, modelPath, inputSize=[320, 320], confThreshold=0.6, nmsThreshold=0.3, topK=5000, backendId=0, targetId=0):
|
14 |
self._modelPath = modelPath
|
15 |
+
self._inputSize = tuple(inputSize) # [w, h]
|
|
|
|
|
|
|
|
|
16 |
self._confThreshold = confThreshold
|
17 |
self._nmsThreshold = nmsThreshold
|
18 |
self._topK = topK
|
19 |
+
self._backendId = backendId
|
20 |
+
self._targetId = targetId
|
|
|
|
|
|
|
21 |
|
22 |
+
self._model = cv.FaceDetectorYN.create(
|
23 |
+
model=self._modelPath,
|
24 |
+
config="",
|
25 |
+
input_size=self._inputSize,
|
26 |
+
score_threshold=self._confThreshold,
|
27 |
+
nms_threshold=self._nmsThreshold,
|
28 |
+
top_k=self._topK,
|
29 |
+
backend_id=self._backendId,
|
30 |
+
target_id=self._targetId)
|
31 |
|
32 |
@property
|
33 |
def name(self):
|
34 |
return self.__class__.__name__
|
35 |
|
36 |
+
def setBackend(self, backendId):
|
37 |
+
self._backendId = backendId
|
38 |
+
self._model = cv.FaceDetectorYN.create(
|
39 |
+
model=self._modelPath,
|
40 |
+
config="",
|
41 |
+
input_size=self._inputSize,
|
42 |
+
score_threshold=self._confThreshold,
|
43 |
+
nms_threshold=self._nmsThreshold,
|
44 |
+
top_k=self._topK,
|
45 |
+
backend_id=self._backendId,
|
46 |
+
target_id=self._targetId)
|
47 |
+
|
48 |
+
def setTarget(self, targetId):
|
49 |
+
self._targetId = targetId
|
50 |
+
self._model = cv.FaceDetectorYN.create(
|
51 |
+
model=self._modelPath,
|
52 |
+
config="",
|
53 |
+
input_size=self._inputSize,
|
54 |
+
score_threshold=self._confThreshold,
|
55 |
+
nms_threshold=self._nmsThreshold,
|
56 |
+
top_k=self._topK,
|
57 |
+
backend_id=self._backendId,
|
58 |
+
target_id=self._targetId)
|
59 |
|
60 |
def setInputSize(self, input_size):
|
61 |
+
self._model.setInputSize(tuple(input_size))
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
def infer(self, image):
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
# Forward
|
65 |
+
faces = self._model.detect(image)
|
66 |
+
return faces[1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
models/face_recognition_sface/demo.py
CHANGED
@@ -35,14 +35,13 @@ args = parser.parse_args()
|
|
35 |
|
36 |
if __name__ == '__main__':
|
37 |
# Instantiate SFace for face recognition
|
38 |
-
recognizer = SFace(modelPath=args.model)
|
39 |
# Instantiate YuNet for face detection
|
40 |
detector = YuNet(modelPath='../face_detection_yunet/face_detection_yunet.onnx',
|
41 |
inputSize=[320, 320],
|
42 |
confThreshold=0.9,
|
43 |
nmsThreshold=0.3,
|
44 |
-
topK=5000
|
45 |
-
keepTopK=750)
|
46 |
|
47 |
img1 = cv.imread(args.input1)
|
48 |
img2 = cv.imread(args.input2)
|
@@ -56,16 +55,5 @@ if __name__ == '__main__':
|
|
56 |
assert face2.shape[0] > 0, 'Cannot find a face in {}'.format(args.input2)
|
57 |
|
58 |
# Match
|
59 |
-
|
60 |
-
print(
|
61 |
-
if args.dis_type == 0:
|
62 |
-
dis_type = 'Cosine'
|
63 |
-
threshold = 0.363
|
64 |
-
result = 'same identity' if distance >= threshold else 'different identity'
|
65 |
-
elif args.dis_type == 1:
|
66 |
-
dis_type = 'Norm-L2'
|
67 |
-
threshold = 1.128
|
68 |
-
result = 'same identity' if distance <= threshold else 'different identity'
|
69 |
-
else:
|
70 |
-
raise NotImplementedError()
|
71 |
-
print('Using {} distance, threshold {}: {}.'.format(dis_type, threshold, result))
|
|
|
35 |
|
36 |
if __name__ == '__main__':
|
37 |
# Instantiate SFace for face recognition
|
38 |
+
recognizer = SFace(modelPath=args.model, disType=args.dis_type)
|
39 |
# Instantiate YuNet for face detection
|
40 |
detector = YuNet(modelPath='../face_detection_yunet/face_detection_yunet.onnx',
|
41 |
inputSize=[320, 320],
|
42 |
confThreshold=0.9,
|
43 |
nmsThreshold=0.3,
|
44 |
+
topK=5000)
|
|
|
45 |
|
46 |
img1 = cv.imread(args.input1)
|
47 |
img2 = cv.imread(args.input2)
|
|
|
55 |
assert face2.shape[0] > 0, 'Cannot find a face in {}'.format(args.input2)
|
56 |
|
57 |
# Match
|
58 |
+
result = recognizer.match(img1, face1[0][:-1], img2, face2[0][:-1])
|
59 |
+
print('Result: {}.'.format('same identity' if result else 'different identities'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
models/face_recognition_sface/sface.py
CHANGED
@@ -10,156 +10,60 @@ import cv2 as cv
|
|
10 |
from _testcapi import FLT_MIN
|
11 |
|
12 |
class SFace:
|
13 |
-
def __init__(self, modelPath):
|
14 |
-
self.
|
15 |
-
self.
|
16 |
-
self.
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
self.
|
|
|
|
|
|
|
|
|
24 |
|
25 |
@property
|
26 |
def name(self):
|
27 |
return self.__class__.__name__
|
28 |
|
29 |
-
def setBackend(self,
|
30 |
-
self.
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
def _preprocess(self, image, bbox):
|
36 |
-
|
37 |
-
return cv.dnn.blobFromImage(aligned_image)
|
38 |
|
39 |
def infer(self, image, bbox):
|
40 |
# Preprocess
|
41 |
inputBlob = self._preprocess(image, bbox)
|
42 |
|
43 |
# Forward
|
44 |
-
self._model.
|
45 |
-
|
46 |
-
|
47 |
-
# Postprocess
|
48 |
-
results = self._postprocess(outputBlob)
|
49 |
-
|
50 |
-
return results
|
51 |
|
52 |
-
def
|
53 |
-
return outputBlob / cv.norm(outputBlob)
|
54 |
-
|
55 |
-
def match(self, image1, face1, image2, face2, dis_type=0):
|
56 |
feature1 = self.infer(image1, face1)
|
57 |
feature2 = self.infer(image2, face2)
|
58 |
|
59 |
-
if
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
def _alignCrop(self, image, face):
|
67 |
-
# Retrieve landmarks
|
68 |
-
if face.shape[-1] == (4 + 5 * 2):
|
69 |
-
landmarks = face[4:].reshape(5, 2)
|
70 |
-
else:
|
71 |
-
raise NotImplementedError()
|
72 |
-
warp_mat = self._getSimilarityTransformMatrix(landmarks)
|
73 |
-
aligned_image = cv.warpAffine(image, warp_mat, self._input_size, flags=cv.INTER_LINEAR)
|
74 |
-
return aligned_image
|
75 |
-
|
76 |
-
def _getSimilarityTransformMatrix(self, src):
|
77 |
-
# compute the mean of src and dst
|
78 |
-
src_mean = np.array([np.mean(src[:, 0]), np.mean(src[:, 1])], dtype=np.float32)
|
79 |
-
dst_mean = np.array([56.0262, 71.9008], dtype=np.float32)
|
80 |
-
# subtract the means from src and dst
|
81 |
-
src_demean = src.copy()
|
82 |
-
src_demean[:, 0] = src_demean[:, 0] - src_mean[0]
|
83 |
-
src_demean[:, 1] = src_demean[:, 1] - src_mean[1]
|
84 |
-
dst_demean = self._dst.copy()
|
85 |
-
dst_demean[:, 0] = dst_demean[:, 0] - dst_mean[0]
|
86 |
-
dst_demean[:, 1] = dst_demean[:, 1] - dst_mean[1]
|
87 |
-
|
88 |
-
A = np.array([[0., 0.], [0., 0.]], dtype=np.float64)
|
89 |
-
for i in range(5):
|
90 |
-
A[0][0] += dst_demean[i][0] * src_demean[i][0]
|
91 |
-
A[0][1] += dst_demean[i][0] * src_demean[i][1]
|
92 |
-
A[1][0] += dst_demean[i][1] * src_demean[i][0]
|
93 |
-
A[1][1] += dst_demean[i][1] * src_demean[i][1]
|
94 |
-
A = A / 5
|
95 |
-
|
96 |
-
d = np.array([1.0, 1.0], dtype=np.float64)
|
97 |
-
if A[0][0] * A[1][1] - A[0][1] * A[1][0] < 0:
|
98 |
-
d[1] = -1
|
99 |
-
|
100 |
-
T = np.array([
|
101 |
-
[1.0, 0.0, 0.0],
|
102 |
-
[0.0, 1.0, 0.0],
|
103 |
-
[0.0, 0.0, 1.0]
|
104 |
-
], dtype=np.float64)
|
105 |
-
|
106 |
-
s, u, vt = cv.SVDecomp(A)
|
107 |
-
smax = s[0][0] if s[0][0] > s[1][0] else s[1][0]
|
108 |
-
tol = smax * 2 * FLT_MIN
|
109 |
-
rank = int(0)
|
110 |
-
if s[0][0] > tol:
|
111 |
-
rank += 1
|
112 |
-
if s[1][0] > tol:
|
113 |
-
rank += 1
|
114 |
-
det_u = u[0][0] * u[1][1] - u[0][1] * u[1][0]
|
115 |
-
det_vt = vt[0][0] * vt[1][1] - vt[0][1] * vt[1][0]
|
116 |
-
if rank == 1:
|
117 |
-
if det_u * det_vt > 0:
|
118 |
-
uvt = np.matmul(u, vt)
|
119 |
-
T[0][0] = uvt[0][0]
|
120 |
-
T[0][1] = uvt[0][1]
|
121 |
-
T[1][0] = uvt[1][0]
|
122 |
-
T[1][1] = uvt[1][1]
|
123 |
-
else:
|
124 |
-
temp = d[1]
|
125 |
-
d[1] = -1
|
126 |
-
D = np.array([[d[0], 0.0], [0.0, d[1]]], dtype=np.float64)
|
127 |
-
Dvt = np.matmul(D, vt)
|
128 |
-
uDvt = np.matmul(u, Dvt)
|
129 |
-
T[0][0] = uDvt[0][0]
|
130 |
-
T[0][1] = uDvt[0][1]
|
131 |
-
T[1][0] = uDvt[1][0]
|
132 |
-
T[1][1] = uDvt[1][1]
|
133 |
-
d[1] = temp
|
134 |
-
else:
|
135 |
-
D = np.array([[d[0], 0.0], [0.0, d[1]]], dtype=np.float64)
|
136 |
-
Dvt = np.matmul(D, vt)
|
137 |
-
uDvt = np.matmul(u, Dvt)
|
138 |
-
T[0][0] = uDvt[0][0]
|
139 |
-
T[0][1] = uDvt[0][1]
|
140 |
-
T[1][0] = uDvt[1][0]
|
141 |
-
T[1][1] = uDvt[1][1]
|
142 |
-
|
143 |
-
var1 = 0.0
|
144 |
-
var2 = 0.0
|
145 |
-
for i in range(5):
|
146 |
-
var1 += src_demean[i][0] * src_demean[i][0]
|
147 |
-
var2 += src_demean[i][1] * src_demean[i][1]
|
148 |
-
var1 /= 5
|
149 |
-
var2 /= 5
|
150 |
-
|
151 |
-
scale = 1.0 / (var1 + var2) * (s[0][0] * d[0] + s[1][0] * d[1])
|
152 |
-
TS = [
|
153 |
-
T[0][0] * src_mean[0] + T[0][1] * src_mean[1],
|
154 |
-
T[1][0] * src_mean[0] + T[1][1] * src_mean[1]
|
155 |
-
]
|
156 |
-
T[0][2] = dst_mean[0] - scale * TS[0]
|
157 |
-
T[1][2] = dst_mean[1] - scale * TS[1]
|
158 |
-
T[0][0] *= scale
|
159 |
-
T[0][1] *= scale
|
160 |
-
T[1][0] *= scale
|
161 |
-
T[1][1] *= scale
|
162 |
-
return np.array([
|
163 |
-
[T[0][0], T[0][1], T[0][2]],
|
164 |
-
[T[1][0], T[1][1], T[1][2]]
|
165 |
-
], dtype=np.float64)
|
|
|
10 |
from _testcapi import FLT_MIN
|
11 |
|
12 |
class SFace:
|
13 |
+
def __init__(self, modelPath, disType=0, backendId=0, targetId=0):
|
14 |
+
self._modelPath = modelPath
|
15 |
+
self._backendId = backendId
|
16 |
+
self._targetId = targetId
|
17 |
+
self._model = cv.FaceRecognizerSF.create(
|
18 |
+
model=self._modelPath,
|
19 |
+
config="",
|
20 |
+
backend_id=self._backendId,
|
21 |
+
target_id=self._targetId)
|
22 |
+
|
23 |
+
self._disType = disType # 0: cosine similarity, 1: Norm-L2 distance
|
24 |
+
assert self._disType in [0, 1], "0: Cosine similarity, 1: norm-L2 distance, others: invalid"
|
25 |
+
|
26 |
+
self._threshold_cosine = 0.363
|
27 |
+
self._threshold_norml2 = 1.128
|
28 |
|
29 |
@property
|
30 |
def name(self):
|
31 |
return self.__class__.__name__
|
32 |
|
33 |
+
def setBackend(self, backendId):
|
34 |
+
self._backendId = backendId
|
35 |
+
self._model = cv.FaceRecognizerSF.create(
|
36 |
+
model=self._modelPath,
|
37 |
+
config="",
|
38 |
+
backend_id=self._backendId,
|
39 |
+
target_id=self._targetId)
|
40 |
+
|
41 |
+
def setTarget(self, targetId):
|
42 |
+
self._targetId = targetId
|
43 |
+
self._model = cv.FaceRecognizerSF.create(
|
44 |
+
model=self._modelPath,
|
45 |
+
config="",
|
46 |
+
backend_id=self._backendId,
|
47 |
+
target_id=self._targetId)
|
48 |
|
49 |
def _preprocess(self, image, bbox):
|
50 |
+
return self._model.alignCrop(image, bbox)
|
|
|
51 |
|
52 |
def infer(self, image, bbox):
|
53 |
# Preprocess
|
54 |
inputBlob = self._preprocess(image, bbox)
|
55 |
|
56 |
# Forward
|
57 |
+
features = self._model.feature(inputBlob)
|
58 |
+
return features
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
def match(self, image1, face1, image2, face2):
|
|
|
|
|
|
|
61 |
feature1 = self.infer(image1, face1)
|
62 |
feature2 = self.infer(image2, face2)
|
63 |
|
64 |
+
if self._disType == 0: # COSINE
|
65 |
+
cosine_score = self._model.match(feature1, feature2, self._disType)
|
66 |
+
return 1 if cosine_score >= self._threshold_cosine else 0
|
67 |
+
else: # NORM_L2
|
68 |
+
norml2_distance = self._model.match(feature1, feature2, self._disType)
|
69 |
+
return 1 if norml2_distance <= self._threshold_norml2 else 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|