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 | |
| 8 | import logging as log |
| 9 | import os |
| 10 | import sys |
| 11 | import time |
| 12 | from argparse import ArgumentParser, SUPPRESS |
| 13 | |
| 14 | import cv2 |
| 15 | from imutils import build_montages |
| 16 | from openvino.inference_engine import IECore |
| 17 | |
| 18 | |
| 19 | def build_argparser(): |
| 20 | parser = ArgumentParser(add_help=False) |
| 21 | args = parser.add_argument_group('Options') |
| 22 | args.add_argument('-h', '--help', action='help', default=SUPPRESS, help='Show this help message and exit.') |
| 23 | args.add_argument("-m", "--model", help="Required. Path to an .xml file with a trained model.", |
| 24 | required=True, type=str) |
| 25 | args.add_argument("-i", "--input", |
| 26 | help="Required. Path to video file or image. 'cam' for capturing video stream from camera", |
| 27 | required=True, type=str) |
| 28 | # args.add_argument("-i2", "--input2", |
| 29 | # help="Optional. Path to second video file or image. 'cam' for capturing video stream from camera", |
| 30 | # default=None, type=str) |
| 31 | args.add_argument("-l", "--cpu_extension", |
| 32 | help="Optional. Required for CPU custom layers. Absolute path to a shared library with the " |
| 33 | "kernels implementations.", type=str, default=None) |
| 34 | args.add_argument("-pp", "--plugin_dir", help="Optional. Path to a plugin folder", type=str, default=None) |
| 35 | args.add_argument("-d", "--device", |
| 36 | help="Optional. Specify the target device to infer on; CPU, GPU, FPGA, HDDL or MYRIAD is " |
| 37 | "acceptable. The demo will look for a suitable plugin for device specified. " |
| 38 | "Default value is CPU", default="CPU", type=str) |
| 39 | args.add_argument("--labels", help="Optional. Path to labels mapping file", default=None, type=str) |
| 40 | args.add_argument("-pt", "--prob_threshold", help="Optional. Probability threshold for detections filtering", |
| 41 | default=0.5, type=float) |
| 42 | args.add_argument("-ns", help='No show output', action='store_true') |
| 43 | |
| 44 | return parser |
| 45 | |
| 46 | |
| 47 | def main(): |
| 48 | log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout) |
| 49 | args = build_argparser().parse_args() |
| 50 | model_xml = args.model |
| 51 | model_bin = os.path.splitext(model_xml)[0] + ".bin" |
| 52 | # Plugin initialization for specified device and load extensions library if specified |
| 53 | log.info("Initializing plugin for {} device...".format(args.device)) |
| 54 | # plugin = IEPlugin(device=args.device, plugin_dirs=args.plugin_dir) |
| 55 | # if args.cpu_extension and 'CPU' in args.device: |
| 56 | # plugin.add_cpu_extension(args.cpu_extension) |
| 57 | # Read IR |
| 58 | log.info("Reading IR...") |
| 59 | net = IECore().read_network(model=model_xml, weights=model_bin) |
| 60 | |
| 61 | assert len(net.inputs.keys()) == 1, "Demo supports only single input topologies" |
| 62 | assert len(net.outputs) == 1, "Demo supports only single output topologies" |
| 63 | input_blob = next(iter(net.inputs)) |
| 64 | out_blob = next(iter(net.outputs)) |
| 65 | |
| 66 | # input_blob2 = next(iter(net.inputs)) |
| 67 | # out_blob2 = next(iter(net.outputs)) |
| 68 | |
| 69 | log.info("Loading IR to the plugin...") |
| 70 | # exec_net = IECore().load_network(network=net, device_name=args.device, num_requests=2) |
| 71 | exec_net = IECore().load_network(network=net, device_name=args.device, num_requests=1) |
| 72 | # Read and pre-process input image |
| 73 | n, c, h, w = net.inputs[input_blob].shape |
| 74 | # n2, c2, h2, w2 = net.inputs[input_blob2].shape |
| 75 | del net |
| 76 | if args.input == 'cam': |
| 77 | input_stream = 0 |
| 78 | elif args.input == 'gstreamer': |
| 79 | # gst rtp sink |
| 80 | input_stream = 'udpsrc port=5000 caps = " application/x-rtp, encoding-name=JPEG,payload=26" ! rtpjpegdepay ! decodebin ! videoconvert ! appsink' |
| 81 | #input_stream = 'udpsrc port=5000 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96" ! rtph264depay ! decodebin ! videoconvert ! appsink' |
| 82 | else: |
| 83 | input_stream = args.input |
| 84 | assert os.path.isfile(args.input), "Specified input file doesn't exist" |
| 85 | |
| 86 | if input_stream == 'gstreamer': |
| 87 | cap = cv2.VideoCapture(input_stream, cv2.CAP_GSTREAMER) |
| 88 | else: |
| 89 | cap = cv2.VideoCapture(input_stream) |
| 90 | |
| 91 | # if args.input2 == 'cam': |
| 92 | # input_stream2 = 0 |
| 93 | # elif args.input2 == 'gstreamer': |
| 94 | # input_stream2 = 'udpsrc port=5001 caps = " application/x-rtp, encoding-name=JPEG,payload=26" ! rtpjpegdepay ! decodebin ! videoconvert ! appsink' |
| 95 | # else: |
| 96 | # input_stream2 = args.input2 |
| 97 | # assert os.path.isfile(args.input2), "Specified input file doesn't exist" |
| 98 | if args.labels: |
| 99 | with open(args.labels, 'r') as f: |
| 100 | labels_map = [x.strip() for x in f] |
| 101 | else: |
| 102 | labels_map = None |
| 103 | |
| 104 | # if input_stream2 == 'gstreamer': |
| 105 | # cap2 = cv2.VideoCapture(input_stream2, cv2.CAP_GSTREAMER) |
| 106 | # else: |
| 107 | # cap2 = cv2.VideoCapture(input_stream2) |
| 108 | |
| 109 | cur_request_id = 0 |
| 110 | next_request_id = 1 |
| 111 | |
| 112 | # cur_request_id2 = 1 |
| 113 | # next_request_id2 = 0 |
| 114 | |
| 115 | log.info("Starting inference in async mode...") |
| 116 | log.info("To switch between sync and async modes press Tab button") |
| 117 | log.info("To stop the demo execution press Esc button") |
| 118 | |
| 119 | # Async doesn't work if True |
| 120 | # Request issues = Runtime Error: [REQUEST BUSY] |
| 121 | is_async_mode = False |
| 122 | render_time = 0 |
| 123 | ret, frame = cap.read() |
| 124 | # ret2, frame2 = cap2.read() |
| 125 | |
| 126 | # Montage width and height |
| 127 | # In this case means 2x1 boxes |
| 128 | mW = 2 |
| 129 | mH = 1 |
| 130 | |
| 131 | frameList = [] |
| 132 | |
| 133 | print("To close the application, press 'CTRL+C' or any key with focus on the output window") |
| 134 | # while cap.isOpened() or cap2.isOpened(): |
| 135 | while cap.isOpened(): |
| 136 | if is_async_mode: |
| 137 | ret, next_frame = cap.read() |
| 138 | # ret2, next_frame2 = cap2.read() |
| 139 | else: |
| 140 | ret, frame = cap.read() |
| 141 | # ret2, frame2 = cap2.read() |
| 142 | #if not (ret and ret2): |
| 143 | if not ret: |
| 144 | break |
| 145 | initial_w = cap.get(3) |
| 146 | initial_h = cap.get(4) |
| 147 | # initial_w2 = cap2.get(3) |
| 148 | # initial_h2 = cap2.get(4) |
| 149 | # Main sync point: |
| 150 | # in the truly Async mode we start the NEXT infer request, while waiting for the CURRENT to complete |
| 151 | # in the regular mode we start the CURRENT request and immediately wait for it's completion |
| 152 | inf_start = time.time() |
| 153 | if is_async_mode: |
| 154 | # if ret and ret2: |
| 155 | if ret: |
| 156 | in_frame = cv2.resize(next_frame, (w, h)) |
| 157 | in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW |
| 158 | in_frame = in_frame.reshape((n, c, h, w)) |
| 159 | exec_net.start_async(request_id=next_request_id, inputs={input_blob: in_frame}) |
| 160 | |
| 161 | # in_frame2 = cv2.resize(next_frame2, (w2, h2)) |
| 162 | # in_frame2 = in_frame2.transpose((2, 0, 1)) # Change data layout from HWC to CHW |
| 163 | # in_frame2 = in_frame2.reshape((n2, c2, h2, w2)) |
| 164 | # exec_net.start_async(request_id=next_request_id2, inputs={input_blob2: in_frame2}) |
| 165 | |
| 166 | else: |
| 167 | # if (ret and ret2): |
| 168 | if ret: |
| 169 | in_frame = cv2.resize(frame, (w, h)) |
| 170 | in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW |
| 171 | in_frame = in_frame.reshape((n, c, h, w)) |
| 172 | exec_net.start_async(request_id=cur_request_id, inputs={input_blob: in_frame}) |
| 173 | |
| 174 | # in_frame2 = cv2.resize(frame2, (w2, h2)) |
| 175 | # in_frame2 = in_frame2.transpose((2, 0, 1)) # Change data layout from HWC to CHW |
| 176 | # in_frame2 = in_frame2.reshape((n2, c2, h2, w2)) |
| 177 | # exec_net.start_async(request_id=cur_request_id2, inputs={input_blob2: in_frame2}) |
| 178 | |
| 179 | # if exec_net.requests[cur_request_id].wait(-1) == 0 and exec_net.requests[cur_request_id2].wait(-1) == 0: |
| 180 | if exec_net.requests[cur_request_id].wait(-1) == 0: |
| 181 | inf_end = time.time() |
| 182 | det_time = inf_end - inf_start |
| 183 | |
| 184 | # Parse detection results of the current request |
| 185 | res = exec_net.requests[cur_request_id].outputs[out_blob] |
| 186 | # res2 = exec_net.requests[cur_request_id2].outputs[out_blob2] |
| 187 | |
| 188 | for obj in res[0][0]: |
| 189 | # Draw only objects when probability more than specified threshold |
| 190 | if obj[2] > args.prob_threshold: |
| 191 | xmin = int(obj[3] * initial_w) |
| 192 | ymin = int(obj[4] * initial_h) |
| 193 | xmax = int(obj[5] * initial_w) |
| 194 | ymax = int(obj[6] * initial_h) |
| 195 | class_id = int(obj[1]) |
| 196 | # Draw box and label\class_id |
| 197 | color = (min(class_id * 12.5, 255), min(class_id * 7, 255), min(class_id * 5, 255)) |
| 198 | cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), color, 2) |
| 199 | det_label = labels_map[class_id] if labels_map else str(class_id) |
| 200 | cv2.putText(frame, det_label + ' ' + str(round(obj[2] * 100, 1)) + ' %', (xmin, ymin - 7), |
| 201 | cv2.FONT_HERSHEY_COMPLEX, 0.6, color, 1) |
| 202 | print('Object detected, class_id:', class_id, 'probability:', obj[2], 'xmin:', xmin, 'ymin:', ymin, |
| 203 | 'xmax:', xmax, 'ymax:', ymax) |
| 204 | |
| 205 | # for obj in res2[0][0]: |
| 206 | # # Draw only objects when probability more than specified threshold |
| 207 | # if obj[2] > args.prob_threshold: |
| 208 | # xmin = int(obj[3] * initial_w2) |
| 209 | # ymin = int(obj[4] * initial_h2) |
| 210 | # xmax = int(obj[5] * initial_w2) |
| 211 | # ymax = int(obj[6] * initial_h2) |
| 212 | # class_id = int(obj[1]) |
| 213 | # # Draw box and label\class_id |
| 214 | # color = (min(class_id * 12.5, 255), min(class_id * 7, 255), min(class_id * 5, 255)) |
| 215 | # cv2.rectangle(frame2, (xmin, ymin), (xmax, ymax), color, 2) |
| 216 | # det_label = labels_map[class_id] if labels_map else str(class_id) |
| 217 | # cv2.putText(frame2, det_label + ' ' + str(round(obj[2] * 100, 1)) + ' %', (xmin, ymin - 7), |
| 218 | # cv2.FONT_HERSHEY_COMPLEX, 0.6, color, 1) |
| 219 | # print('Object detected, class_id:', class_id, 'probability:', obj[2], 'xmin:', xmin, 'ymin:', ymin, |
| 220 | # 'xmax:', xmax, 'ymax:', ymax) |
| 221 | |
| 222 | # Draw performance stats |
| 223 | inf_time_message = "Inference time: Not applicable for async mode" if is_async_mode else \ |
| 224 | "Inference time: {:.3f} ms".format(det_time * 1000) |
| 225 | render_time_message = "OpenCV rendering time: {:.3f} ms".format(render_time * 1000) |
| 226 | if is_async_mode: |
| 227 | async_mode_message = "Async mode is on. Processing request {}".format(cur_request_id) |
| 228 | else: |
| 229 | async_mode_message = "Async mode is off. Processing request {}".format(cur_request_id) |
| 230 | |
| 231 | cv2.putText(frame, inf_time_message, (15, 15), cv2.FONT_HERSHEY_COMPLEX, 0.5, (200, 10, 10), 1) |
| 232 | cv2.putText(frame, render_time_message, (15, 30), cv2.FONT_HERSHEY_COMPLEX, 0.5, (10, 10, 200), 1) |
| 233 | cv2.putText(frame, async_mode_message, (10, int(initial_h - 20)), cv2.FONT_HERSHEY_COMPLEX, 0.5, |
| 234 | (10, 10, 200), 1) |
| 235 | |
| 236 | # cv2.putText(frame2, inf_time_message, (15, 15), cv2.FONT_HERSHEY_COMPLEX, 0.5, (200, 10, 10), 1) |
| 237 | # cv2.putText(frame2, render_time_message, (15, 30), cv2.FONT_HERSHEY_COMPLEX, 0.5, (10, 10, 200), 1) |
| 238 | # cv2.putText(frame2, async_mode_message, (10, int(initial_h - 20)), cv2.FONT_HERSHEY_COMPLEX, 0.5, |
| 239 | # (10, 10, 200), 1) |
| 240 | |
| 241 | render_start = time.time() |
| 242 | |
| 243 | if not args.ns: |
| 244 | # if ret and ret2: |
| 245 | if ret: |
| 246 | # frameList.append(frame) |
| 247 | # # frameList.append(frame2) |
| 248 | # montages = build_montages(frameList, (640, 480), (mW, mH)) |
| 249 | # for montage in montages: |
| 250 | # cv2.imshow("Detection results", montage) |
| 251 | cv2.imshow("Detection results", frame) |
| 252 | render_end = time.time() |
| 253 | render_time = render_end - render_start |
| 254 | |
| 255 | if is_async_mode: |
| 256 | cur_request_id, next_request_id = next_request_id, cur_request_id |
| 257 | |
| 258 | frame = next_frame |
| 259 | # frame2 = next_frame2 |
| 260 | key = cv2.waitKey(1) |
| 261 | if key == 27: |
| 262 | break |
| 263 | if 9 == key: |
| 264 | is_async_mode = not is_async_mode |
| 265 | log.info("Switched to {} mode".format("async" if is_async_mode else "sync")) |
| 266 | |
| 267 | cap.release() |
| 268 | # cap2.release() |
| 269 | cv2.destroyAllWindows() |
| 270 | |
| 271 | |
| 272 | if __name__ == '__main__': |
| 273 | sys.exit(main() or 0) |