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 argparse
8 
9 import numpy as np
10 import cv2 as cv
11 
12 from ppresnet import PPResNet
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 = "Chose 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://gist.github.com/fengyuentau/5a7a5ba36328f2b763aea026c43fa45f for more information.')
33 
34 parser = argparse.ArgumentParser(description='Deep Residual Learning for Image Recognition (https://arxiv.org/abs/1512.03385, https://github.com/PaddlePaddle/PaddleHub)')
35 parser.add_argument('--input', '-i', type=str, help='Path to the input image.')
36 parser.add_argument('--model', '-m', type=str, default='image_classification_ppresnet50_2022jan.onnx', help='Path to the model.')
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('--label', '-l', type=str, default='./imagenet_labels.txt', help='Path to the dataset labels.')
40 args = parser.parse_args()
41 
42 if __name__ == '__main__':
43  # Instantiate ResNet
44  model = PPResNet(modelPath=args.model, labelPath=args.label, backendId=args.backend, targetId=args.target)
45 
46  # Read image and get a 224x224 crop from a 256x256 resized
47  image = cv.imread(args.input)
48  image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
49  image = cv.resize(image, dsize=(256, 256))
50  image = image[16:240, 16:240, :]
51 
52  # Inference
53  result = model.infer(image)
54 
55  # Print result
56  print('label: {}'.format(result))
57 
demo.str2bool
str2bool
Definition: demo.py:43