blob: 92a5e4a35431e2b68924d6f471f1db0c877c0b96 [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 Ansari3a8a3912021-11-18 00:44:50 +000020import mqtt
21
Shad Ansaria2714652021-10-23 00:10:18 +000022Shape = namedtuple('Shape', ['n','c','h','w'])
Shad Ansari47432b62021-09-27 22:46:25 +000023
Shad Ansari30a23732021-09-29 23:07:21 -070024class Camera(BaseCamera):
Shad Ansaria2714652021-10-23 00:10:18 +000025 model = None
26 prob_threshold = 0.0
27 input = None
28 device = None
Shad Ansarid3654512021-09-29 10:31:53 -070029
Shad Ansaric0726e62021-10-04 22:38:53 +000030 def __init__(self, device, args):
Shad Ansari30a23732021-09-29 23:07:21 -070031 log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout)
Shad Ansaria2714652021-10-23 00:10:18 +000032
Shad Ansaric9f48d32021-10-25 19:03:34 +000033 self.model_xml = args.model
Shad Ansaria2714652021-10-23 00:10:18 +000034 self.input = args.input
35 self.prob_threshold = args.prob_threshold
36
37 self.is_async_mode = True
38
39 self.device = device
40
Shad Ansari60ca8cc2021-11-02 18:46:44 +000041 super(Camera, self).__init__(device, args.idle)
Shad Ansaria2714652021-10-23 00:10:18 +000042
43 def __del__(self):
44 # stream.release()
45 cv2.destroyAllWindows()
46
47 def init_stream(self):
48 if self.input == 'cam':
49 input_stream = 0
50 elif self.input == 'gstreamer':
Shad Ansarif7633892021-11-02 19:31:00 +000051 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 output-corrupt=false ! videoconvert ! appsink'
Shad Ansaria2714652021-10-23 00:10:18 +000052 else:
53 input_stream = self.input
54 assert os.path.isfile(self.input), "Specified input file doesn't exist"
55
56 if self.input == 'gstreamer':
57 stream = cv2.VideoCapture(input_stream, cv2.CAP_GSTREAMER)
58 else:
59 stream = cv2.VideoCapture(input_stream)
60
61 return stream
62
63
64 def init_inference(self):
Shad Ansaric9f48d32021-10-25 19:03:34 +000065 self.model_bin = os.path.splitext(self.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...")
Shad Ansaric9f48d32021-10-25 19:03:34 +000069 net = IECore().read_network(model=self.model_xml, weights=self.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 Ansaric9f48d32021-10-25 19:03:34 +000080
Shad Ansari30a23732021-09-29 23:07:21 -070081 del net
Shad Ansarid3654512021-09-29 10:31:53 -070082
Shad Ansaria2714652021-10-23 00:10:18 +000083 return exec_net, shape, input_blob, out_blob
Shad Ansari47432b62021-09-27 22:46:25 +000084
Shad Ansari30a23732021-09-29 23:07:21 -070085
86 def frames(self):
Shad Ansari79615b92021-09-30 11:15:41 -070087
Shad Ansaria2714652021-10-23 00:10:18 +000088 exec_net, shape, input_blob, out_blob = self.init_inference()
89 stream = self.init_stream()
Shad Ansari79615b92021-09-30 11:15:41 -070090
Shad Ansari30a23732021-09-29 23:07:21 -070091 cur_request_id = 0
92 next_request_id = 1
93
Shad Ansaria2714652021-10-23 00:10:18 +000094 ret, frame = stream.read()
Shad Ansari30a23732021-09-29 23:07:21 -070095
96 while True:
97 if self.is_async_mode:
Shad Ansaria2714652021-10-23 00:10:18 +000098 ret, next_frame = stream.read()
Shad Ansari47432b62021-09-27 22:46:25 +000099 else:
Shad Ansaria2714652021-10-23 00:10:18 +0000100 ret, frame = stream.read()
Shad Ansari30a23732021-09-29 23:07:21 -0700101 if not ret:
102 break
Shad Ansaria2714652021-10-23 00:10:18 +0000103 initial_w = stream.get(cv2.CAP_PROP_FRAME_WIDTH)
104 initial_h = stream.get(cv2.CAP_PROP_FRAME_HEIGHT)
Shad Ansari47432b62021-09-27 22:46:25 +0000105
Shad Ansari30a23732021-09-29 23:07:21 -0700106 # Main sync point:
107 # in the truly Async mode we start the NEXT infer request, while waiting for the CURRENT to complete
108 # in the regular mode we start the CURRENT request and immediately wait for it's completion
Shad Ansari30a23732021-09-29 23:07:21 -0700109 if self.is_async_mode:
Shad Ansaria2714652021-10-23 00:10:18 +0000110 in_frame = cv2.resize(next_frame, (shape.w, shape.h))
Shad Ansari341ca3a2021-09-30 12:10:00 -0700111 in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW
Shad Ansaria2714652021-10-23 00:10:18 +0000112 in_frame = in_frame.reshape((shape.n, shape.c, shape.h, shape.w))
113 exec_net.start_async(request_id=next_request_id, inputs={input_blob: in_frame})
Shad Ansari30a23732021-09-29 23:07:21 -0700114 else:
Shad Ansaria2714652021-10-23 00:10:18 +0000115 in_frame = cv2.resize(frame, (shape.w, shape.h))
Shad Ansari341ca3a2021-09-30 12:10:00 -0700116 in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW
Shad Ansaria2714652021-10-23 00:10:18 +0000117 in_frame = in_frame.reshape((shape.n, shape.c, shape.h, shape.w))
118 exec_net.start_async(request_id=cur_request_id, inputs={input_blob: in_frame})
Shad Ansari47432b62021-09-27 22:46:25 +0000119
Shad Ansaria2714652021-10-23 00:10:18 +0000120 if exec_net.requests[cur_request_id].wait(-1) == 0:
Shad Ansari47432b62021-09-27 22:46:25 +0000121
Shad Ansari30a23732021-09-29 23:07:21 -0700122 # Parse detection results of the current request
Shad Ansaria2714652021-10-23 00:10:18 +0000123 res = exec_net.requests[cur_request_id].outputs[out_blob]
Shad Ansari47432b62021-09-27 22:46:25 +0000124
Shad Ansari0bee2f82021-10-25 20:07:22 +0000125 initial_w = 640
126 initial_h = 480
127 frame = cv2.resize(frame, (initial_w, initial_h))
128
Shad Ansari51554d82021-10-26 20:42:18 +0000129 obj_count = 0
130 red = (0, 0, 255)
131 black = (0, 0, 0)
Shad Ansari30a23732021-09-29 23:07:21 -0700132 for obj in res[0][0]:
133 # Draw only objects when probability more than specified threshold
Shad Ansari4ae11682021-10-22 18:51:53 +0000134 if obj[2] > self.prob_threshold:
Shad Ansari30a23732021-09-29 23:07:21 -0700135 xmin = int(obj[3] * initial_w)
136 ymin = int(obj[4] * initial_h)
137 xmax = int(obj[5] * initial_w)
138 ymax = int(obj[6] * initial_h)
Shad Ansari51554d82021-10-26 20:42:18 +0000139 # Draw box and prob
140 cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), red, 2)
141 cv2.putText(frame, str(round(obj[2] * 100, 1)) + ' %', (xmin, ymin - 7),
142 cv2.FONT_HERSHEY_COMPLEX, 0.6, black, 1)
143 obj_count += 1
Shad Ansari47432b62021-09-27 22:46:25 +0000144
Shad Ansari51554d82021-10-26 20:42:18 +0000145 cv2.putText(frame, "persons: {}".format(str(obj_count)), (10, 20),
146 cv2.FONT_HERSHEY_COMPLEX, 0.6, black, 1)
147 cv2.putText(frame, "camera: {}".format(self.device), (10, int(initial_h - 20)),
148 cv2.FONT_HERSHEY_COMPLEX, 0.6, black, 1)
Shad Ansari30a23732021-09-29 23:07:21 -0700149
Shad Ansari3a8a3912021-11-18 00:44:50 +0000150 if obj_count > 0:
151 mqtt.person_detected(self.device, obj_count)
152
Shad Ansari30a23732021-09-29 23:07:21 -0700153 yield cv2.imencode('.jpg', frame)[1].tobytes()
154
Shad Ansari30a23732021-09-29 23:07:21 -0700155 if self.is_async_mode:
156 cur_request_id, next_request_id = next_request_id, cur_request_id
Shad Ansari30a23732021-09-29 23:07:21 -0700157 frame = next_frame