classification.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import argparse
  2. import cv2 as cv
  3. import numpy as np
  4. from common import *
  5. def get_args_parser(func_args):
  6. backends = (cv.dnn.DNN_BACKEND_DEFAULT, cv.dnn.DNN_BACKEND_HALIDE, cv.dnn.DNN_BACKEND_INFERENCE_ENGINE,
  7. cv.dnn.DNN_BACKEND_OPENCV, cv.dnn.DNN_BACKEND_VKCOM, cv.dnn.DNN_BACKEND_CUDA)
  8. targets = (cv.dnn.DNN_TARGET_CPU, cv.dnn.DNN_TARGET_OPENCL, cv.dnn.DNN_TARGET_OPENCL_FP16, cv.dnn.DNN_TARGET_MYRIAD,
  9. cv.dnn.DNN_TARGET_HDDL, cv.dnn.DNN_TARGET_VULKAN, cv.dnn.DNN_TARGET_CUDA, cv.dnn.DNN_TARGET_CUDA_FP16)
  10. parser = argparse.ArgumentParser(add_help=False)
  11. parser.add_argument('--zoo', default=os.path.join(os.path.dirname(os.path.abspath(__file__)), 'models.yml'),
  12. help='An optional path to file with preprocessing parameters.')
  13. parser.add_argument('--input',
  14. help='Path to input image or video file. Skip this argument to capture frames from a camera.')
  15. parser.add_argument('--framework', choices=['caffe', 'tensorflow', 'torch', 'darknet'],
  16. help='Optional name of an origin framework of the model. '
  17. 'Detect it automatically if it does not set.')
  18. parser.add_argument('--std', nargs='*', type=float,
  19. help='Preprocess input image by dividing on a standard deviation.')
  20. parser.add_argument('--crop', type=bool, default=False,
  21. help='Preprocess input image by dividing on a standard deviation.')
  22. parser.add_argument('--initial_width', type=int,
  23. help='Preprocess input image by initial resizing to a specific width.')
  24. parser.add_argument('--initial_height', type=int,
  25. help='Preprocess input image by initial resizing to a specific height.')
  26. parser.add_argument('--backend', choices=backends, default=cv.dnn.DNN_BACKEND_DEFAULT, type=int,
  27. help="Choose one of computation backends: "
  28. "%d: automatically (by default), "
  29. "%d: Halide language (http://halide-lang.org/), "
  30. "%d: Intel's Deep Learning Inference Engine (https://software.intel.com/openvino-toolkit), "
  31. "%d: OpenCV implementation, "
  32. "%d: VKCOM, "
  33. "%d: CUDA" % backends)
  34. parser.add_argument('--target', choices=targets, default=cv.dnn.DNN_TARGET_CPU, type=int,
  35. help='Choose one of target computation devices: '
  36. '%d: CPU target (by default), '
  37. '%d: OpenCL, '
  38. '%d: OpenCL fp16 (half-float precision), '
  39. '%d: NCS2 VPU, '
  40. '%d: HDDL VPU, '
  41. '%d: Vulkan, '
  42. '%d: CUDA, '
  43. '%d: CUDA fp16 (half-float preprocess)'% targets)
  44. args, _ = parser.parse_known_args()
  45. add_preproc_args(args.zoo, parser, 'classification')
  46. parser = argparse.ArgumentParser(parents=[parser],
  47. description='Use this script to run classification deep learning networks using OpenCV.',
  48. formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  49. return parser.parse_args(func_args)
  50. def main(func_args=None):
  51. args = get_args_parser(func_args)
  52. args.model = findFile(args.model)
  53. args.config = findFile(args.config)
  54. args.classes = findFile(args.classes)
  55. # Load names of classes
  56. classes = None
  57. if args.classes:
  58. with open(args.classes, 'rt') as f:
  59. classes = f.read().rstrip('\n').split('\n')
  60. # Load a network
  61. net = cv.dnn.readNet(args.model, args.config, args.framework)
  62. net.setPreferableBackend(args.backend)
  63. net.setPreferableTarget(args.target)
  64. winName = 'Deep learning image classification in OpenCV'
  65. cv.namedWindow(winName, cv.WINDOW_NORMAL)
  66. cap = cv.VideoCapture(args.input if args.input else 0)
  67. while cv.waitKey(1) < 0:
  68. hasFrame, frame = cap.read()
  69. if not hasFrame:
  70. cv.waitKey()
  71. break
  72. # Create a 4D blob from a frame.
  73. inpWidth = args.width if args.width else frame.shape[1]
  74. inpHeight = args.height if args.height else frame.shape[0]
  75. if args.initial_width and args.initial_height:
  76. frame = cv.resize(frame, (args.initial_width, args.initial_height))
  77. blob = cv.dnn.blobFromImage(frame, args.scale, (inpWidth, inpHeight), args.mean, args.rgb, crop=args.crop)
  78. if args.std:
  79. blob[0] /= np.asarray(args.std, dtype=np.float32).reshape(3, 1, 1)
  80. # Run a model
  81. net.setInput(blob)
  82. out = net.forward()
  83. # Get a class with a highest score.
  84. out = out.flatten()
  85. classId = np.argmax(out)
  86. confidence = out[classId]
  87. # Put efficiency information.
  88. t, _ = net.getPerfProfile()
  89. label = 'Inference time: %.2f ms' % (t * 1000.0 / cv.getTickFrequency())
  90. cv.putText(frame, label, (0, 15), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))
  91. # Print predicted class.
  92. label = '%s: %.4f' % (classes[classId] if classes else 'Class #%d' % classId, confidence)
  93. cv.putText(frame, label, (0, 40), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))
  94. cv.imshow(winName, frame)
  95. if __name__ == "__main__":
  96. main()