Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 1 | """ |
| 2 | SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org> |
| 3 | SPDX-License-Identifier: LicenseRef-ONF-Member-1.01 |
| 4 | """ |
| 5 | |
| 6 | from __future__ import print_function |
| 7 | |
Shad Ansari | d365451 | 2021-09-29 10:31:53 -0700 | [diff] [blame] | 8 | import cv2 |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 9 | import logging as log |
| 10 | import os |
| 11 | import sys |
| 12 | import time |
| 13 | from argparse import ArgumentParser, SUPPRESS |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 14 | from imutils import build_montages |
| 15 | from openvino.inference_engine import IECore |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 16 | from base_camera import BaseCamera |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 17 | |
Shad Ansari | 4ae1168 | 2021-10-22 18:51:53 +0000 | [diff] [blame^] | 18 | DEFAULT_PROB_THRESH = 0.5 |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 19 | |
| 20 | def build_argparser(): |
| 21 | parser = ArgumentParser(add_help=False) |
| 22 | args = parser.add_argument_group('Options') |
| 23 | args.add_argument('-h', '--help', action='help', default=SUPPRESS, help='Show this help message and exit.') |
| 24 | args.add_argument("-m", "--model", help="Required. Path to an .xml file with a trained model.", |
| 25 | required=True, type=str) |
| 26 | args.add_argument("-i", "--input", |
| 27 | help="Required. Path to video file or image. 'cam' for capturing video stream from camera", |
| 28 | required=True, type=str) |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 29 | args.add_argument("-l", "--cpu_extension", |
| 30 | help="Optional. Required for CPU custom layers. Absolute path to a shared library with the " |
| 31 | "kernels implementations.", type=str, default=None) |
| 32 | args.add_argument("-pp", "--plugin_dir", help="Optional. Path to a plugin folder", type=str, default=None) |
| 33 | args.add_argument("-d", "--device", |
| 34 | help="Optional. Specify the target device to infer on; CPU, GPU, FPGA, HDDL or MYRIAD is " |
| 35 | "acceptable. The demo will look for a suitable plugin for device specified. " |
| 36 | "Default value is CPU", default="CPU", type=str) |
| 37 | args.add_argument("--labels", help="Optional. Path to labels mapping file", default=None, type=str) |
| 38 | args.add_argument("-pt", "--prob_threshold", help="Optional. Probability threshold for detections filtering", |
Shad Ansari | 4ae1168 | 2021-10-22 18:51:53 +0000 | [diff] [blame^] | 39 | default=DEFAULT_PROB_THRESH, type=float) |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 40 | args.add_argument("-ns", help='No show output', action='store_true') |
| 41 | |
| 42 | return parser |
| 43 | |
| 44 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 45 | class Camera(BaseCamera): |
Shad Ansari | 4ae1168 | 2021-10-22 18:51:53 +0000 | [diff] [blame^] | 46 | prob_threshold = DEFAULT_PROB_THRESH |
Shad Ansari | d365451 | 2021-09-29 10:31:53 -0700 | [diff] [blame] | 47 | |
Shad Ansari | c0726e6 | 2021-10-04 22:38:53 +0000 | [diff] [blame] | 48 | def __init__(self, device, args): |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 49 | log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout) |
| 50 | model_xml = args.model |
| 51 | model_bin = os.path.splitext(model_xml)[0] + ".bin" |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 52 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 53 | # Read IR |
| 54 | log.info("Reading IR...") |
| 55 | net = IECore().read_network(model=model_xml, weights=model_bin) |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 56 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 57 | assert len(net.inputs.keys()) == 1, "Demo supports only single input topologies" |
| 58 | assert len(net.outputs) == 1, "Demo supports only single output topologies" |
| 59 | self.input_blob = next(iter(net.inputs)) |
| 60 | self.out_blob = next(iter(net.outputs)) |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 61 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 62 | log.info("Loading IR to the plugin...") |
| 63 | self.exec_net = IECore().load_network(network=net, device_name=args.device, num_requests=2) |
| 64 | # Read and pre-process input image |
| 65 | self.n, self.c, self.h, self.w = net.inputs[self.input_blob].shape |
| 66 | del net |
| 67 | if args.input == 'cam': |
Shad Ansari | 79615b9 | 2021-09-30 11:15:41 -0700 | [diff] [blame] | 68 | self.input_stream = 0 |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 69 | elif args.input == 'gstreamer': |
Shad Ansari | 7f9a451 | 2021-10-12 00:14:58 +0000 | [diff] [blame] | 70 | # M-JPEG |
| 71 | # self.input_stream = 'udpsrc port=500' + device + ' caps = " application/x-rtp, encoding-name=JPEG,payload=26" ! rtpjpegdepay ! decodebin ! videoconvert ! appsink' |
| 72 | # H.264 |
| 73 | self.input_stream = 'udpsrc port=500' + device + ' caps = " application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96" ! rtph264depay ! avdec_h264 ! videoconvert ! appsink' |
Shad Ansari | c0726e6 | 2021-10-04 22:38:53 +0000 | [diff] [blame] | 74 | print("input_stream:", self.input_stream) |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 75 | else: |
Shad Ansari | 341ca3a | 2021-09-30 12:10:00 -0700 | [diff] [blame] | 76 | self.input_stream = args.input |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 77 | assert os.path.isfile(args.input), "Specified input file doesn't exist" |
Shad Ansari | d365451 | 2021-09-29 10:31:53 -0700 | [diff] [blame] | 78 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 79 | if args.labels: |
| 80 | with open(args.labels, 'r') as f: |
| 81 | self.labels_map = [x.strip() for x in f] |
| 82 | else: |
| 83 | self.labels_map = None |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 84 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 85 | self.args = args |
Shad Ansari | 4ae1168 | 2021-10-22 18:51:53 +0000 | [diff] [blame^] | 86 | self.prob_threshold = args.prob_threshold |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 87 | |
Shad Ansari | 4ae1168 | 2021-10-22 18:51:53 +0000 | [diff] [blame^] | 88 | super(Camera, self).__init__(device, args.idle) |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 89 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 90 | def __del__(self): |
| 91 | self.cap.release() |
| 92 | cv2.destroyAllWindows() |
| 93 | |
| 94 | def frames(self): |
Shad Ansari | 79615b9 | 2021-09-30 11:15:41 -0700 | [diff] [blame] | 95 | |
| 96 | if self.input_stream == 'gstreamer': |
| 97 | self.cap = cv2.VideoCapture(self.input_stream, cv2.CAP_GSTREAMER) |
| 98 | else: |
| 99 | self.cap = cv2.VideoCapture(self.input_stream) |
| 100 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 101 | cur_request_id = 0 |
| 102 | next_request_id = 1 |
| 103 | |
| 104 | log.info("Starting inference in async mode...") |
| 105 | log.info("To switch between sync and async modes press Tab button") |
| 106 | log.info("To stop the demo execution press Esc button") |
| 107 | |
| 108 | # Async doesn't work if True |
| 109 | # Request issues = Runtime Error: [REQUEST BUSY] |
Shad Ansari | ef185d2 | 2021-10-14 17:49:26 +0000 | [diff] [blame] | 110 | # self.is_async_mode = False |
| 111 | self.is_async_mode = True |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 112 | render_time = 0 |
| 113 | ret, frame = self.cap.read() |
| 114 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 115 | print("To close the application, press 'CTRL+C' or any key with focus on the output window") |
| 116 | |
| 117 | while True: |
| 118 | if self.is_async_mode: |
| 119 | ret, next_frame = self.cap.read() |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 120 | else: |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 121 | ret, frame = self.cap.read() |
| 122 | if not ret: |
| 123 | break |
Shad Ansari | ef185d2 | 2021-10-14 17:49:26 +0000 | [diff] [blame] | 124 | initial_w = self.cap.get(cv2.CAP_PROP_FRAME_WIDTH) |
| 125 | initial_h = self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT) |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 126 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 127 | # Main sync point: |
| 128 | # in the truly Async mode we start the NEXT infer request, while waiting for the CURRENT to complete |
| 129 | # in the regular mode we start the CURRENT request and immediately wait for it's completion |
| 130 | inf_start = time.time() |
| 131 | if self.is_async_mode: |
Shad Ansari | 341ca3a | 2021-09-30 12:10:00 -0700 | [diff] [blame] | 132 | in_frame = cv2.resize(next_frame, (self.w, self.h)) |
| 133 | in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW |
| 134 | in_frame = in_frame.reshape((self.n, self.c, self.h, self.w)) |
| 135 | self.exec_net.start_async(request_id=next_request_id, inputs={self.input_blob: in_frame}) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 136 | else: |
Shad Ansari | 341ca3a | 2021-09-30 12:10:00 -0700 | [diff] [blame] | 137 | in_frame = cv2.resize(frame, (self.w, self.h)) |
| 138 | in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW |
| 139 | in_frame = in_frame.reshape((self.n, self.c, self.h, self.w)) |
| 140 | self.exec_net.start_async(request_id=cur_request_id, inputs={self.input_blob: in_frame}) |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 141 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 142 | if self.exec_net.requests[cur_request_id].wait(-1) == 0: |
| 143 | inf_end = time.time() |
| 144 | det_time = inf_end - inf_start |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 145 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 146 | # Parse detection results of the current request |
| 147 | res = self.exec_net.requests[cur_request_id].outputs[self.out_blob] |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 148 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 149 | for obj in res[0][0]: |
| 150 | # Draw only objects when probability more than specified threshold |
Shad Ansari | 4ae1168 | 2021-10-22 18:51:53 +0000 | [diff] [blame^] | 151 | if obj[2] > self.prob_threshold: |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 152 | xmin = int(obj[3] * initial_w) |
| 153 | ymin = int(obj[4] * initial_h) |
| 154 | xmax = int(obj[5] * initial_w) |
| 155 | ymax = int(obj[6] * initial_h) |
| 156 | class_id = int(obj[1]) |
| 157 | # Draw box and label\class_id |
Shad Ansari | ef185d2 | 2021-10-14 17:49:26 +0000 | [diff] [blame] | 158 | color = (0, 0, 255) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 159 | cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), color, 2) |
| 160 | det_label = self.labels_map[class_id] if self.labels_map else str(class_id) |
| 161 | cv2.putText(frame, det_label + ' ' + str(round(obj[2] * 100, 1)) + ' %', (xmin, ymin - 7), |
| 162 | cv2.FONT_HERSHEY_COMPLEX, 0.6, color, 1) |
Shad Ansari | 341ca3a | 2021-09-30 12:10:00 -0700 | [diff] [blame] | 163 | # print('Object detected, class_id:', class_id, 'probability:', obj[2], 'xmin:', xmin, 'ymin:', ymin, |
| 164 | # 'xmax:', xmax, 'ymax:', ymax) |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 165 | |
Shad Ansari | c0726e6 | 2021-10-04 22:38:53 +0000 | [diff] [blame] | 166 | cv2.putText(frame, self.device, (10, int(initial_h - 20)), |
| 167 | cv2.FONT_HERSHEY_COMPLEX, 0.5, (10, 10, 200), 1) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 168 | |
| 169 | render_start = time.time() |
| 170 | |
| 171 | yield cv2.imencode('.jpg', frame)[1].tobytes() |
| 172 | |
Shad Ansari | 341ca3a | 2021-09-30 12:10:00 -0700 | [diff] [blame] | 173 | render_end = time.time() |
| 174 | render_time = render_end - render_start |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 175 | |
| 176 | if self.is_async_mode: |
| 177 | cur_request_id, next_request_id = next_request_id, cur_request_id |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 178 | frame = next_frame |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 179 | |
| 180 | |
| 181 | if __name__ == '__main__': |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 182 | args = build_argparser().parse_args() |
| 183 | camera = Camera(args) |
| 184 | camera.frames() |
| 185 | del camera |