ONNX
ytfeng commited on
Commit
a02db34
·
1 Parent(s): 9177150

Add options for demo scripts to select backend & targets (#43)

Browse files

* add options for selecting backend & targets

* add eol

Files changed (1) hide show
  1. demo.py +20 -3
demo.py CHANGED
@@ -23,11 +23,25 @@ def str2bool(v):
23
  else:
24
  raise NotImplementedError
25
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  parser = argparse.ArgumentParser(
27
  description="SFace: Sigmoid-Constrained Hypersphere Loss for Robust Face Recognition (https://ieeexplore.ieee.org/document/9318547)")
28
  parser.add_argument('--input1', '-i1', type=str, help='Path to the input image 1.')
29
  parser.add_argument('--input2', '-i2', type=str, help='Path to the input image 2.')
30
  parser.add_argument('--model', '-m', type=str, default='face_recognition_sface_2021dec.onnx', help='Path to the model.')
 
 
31
  parser.add_argument('--dis_type', type=int, choices=[0, 1], default=0, help='Distance type. \'0\': cosine, \'1\': norm_l1.')
32
  parser.add_argument('--save', '-s', type=str, default=False, help='Set true to save results. This flag is invalid when using camera.')
33
  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.')
@@ -35,13 +49,15 @@ args = parser.parse_args()
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_2021dec.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)
@@ -56,4 +72,5 @@ if __name__ == '__main__':
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'))
 
 
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.')
 
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_2021dec.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)
 
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
+