|
11 | 11 |
|
12 | 12 | from yunet import YuNet |
13 | 13 |
|
14 | | -def str2bool(v): |
15 | | - if v.lower() in ['on', 'yes', 'true', 'y', 't']: |
16 | | - return True |
17 | | - elif v.lower() in ['off', 'no', 'false', 'n', 'f']: |
18 | | - return False |
19 | | - else: |
20 | | - raise NotImplementedError |
21 | | - |
22 | | -backends = [cv.dnn.DNN_BACKEND_OPENCV, cv.dnn.DNN_BACKEND_CUDA] |
23 | | -targets = [cv.dnn.DNN_TARGET_CPU, cv.dnn.DNN_TARGET_CUDA, cv.dnn.DNN_TARGET_CUDA_FP16] |
24 | | -help_msg_backends = "Choose one of the computation backends: {:d}: OpenCV implementation (default); {:d}: CUDA" |
25 | | -help_msg_targets = "Choose one of the target computation devices: {:d}: CPU (default); {:d}: CUDA; {:d}: CUDA fp16" |
26 | | -try: |
27 | | - backends += [cv.dnn.DNN_BACKEND_TIMVX] |
28 | | - targets += [cv.dnn.DNN_TARGET_NPU] |
29 | | - help_msg_backends += "; {:d}: TIMVX" |
30 | | - help_msg_targets += "; {:d}: NPU" |
31 | | -except: |
32 | | - print('This version of OpenCV does not support TIM-VX and NPU. Visit https://github.com/opencv/opencv/wiki/TIM-VX-Backend-For-Running-OpenCV-On-NPU for more information.') |
| 14 | +# Check OpenCV version |
| 15 | +assert cv.__version__ >= "4.7.0", \ |
| 16 | + "Please install latest opencv-python to try this demo: python3 -m pip install --upgrade opencv-python" |
| 17 | + |
| 18 | +# Valid combinations of backends and targets |
| 19 | +backend_target_pairs = [ |
| 20 | + [cv.dnn.DNN_BACKEND_OPENCV, cv.dnn.DNN_TARGET_CPU], |
| 21 | + [cv.dnn.DNN_BACKEND_CUDA, cv.dnn.DNN_TARGET_CUDA], |
| 22 | + [cv.dnn.DNN_BACKEND_CUDA, cv.dnn.DNN_TARGET_CUDA_FP16], |
| 23 | + [cv.dnn.DNN_BACKEND_TIMVX, cv.dnn.DNN_TARGET_NPU], |
| 24 | + [cv.dnn.DNN_BACKEND_CANN, cv.dnn.DNN_TARGET_NPU] |
| 25 | +] |
33 | 26 |
|
34 | 27 | parser = argparse.ArgumentParser(description='YuNet: A Fast and Accurate CNN-based Face Detector (https://github.com/ShiqiYu/libfacedetection).') |
35 | | -parser.add_argument('--input', '-i', type=str, help='Usage: Set input to a certain image, omit if using camera.') |
36 | | -parser.add_argument('--model', '-m', type=str, default='face_detection_yunet_2022mar.onnx', help="Usage: Set model type, defaults to 'face_detection_yunet_2022mar.onnx'.") |
37 | | -parser.add_argument('--backend', '-b', type=int, default=backends[0], help=help_msg_backends.format(*backends)) |
38 | | -parser.add_argument('--target', '-t', type=int, default=targets[0], help=help_msg_targets.format(*targets)) |
39 | | -parser.add_argument('--conf_threshold', type=float, default=0.9, help='Usage: Set the minimum needed confidence for the model to identify a face, defauts to 0.9. Smaller values may result in faster detection, but will limit accuracy. Filter out faces of confidence < conf_threshold.') |
40 | | -parser.add_argument('--nms_threshold', type=float, default=0.3, help='Usage: Suppress bounding boxes of iou >= nms_threshold. Default = 0.3.') |
41 | | -parser.add_argument('--top_k', type=int, default=5000, help='Usage: Keep top_k bounding boxes before NMS.') |
42 | | -parser.add_argument('--save', '-s', type=str, default=False, help='Usage: Set “True” to save file with results (i.e. bounding box, confidence level). Invalid in case of camera input. Default will be set to “False”.') |
43 | | -parser.add_argument('--vis', '-v', type=str2bool, default=True, help='Usage: Default will be set to “True” and will open a new window to show results. Set to “False” to stop visualizations from being shown. Invalid in case of camera input.') |
| 28 | +parser.add_argument('--input', '-i', type=str, |
| 29 | + help='Usage: Set input to a certain image, omit if using camera.') |
| 30 | +parser.add_argument('--model', '-m', type=str, default='face_detection_yunet_2022mar.onnx', |
| 31 | + help="Usage: Set model type, defaults to 'face_detection_yunet_2022mar.onnx'.") |
| 32 | +parser.add_argument('--backend_target', '-bt', type=int, default=0, |
| 33 | + help='''Choose one of the backend-target pair to run this demo: |
| 34 | + {:d}: (default) OpenCV implementation + CPU, |
| 35 | + {:d}: CUDA + GPU (CUDA), |
| 36 | + {:d}: CUDA + GPU (CUDA FP16), |
| 37 | + {:d}: TIM-VX + NPU, |
| 38 | + {:d}: CANN + NPU |
| 39 | + '''.format(*[x for x in range(len(backend_target_pairs))])) |
| 40 | +parser.add_argument('--conf_threshold', type=float, default=0.9, |
| 41 | + help='Usage: Set the minimum needed confidence for the model to identify a face, defauts to 0.9. Smaller values may result in faster detection, but will limit accuracy. Filter out faces of confidence < conf_threshold.') |
| 42 | +parser.add_argument('--nms_threshold', type=float, default=0.3, |
| 43 | + help='Usage: Suppress bounding boxes of iou >= nms_threshold. Default = 0.3.') |
| 44 | +parser.add_argument('--top_k', type=int, default=5000, |
| 45 | + help='Usage: Keep top_k bounding boxes before NMS.') |
| 46 | +parser.add_argument('--save', '-s', action='store_true', |
| 47 | + help='Usage: Specify to save file with results (i.e. bounding box, confidence level). Invalid in case of camera input.') |
| 48 | +parser.add_argument('--vis', '-v', action='store_true', |
| 49 | + help='Usage: Specify to open a new window to show results. Invalid in case of camera input.') |
44 | 50 | args = parser.parse_args() |
45 | 51 |
|
46 | 52 | def visualize(image, results, box_color=(0, 255, 0), text_color=(0, 0, 255), fps=None): |
@@ -70,14 +76,17 @@ def visualize(image, results, box_color=(0, 255, 0), text_color=(0, 0, 255), fps |
70 | 76 | return output |
71 | 77 |
|
72 | 78 | if __name__ == '__main__': |
| 79 | + backend_id = backend_target_pairs[args.backend_target][0] |
| 80 | + target_id = backend_target_pairs[args.backend_target][1] |
| 81 | + |
73 | 82 | # Instantiate YuNet |
74 | 83 | model = YuNet(modelPath=args.model, |
75 | 84 | inputSize=[320, 320], |
76 | 85 | confThreshold=args.conf_threshold, |
77 | 86 | nmsThreshold=args.nms_threshold, |
78 | 87 | topK=args.top_k, |
79 | | - backendId=args.backend, |
80 | | - targetId=args.target) |
| 88 | + backendId=backend_id, |
| 89 | + targetId=target_id) |
81 | 90 |
|
82 | 91 | # If input is an image |
83 | 92 | if args.input is not None: |
@@ -134,4 +143,3 @@ def visualize(image, results, box_color=(0, 255, 0), text_color=(0, 0, 255), fps |
134 | 143 | cv.imshow('YuNet Demo', frame) |
135 | 144 |
|
136 | 145 | tm.reset() |
137 | | - |
|
0 commit comments