blob: 8eff1bcf8fc38ba54351d1a06a0b1df3d3c523eb [file] [log] [blame]
Shad Ansari47432b62021-09-27 22:46:25 +00001"""
2SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org>
3SPDX-License-Identifier: LicenseRef-ONF-Member-1.01
4"""
5
6from __future__ import print_function
7
Shad Ansaria2714652021-10-23 00:10:18 +00008from collections import namedtuple
Shad Ansari47432b62021-09-27 22:46:25 +00009import logging as log
10import os
11import sys
12import time
13from argparse import ArgumentParser, SUPPRESS
Shad Ansari47432b62021-09-27 22:46:25 +000014from imutils import build_montages
Shad Ansaria2714652021-10-23 00:10:18 +000015
16import cv2
Shad Ansari47432b62021-09-27 22:46:25 +000017from openvino.inference_engine import IECore
Shad Ansaria2714652021-10-23 00:10:18 +000018
Shad Ansari30a23732021-09-29 23:07:21 -070019from base_camera import BaseCamera
Shad Ansari47432b62021-09-27 22:46:25 +000020
Shad Ansaria2714652021-10-23 00:10:18 +000021Shape = namedtuple('Shape', ['n','c','h','w'])
Shad Ansari47432b62021-09-27 22:46:25 +000022
Shad Ansari30a23732021-09-29 23:07:21 -070023class Camera(BaseCamera):
Shad Ansaria2714652021-10-23 00:10:18 +000024 model = None
25 prob_threshold = 0.0
26 input = None
27 device = None
Shad Ansarid3654512021-09-29 10:31:53 -070028
Shad Ansaric0726e62021-10-04 22:38:53 +000029 def __init__(self, device, args):
Shad Ansari30a23732021-09-29 23:07:21 -070030 log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout)
Shad Ansaria2714652021-10-23 00:10:18 +000031
32 self.model = args.model
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):
64 model_xml = self.model
Shad Ansari30a23732021-09-29 23:07:21 -070065 model_bin = os.path.splitext(model_xml)[0] + ".bin"
Shad Ansari47432b62021-09-27 22:46:25 +000066
Shad Ansari30a23732021-09-29 23:07:21 -070067 # Read IR
68 log.info("Reading IR...")
69 net = IECore().read_network(model=model_xml, weights=model_bin)
Shad Ansari47432b62021-09-27 22:46:25 +000070
Shad Ansari30a23732021-09-29 23:07:21 -070071 assert len(net.inputs.keys()) == 1, "Demo supports only single input topologies"
72 assert len(net.outputs) == 1, "Demo supports only single output topologies"
Shad Ansaria2714652021-10-23 00:10:18 +000073 input_blob = next(iter(net.inputs))
74 out_blob = next(iter(net.outputs))
Shad Ansari47432b62021-09-27 22:46:25 +000075
Shad Ansari30a23732021-09-29 23:07:21 -070076 log.info("Loading IR to the plugin...")
Shad Ansaria2714652021-10-23 00:10:18 +000077 exec_net = IECore().load_network(network=net, device_name="CPU", num_requests=2)
Shad Ansari30a23732021-09-29 23:07:21 -070078 # Read and pre-process input image
Shad Ansaria2714652021-10-23 00:10:18 +000079 shape = Shape(*net.inputs[input_blob].shape)
Shad Ansari30a23732021-09-29 23:07:21 -070080 del net
Shad Ansarid3654512021-09-29 10:31:53 -070081
Shad Ansaria2714652021-10-23 00:10:18 +000082 return exec_net, shape, input_blob, out_blob
Shad Ansari47432b62021-09-27 22:46:25 +000083
Shad Ansari30a23732021-09-29 23:07:21 -070084
85 def frames(self):
Shad Ansari79615b92021-09-30 11:15:41 -070086
Shad Ansaria2714652021-10-23 00:10:18 +000087 exec_net, shape, input_blob, out_blob = self.init_inference()
88 stream = self.init_stream()
Shad Ansari79615b92021-09-30 11:15:41 -070089
Shad Ansari30a23732021-09-29 23:07:21 -070090 cur_request_id = 0
91 next_request_id = 1
92
93 log.info("Starting inference in async mode...")
94 log.info("To switch between sync and async modes press Tab button")
95 log.info("To stop the demo execution press Esc button")
96
97 # Async doesn't work if True
98 # Request issues = Runtime Error: [REQUEST BUSY]
Shad Ansarief185d22021-10-14 17:49:26 +000099 # self.is_async_mode = False
Shad Ansari30a23732021-09-29 23:07:21 -0700100 render_time = 0
Shad Ansaria2714652021-10-23 00:10:18 +0000101 ret, frame = stream.read()
Shad Ansari30a23732021-09-29 23:07:21 -0700102
103 while True:
104 if self.is_async_mode:
Shad Ansaria2714652021-10-23 00:10:18 +0000105 ret, next_frame = stream.read()
Shad Ansari47432b62021-09-27 22:46:25 +0000106 else:
Shad Ansaria2714652021-10-23 00:10:18 +0000107 ret, frame = stream.read()
Shad Ansari30a23732021-09-29 23:07:21 -0700108 if not ret:
109 break
Shad Ansaria2714652021-10-23 00:10:18 +0000110 initial_w = stream.get(cv2.CAP_PROP_FRAME_WIDTH)
111 initial_h = stream.get(cv2.CAP_PROP_FRAME_HEIGHT)
Shad Ansari47432b62021-09-27 22:46:25 +0000112
Shad Ansari30a23732021-09-29 23:07:21 -0700113 # Main sync point:
114 # in the truly Async mode we start the NEXT infer request, while waiting for the CURRENT to complete
115 # in the regular mode we start the CURRENT request and immediately wait for it's completion
116 inf_start = time.time()
117 if self.is_async_mode:
Shad Ansaria2714652021-10-23 00:10:18 +0000118 in_frame = cv2.resize(next_frame, (shape.w, shape.h))
Shad Ansari341ca3a2021-09-30 12:10:00 -0700119 in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW
Shad Ansaria2714652021-10-23 00:10:18 +0000120 in_frame = in_frame.reshape((shape.n, shape.c, shape.h, shape.w))
121 exec_net.start_async(request_id=next_request_id, inputs={input_blob: in_frame})
Shad Ansari30a23732021-09-29 23:07:21 -0700122 else:
Shad Ansaria2714652021-10-23 00:10:18 +0000123 in_frame = cv2.resize(frame, (shape.w, shape.h))
Shad Ansari341ca3a2021-09-30 12:10:00 -0700124 in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW
Shad Ansaria2714652021-10-23 00:10:18 +0000125 in_frame = in_frame.reshape((shape.n, shape.c, shape.h, shape.w))
126 exec_net.start_async(request_id=cur_request_id, inputs={input_blob: in_frame})
Shad Ansari47432b62021-09-27 22:46:25 +0000127
Shad Ansaria2714652021-10-23 00:10:18 +0000128 if exec_net.requests[cur_request_id].wait(-1) == 0:
Shad Ansari30a23732021-09-29 23:07:21 -0700129 inf_end = time.time()
130 det_time = inf_end - inf_start
Shad Ansari47432b62021-09-27 22:46:25 +0000131
Shad Ansari30a23732021-09-29 23:07:21 -0700132 # Parse detection results of the current request
Shad Ansaria2714652021-10-23 00:10:18 +0000133 res = exec_net.requests[cur_request_id].outputs[out_blob]
Shad Ansari47432b62021-09-27 22:46:25 +0000134
Shad Ansari30a23732021-09-29 23:07:21 -0700135 for obj in res[0][0]:
136 # Draw only objects when probability more than specified threshold
Shad Ansari4ae11682021-10-22 18:51:53 +0000137 if obj[2] > self.prob_threshold:
Shad Ansari30a23732021-09-29 23:07:21 -0700138 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 Ansarief185d22021-10-14 17:49:26 +0000144 color = (0, 0, 255)
Shad Ansari30a23732021-09-29 23:07:21 -0700145 cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), color, 2)
Shad Ansaria2714652021-10-23 00:10:18 +0000146 det_label = str(class_id)
Shad Ansari30a23732021-09-29 23:07:21 -0700147 cv2.putText(frame, det_label + ' ' + str(round(obj[2] * 100, 1)) + ' %', (xmin, ymin - 7),
148 cv2.FONT_HERSHEY_COMPLEX, 0.6, color, 1)
Shad Ansari341ca3a2021-09-30 12:10:00 -0700149 # print('Object detected, class_id:', class_id, 'probability:', obj[2], 'xmin:', xmin, 'ymin:', ymin,
150 # 'xmax:', xmax, 'ymax:', ymax)
Shad Ansari47432b62021-09-27 22:46:25 +0000151
Shad Ansaric0726e62021-10-04 22:38:53 +0000152 cv2.putText(frame, self.device, (10, int(initial_h - 20)),
153 cv2.FONT_HERSHEY_COMPLEX, 0.5, (10, 10, 200), 1)
Shad Ansari30a23732021-09-29 23:07:21 -0700154
155 render_start = time.time()
156
157 yield cv2.imencode('.jpg', frame)[1].tobytes()
158
Shad Ansari341ca3a2021-09-30 12:10:00 -0700159 render_end = time.time()
160 render_time = render_end - render_start
Shad Ansari30a23732021-09-29 23:07:21 -0700161
162 if self.is_async_mode:
163 cur_request_id, next_request_id = next_request_id, cur_request_id
Shad Ansari30a23732021-09-29 23:07:21 -0700164 frame = next_frame