JeVoisBase  1.22
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
demo.py
Go to the documentation of this file.
1# This file is part of OpenCV Zoo project.
2# It is subject to the license terms in the LICENSE file found in the same directory.
3#
4# Copyright (C) 2021, Shenzhen Institute of Artificial Intelligence and Robotics for Society, all rights reserved.
5# Third party copyrights are property of their respective owners.
6
7import sys
8import argparse
9
10import numpy as np
11import cv2 as cv
12
13from sface import SFace
14
15sys.path.append('../face_detection_yunet')
16from yunet import YuNet
17
18def str2bool(v):
19 if v.lower() in ['on', 'yes', 'true', 'y', 't']:
20 return True
21 elif v.lower() in ['off', 'no', 'false', 'n', 'f']:
22 return False
23 else:
24 raise NotImplementedError
25
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"
30try:
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"
35except:
36 print('This version of OpenCV does not support TIM-VX and NPU. Visit https://gist.github.com/fengyuentau/5a7a5ba36328f2b763aea026c43fa45f for more information.')
37
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()
49
50if __name__ == '__main__':
51 # Instantiate SFace for face recognition
52 recognizer = SFace(modelPath=args.model, disType=args.dis_type, backendId=args.backend, targetId=args.target)
53 # Instantiate YuNet for face detection
54 detector = YuNet(modelPath='../face_detection_yunet/face_detection_yunet_2022mar.onnx',
55 inputSize=[320, 320],
56 confThreshold=0.9,
57 nmsThreshold=0.3,
58 topK=5000,
59 backendId=args.backend,
60 targetId=args.target)
61
62 img1 = cv.imread(args.input1)
63 img2 = cv.imread(args.input2)
64
65 # Detect faces
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)
72
73 # Match
74 result = recognizer.match(img1, face1[0][:-1], img2, face2[0][:-1])
75 print('Result: {}.'.format('same identity' if result else 'different identities'))
76