JeVoisBase  1.20
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
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 
7 import sys
8 import argparse
9 
10 import numpy as np
11 import cv2 as cv
12 
13 from sface import SFace
14 
15 sys.path.append('../face_detection_yunet')
16 from yunet import YuNet
17 
18 def 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 
26 backends = [cv.dnn.DNN_BACKEND_OPENCV, cv.dnn.DNN_BACKEND_CUDA]
27 targets = [cv.dnn.DNN_TARGET_CPU, cv.dnn.DNN_TARGET_CUDA, cv.dnn.DNN_TARGET_CUDA_FP16]
28 help_msg_backends = "Choose one of the computation backends: {:d}: OpenCV implementation (default); {:d}: CUDA"
29 help_msg_targets = "Chose one of the target computation devices: {:d}: CPU (default); {:d}: CUDA; {:d}: CUDA fp16"
30 try:
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"
35 except:
36  print('This version of OpenCV does not support TIM-VX and NPU. Visit https://gist.github.com/fengyuentau/5a7a5ba36328f2b763aea026c43fa45f for more information.')
37 
38 parser = argparse.ArgumentParser(
39  description="SFace: Sigmoid-Constrained Hypersphere Loss for Robust Face Recognition (https://ieeexplore.ieee.org/document/9318547)")
40 parser.add_argument('--input1', '-i1', type=str, help='Path to the input image 1.')
41 parser.add_argument('--input2', '-i2', type=str, help='Path to the input image 2.')
42 parser.add_argument('--model', '-m', type=str, default='face_recognition_sface_2021dec.onnx', help='Path to the model.')
43 parser.add_argument('--backend', '-b', type=int, default=backends[0], help=help_msg_backends.format(*backends))
44 parser.add_argument('--target', '-t', type=int, default=targets[0], help=help_msg_targets.format(*targets))
45 parser.add_argument('--dis_type', type=int, choices=[0, 1], default=0, help='Distance type. \'0\': cosine, \'1\': norm_l1.')
46 parser.add_argument('--save', '-s', type=str, default=False, help='Set true to save results. This flag is invalid when using camera.')
47 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.')
48 args = parser.parse_args()
49 
50 if __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 
demo.str2bool
str2bool
Definition: demo.py:43
yunet.YuNet
Definition: yunet.py:12
sface.SFace
Definition: sface.py:12