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 | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 8 | from collections import namedtuple |
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 |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 15 | |
| 16 | import cv2 |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 17 | from openvino.inference_engine import IECore |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 18 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 19 | from base_camera import BaseCamera |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 20 | |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 21 | Shape = namedtuple('Shape', ['n','c','h','w']) |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 22 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 23 | class Camera(BaseCamera): |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 24 | model = None |
| 25 | prob_threshold = 0.0 |
| 26 | input = None |
| 27 | device = None |
Shad Ansari | d365451 | 2021-09-29 10:31:53 -0700 | [diff] [blame] | 28 | |
Shad Ansari | c0726e6 | 2021-10-04 22:38:53 +0000 | [diff] [blame] | 29 | def __init__(self, device, args): |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 30 | log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout) |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 31 | |
Shad Ansari | c9f48d3 | 2021-10-25 19:03:34 +0000 | [diff] [blame] | 32 | self.model_xml = args.model |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 33 | self.input = args.input |
| 34 | self.prob_threshold = args.prob_threshold |
| 35 | |
| 36 | self.is_async_mode = True |
| 37 | |
| 38 | self.device = device |
| 39 | |
| 40 | super(Camera, self).__init__(device, args.idle) |
| 41 | |
| 42 | def __del__(self): |
| 43 | # stream.release() |
| 44 | cv2.destroyAllWindows() |
| 45 | |
| 46 | def init_stream(self): |
| 47 | if self.input == 'cam': |
| 48 | input_stream = 0 |
| 49 | elif self.input == 'gstreamer': |
| 50 | input_stream = 'udpsrc port=500' + self.device + ' caps = " application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96" ! rtph264depay ! avdec_h264 ! videoconvert ! appsink' |
| 51 | else: |
| 52 | input_stream = self.input |
| 53 | assert os.path.isfile(self.input), "Specified input file doesn't exist" |
| 54 | |
| 55 | if self.input == 'gstreamer': |
| 56 | stream = cv2.VideoCapture(input_stream, cv2.CAP_GSTREAMER) |
| 57 | else: |
| 58 | stream = cv2.VideoCapture(input_stream) |
| 59 | |
| 60 | return stream |
| 61 | |
| 62 | |
| 63 | def init_inference(self): |
Shad Ansari | c9f48d3 | 2021-10-25 19:03:34 +0000 | [diff] [blame] | 64 | self.model_bin = os.path.splitext(self.model_xml)[0] + ".bin" |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 65 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 66 | # Read IR |
| 67 | log.info("Reading IR...") |
Shad Ansari | c9f48d3 | 2021-10-25 19:03:34 +0000 | [diff] [blame] | 68 | net = IECore().read_network(model=self.model_xml, weights=self.model_bin) |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 69 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 70 | assert len(net.inputs.keys()) == 1, "Demo supports only single input topologies" |
| 71 | assert len(net.outputs) == 1, "Demo supports only single output topologies" |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 72 | input_blob = next(iter(net.inputs)) |
| 73 | out_blob = next(iter(net.outputs)) |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 74 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 75 | log.info("Loading IR to the plugin...") |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 76 | exec_net = IECore().load_network(network=net, device_name="CPU", num_requests=2) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 77 | # Read and pre-process input image |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 78 | shape = Shape(*net.inputs[input_blob].shape) |
Shad Ansari | c9f48d3 | 2021-10-25 19:03:34 +0000 | [diff] [blame] | 79 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 80 | del net |
Shad Ansari | d365451 | 2021-09-29 10:31:53 -0700 | [diff] [blame] | 81 | |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 82 | return exec_net, shape, input_blob, out_blob |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 83 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 84 | |
| 85 | def frames(self): |
Shad Ansari | 79615b9 | 2021-09-30 11:15:41 -0700 | [diff] [blame] | 86 | |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 87 | exec_net, shape, input_blob, out_blob = self.init_inference() |
| 88 | stream = self.init_stream() |
Shad Ansari | 79615b9 | 2021-09-30 11:15:41 -0700 | [diff] [blame] | 89 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 90 | cur_request_id = 0 |
| 91 | next_request_id = 1 |
| 92 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 93 | # Async doesn't work if True |
| 94 | # Request issues = Runtime Error: [REQUEST BUSY] |
Shad Ansari | ef185d2 | 2021-10-14 17:49:26 +0000 | [diff] [blame] | 95 | # self.is_async_mode = False |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 96 | render_time = 0 |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 97 | ret, frame = stream.read() |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 98 | |
| 99 | while True: |
| 100 | if self.is_async_mode: |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 101 | ret, next_frame = stream.read() |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 102 | else: |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 103 | ret, frame = stream.read() |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 104 | if not ret: |
| 105 | break |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 106 | initial_w = stream.get(cv2.CAP_PROP_FRAME_WIDTH) |
| 107 | initial_h = stream.get(cv2.CAP_PROP_FRAME_HEIGHT) |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 108 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 109 | # Main sync point: |
| 110 | # in the truly Async mode we start the NEXT infer request, while waiting for the CURRENT to complete |
| 111 | # in the regular mode we start the CURRENT request and immediately wait for it's completion |
| 112 | inf_start = time.time() |
| 113 | if self.is_async_mode: |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 114 | in_frame = cv2.resize(next_frame, (shape.w, shape.h)) |
Shad Ansari | 341ca3a | 2021-09-30 12:10:00 -0700 | [diff] [blame] | 115 | in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 116 | in_frame = in_frame.reshape((shape.n, shape.c, shape.h, shape.w)) |
| 117 | exec_net.start_async(request_id=next_request_id, inputs={input_blob: in_frame}) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 118 | else: |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 119 | in_frame = cv2.resize(frame, (shape.w, shape.h)) |
Shad Ansari | 341ca3a | 2021-09-30 12:10:00 -0700 | [diff] [blame] | 120 | in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 121 | in_frame = in_frame.reshape((shape.n, shape.c, shape.h, shape.w)) |
| 122 | exec_net.start_async(request_id=cur_request_id, inputs={input_blob: in_frame}) |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 123 | |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 124 | if exec_net.requests[cur_request_id].wait(-1) == 0: |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 125 | inf_end = time.time() |
| 126 | det_time = inf_end - inf_start |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 127 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 128 | # Parse detection results of the current request |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 129 | res = exec_net.requests[cur_request_id].outputs[out_blob] |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 130 | |
Shad Ansari | 0bee2f8 | 2021-10-25 20:07:22 +0000 | [diff] [blame] | 131 | initial_w = 640 |
| 132 | initial_h = 480 |
| 133 | frame = cv2.resize(frame, (initial_w, initial_h)) |
| 134 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 135 | for obj in res[0][0]: |
| 136 | # Draw only objects when probability more than specified threshold |
Shad Ansari | 4ae1168 | 2021-10-22 18:51:53 +0000 | [diff] [blame] | 137 | if obj[2] > self.prob_threshold: |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 138 | xmin = int(obj[3] * initial_w) |
| 139 | ymin = int(obj[4] * initial_h) |
| 140 | xmax = int(obj[5] * initial_w) |
| 141 | ymax = int(obj[6] * initial_h) |
| 142 | class_id = int(obj[1]) |
| 143 | # Draw box and label\class_id |
Shad Ansari | ef185d2 | 2021-10-14 17:49:26 +0000 | [diff] [blame] | 144 | color = (0, 0, 255) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 145 | cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), color, 2) |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 146 | det_label = str(class_id) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 147 | cv2.putText(frame, det_label + ' ' + str(round(obj[2] * 100, 1)) + ' %', (xmin, ymin - 7), |
| 148 | cv2.FONT_HERSHEY_COMPLEX, 0.6, color, 1) |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 149 | |
Shad Ansari | c0726e6 | 2021-10-04 22:38:53 +0000 | [diff] [blame] | 150 | cv2.putText(frame, self.device, (10, int(initial_h - 20)), |
| 151 | cv2.FONT_HERSHEY_COMPLEX, 0.5, (10, 10, 200), 1) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 152 | |
| 153 | render_start = time.time() |
| 154 | |
| 155 | yield cv2.imencode('.jpg', frame)[1].tobytes() |
| 156 | |
Shad Ansari | 341ca3a | 2021-09-30 12:10:00 -0700 | [diff] [blame] | 157 | render_end = time.time() |
| 158 | render_time = render_end - render_start |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 159 | |
| 160 | if self.is_async_mode: |
| 161 | cur_request_id, next_request_id = next_request_id, cur_request_id |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 162 | frame = next_frame |