13from sface
import SFace
15sys.path.append(
'../face_detection_yunet')
16from yunet
import YuNet
19 if v.lower()
in [
'on',
'yes',
'true',
'y',
't']:
21 elif v.lower()
in [
'off',
'no',
'false',
'n',
'f']:
24 raise NotImplementedError
26backends = [cv.dnn.DNN_BACKEND_OPENCV, cv.dnn.DNN_BACKEND_CUDA]
27targets = [cv.dnn.DNN_TARGET_CPU, cv.dnn.DNN_TARGET_CUDA, cv.dnn.DNN_TARGET_CUDA_FP16]
28help_msg_backends =
"Choose one of the computation backends: {:d}: OpenCV implementation (default); {:d}: CUDA"
29help_msg_targets =
"Chose one of the target computation devices: {:d}: CPU (default); {:d}: CUDA; {:d}: CUDA fp16"
31 backends += [cv.dnn.DNN_BACKEND_TIMVX]
32 targets += [cv.dnn.DNN_TARGET_NPU]
33 help_msg_backends +=
"; {:d}: TIMVX"
34 help_msg_targets +=
"; {:d}: NPU"
36 print(
'This version of OpenCV does not support TIM-VX and NPU. Visit https://gist.github.com/fengyuentau/5a7a5ba36328f2b763aea026c43fa45f for more information.')
38parser = argparse.ArgumentParser(
39 description=
"SFace: Sigmoid-Constrained Hypersphere Loss for Robust Face Recognition (https://ieeexplore.ieee.org/document/9318547)")
40parser.add_argument(
'--input1',
'-i1', type=str, help=
'Path to the input image 1.')
41parser.add_argument(
'--input2',
'-i2', type=str, help=
'Path to the input image 2.')
42parser.add_argument(
'--model',
'-m', type=str, default=
'face_recognition_sface_2021dec.onnx', help=
'Path to the model.')
43parser.add_argument(
'--backend',
'-b', type=int, default=backends[0], help=help_msg_backends.format(*backends))
44parser.add_argument(
'--target',
'-t', type=int, default=targets[0], help=help_msg_targets.format(*targets))
45parser.add_argument(
'--dis_type', type=int, choices=[0, 1], default=0, help=
'Distance type. \'0\': cosine, \'1\': norm_l1.')
46parser.add_argument(
'--save',
'-s', type=str, default=
False, help=
'Set true to save results. This flag is invalid when using camera.')
47parser.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.')
48args = parser.parse_args()
50if __name__ ==
'__main__':
52 recognizer =
SFace(modelPath=args.model, disType=args.dis_type, backendId=args.backend, targetId=args.target)
54 detector =
YuNet(modelPath=
'../face_detection_yunet/face_detection_yunet_2022mar.onnx',
59 backendId=args.backend,
62 img1 = cv.imread(args.input1)
63 img2 = cv.imread(args.input2)
66 detector.setInputSize([img1.shape[1], img1.shape[0]])
67 face1 = detector.infer(img1)
68 assert face1.shape[0] > 0,
'Cannot find a face in {}'.format(args.input1)
69 detector.setInputSize([img2.shape[1], img2.shape[0]])
70 face2 = detector.infer(img2)
71 assert face2.shape[0] > 0,
'Cannot find a face in {}'.format(args.input2)
74 result = recognizer.match(img1, face1[0][:-1], img2, face2[0][:-1])
75 print(
'Result: {}.'.format(
'same identity' if result
else 'different identities'))