blob: 7cdfd9f1ccd48f40f72bdb344b6d9e1bb7006748 [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
Shad Ansari47432b62021-09-27 22:46:25 +000012from argparse import ArgumentParser, SUPPRESS
Shad Ansari47432b62021-09-27 22:46:25 +000013from imutils import build_montages
Shad Ansaria2714652021-10-23 00:10:18 +000014
15import cv2
Shad Ansari47432b62021-09-27 22:46:25 +000016from openvino.inference_engine import IECore
Shad Ansaria2714652021-10-23 00:10:18 +000017
Shad Ansari30a23732021-09-29 23:07:21 -070018from base_camera import BaseCamera
Shad Ansari47432b62021-09-27 22:46:25 +000019
Shad Ansaria2714652021-10-23 00:10:18 +000020Shape = namedtuple('Shape', ['n','c','h','w'])
Shad Ansari47432b62021-09-27 22:46:25 +000021
Shad Ansari30a23732021-09-29 23:07:21 -070022class Camera(BaseCamera):
Shad Ansaria2714652021-10-23 00:10:18 +000023 model = None
24 prob_threshold = 0.0
25 input = None
26 device = None
Shad Ansarid3654512021-09-29 10:31:53 -070027
Shad Ansaric0726e62021-10-04 22:38:53 +000028 def __init__(self, device, args):
Shad Ansari30a23732021-09-29 23:07:21 -070029 log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout)
Shad Ansaria2714652021-10-23 00:10:18 +000030
Shad Ansaric9f48d32021-10-25 19:03:34 +000031 self.model_xml = args.model
Shad Ansaria2714652021-10-23 00:10:18 +000032 self.input = args.input
33 self.prob_threshold = args.prob_threshold
34
35 self.is_async_mode = True
36
37 self.device = device
38
39 super(Camera, self).__init__(device, args.idle)
40
41 def __del__(self):
42 # stream.release()
43 cv2.destroyAllWindows()
44
45 def init_stream(self):
46 if self.input == 'cam':
47 input_stream = 0
48 elif self.input == 'gstreamer':
49 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'
50 else:
51 input_stream = self.input
52 assert os.path.isfile(self.input), "Specified input file doesn't exist"
53
54 if self.input == 'gstreamer':
55 stream = cv2.VideoCapture(input_stream, cv2.CAP_GSTREAMER)
56 else:
57 stream = cv2.VideoCapture(input_stream)
58
59 return stream
60
61
62 def init_inference(self):
Shad Ansaric9f48d32021-10-25 19:03:34 +000063 self.model_bin = os.path.splitext(self.model_xml)[0] + ".bin"
Shad Ansari47432b62021-09-27 22:46:25 +000064
Shad Ansari30a23732021-09-29 23:07:21 -070065 # Read IR
66 log.info("Reading IR...")
Shad Ansaric9f48d32021-10-25 19:03:34 +000067 net = IECore().read_network(model=self.model_xml, weights=self.model_bin)
Shad Ansari47432b62021-09-27 22:46:25 +000068
Shad Ansari30a23732021-09-29 23:07:21 -070069 assert len(net.inputs.keys()) == 1, "Demo supports only single input topologies"
70 assert len(net.outputs) == 1, "Demo supports only single output topologies"
Shad Ansaria2714652021-10-23 00:10:18 +000071 input_blob = next(iter(net.inputs))
72 out_blob = next(iter(net.outputs))
Shad Ansari47432b62021-09-27 22:46:25 +000073
Shad Ansari30a23732021-09-29 23:07:21 -070074 log.info("Loading IR to the plugin...")
Shad Ansaria2714652021-10-23 00:10:18 +000075 exec_net = IECore().load_network(network=net, device_name="CPU", num_requests=2)
Shad Ansari30a23732021-09-29 23:07:21 -070076 # Read and pre-process input image
Shad Ansaria2714652021-10-23 00:10:18 +000077 shape = Shape(*net.inputs[input_blob].shape)
Shad Ansaric9f48d32021-10-25 19:03:34 +000078
Shad Ansari30a23732021-09-29 23:07:21 -070079 del net
Shad Ansarid3654512021-09-29 10:31:53 -070080
Shad Ansaria2714652021-10-23 00:10:18 +000081 return exec_net, shape, input_blob, out_blob
Shad Ansari47432b62021-09-27 22:46:25 +000082
Shad Ansari30a23732021-09-29 23:07:21 -070083
84 def frames(self):
Shad Ansari79615b92021-09-30 11:15:41 -070085
Shad Ansaria2714652021-10-23 00:10:18 +000086 exec_net, shape, input_blob, out_blob = self.init_inference()
87 stream = self.init_stream()
Shad Ansari79615b92021-09-30 11:15:41 -070088
Shad Ansari30a23732021-09-29 23:07:21 -070089 cur_request_id = 0
90 next_request_id = 1
91
Shad Ansaria2714652021-10-23 00:10:18 +000092 ret, frame = stream.read()
Shad Ansari30a23732021-09-29 23:07:21 -070093
94 while True:
95 if self.is_async_mode:
Shad Ansaria2714652021-10-23 00:10:18 +000096 ret, next_frame = stream.read()
Shad Ansari47432b62021-09-27 22:46:25 +000097 else:
Shad Ansaria2714652021-10-23 00:10:18 +000098 ret, frame = stream.read()
Shad Ansari30a23732021-09-29 23:07:21 -070099 if not ret:
100 break
Shad Ansaria2714652021-10-23 00:10:18 +0000101 initial_w = stream.get(cv2.CAP_PROP_FRAME_WIDTH)
102 initial_h = stream.get(cv2.CAP_PROP_FRAME_HEIGHT)
Shad Ansari47432b62021-09-27 22:46:25 +0000103
Shad Ansari30a23732021-09-29 23:07:21 -0700104 # Main sync point:
105 # in the truly Async mode we start the NEXT infer request, while waiting for the CURRENT to complete
106 # in the regular mode we start the CURRENT request and immediately wait for it's completion
Shad Ansari30a23732021-09-29 23:07:21 -0700107 if self.is_async_mode:
Shad Ansaria2714652021-10-23 00:10:18 +0000108 in_frame = cv2.resize(next_frame, (shape.w, shape.h))
Shad Ansari341ca3a2021-09-30 12:10:00 -0700109 in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW
Shad Ansaria2714652021-10-23 00:10:18 +0000110 in_frame = in_frame.reshape((shape.n, shape.c, shape.h, shape.w))
111 exec_net.start_async(request_id=next_request_id, inputs={input_blob: in_frame})
Shad Ansari30a23732021-09-29 23:07:21 -0700112 else:
Shad Ansaria2714652021-10-23 00:10:18 +0000113 in_frame = cv2.resize(frame, (shape.w, shape.h))
Shad Ansari341ca3a2021-09-30 12:10:00 -0700114 in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW
Shad Ansaria2714652021-10-23 00:10:18 +0000115 in_frame = in_frame.reshape((shape.n, shape.c, shape.h, shape.w))
116 exec_net.start_async(request_id=cur_request_id, inputs={input_blob: in_frame})
Shad Ansari47432b62021-09-27 22:46:25 +0000117
Shad Ansaria2714652021-10-23 00:10:18 +0000118 if exec_net.requests[cur_request_id].wait(-1) == 0:
Shad Ansari47432b62021-09-27 22:46:25 +0000119
Shad Ansari30a23732021-09-29 23:07:21 -0700120 # Parse detection results of the current request
Shad Ansaria2714652021-10-23 00:10:18 +0000121 res = exec_net.requests[cur_request_id].outputs[out_blob]
Shad Ansari47432b62021-09-27 22:46:25 +0000122
Shad Ansari0bee2f82021-10-25 20:07:22 +0000123 initial_w = 640
124 initial_h = 480
125 frame = cv2.resize(frame, (initial_w, initial_h))
126
Shad Ansari30a23732021-09-29 23:07:21 -0700127 for obj in res[0][0]:
128 # Draw only objects when probability more than specified threshold
Shad Ansari4ae11682021-10-22 18:51:53 +0000129 if obj[2] > self.prob_threshold:
Shad Ansari30a23732021-09-29 23:07:21 -0700130 xmin = int(obj[3] * initial_w)
131 ymin = int(obj[4] * initial_h)
132 xmax = int(obj[5] * initial_w)
133 ymax = int(obj[6] * initial_h)
134 class_id = int(obj[1])
135 # Draw box and label\class_id
Shad Ansarief185d22021-10-14 17:49:26 +0000136 color = (0, 0, 255)
Shad Ansari30a23732021-09-29 23:07:21 -0700137 cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), color, 2)
Shad Ansaria2714652021-10-23 00:10:18 +0000138 det_label = str(class_id)
Shad Ansari30a23732021-09-29 23:07:21 -0700139 cv2.putText(frame, det_label + ' ' + str(round(obj[2] * 100, 1)) + ' %', (xmin, ymin - 7),
140 cv2.FONT_HERSHEY_COMPLEX, 0.6, color, 1)
Shad Ansari47432b62021-09-27 22:46:25 +0000141
Shad Ansaric0726e62021-10-04 22:38:53 +0000142 cv2.putText(frame, self.device, (10, int(initial_h - 20)),
143 cv2.FONT_HERSHEY_COMPLEX, 0.5, (10, 10, 200), 1)
Shad Ansari30a23732021-09-29 23:07:21 -0700144
Shad Ansari30a23732021-09-29 23:07:21 -0700145 yield cv2.imencode('.jpg', frame)[1].tobytes()
146
Shad Ansari30a23732021-09-29 23:07:21 -0700147 if self.is_async_mode:
148 cur_request_id, next_request_id = next_request_id, cur_request_id
Shad Ansari30a23732021-09-29 23:07:21 -0700149 frame = next_frame