blob: 15c4e82ac98eb21a09a5f2d42384f307c6a293a5 [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 Ansari3a8a3912021-11-18 00:44:50 +000041 mqtt.init(device)
42
Shad Ansari60ca8cc2021-11-02 18:46:44 +000043 super(Camera, self).__init__(device, args.idle)
Shad Ansaria2714652021-10-23 00:10:18 +000044
45 def __del__(self):
46 # stream.release()
47 cv2.destroyAllWindows()
48
49 def init_stream(self):
50 if self.input == 'cam':
51 input_stream = 0
52 elif self.input == 'gstreamer':
Shad Ansarif7633892021-11-02 19:31:00 +000053 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 +000054 else:
55 input_stream = self.input
56 assert os.path.isfile(self.input), "Specified input file doesn't exist"
57
58 if self.input == 'gstreamer':
59 stream = cv2.VideoCapture(input_stream, cv2.CAP_GSTREAMER)
60 else:
61 stream = cv2.VideoCapture(input_stream)
62
63 return stream
64
65
66 def init_inference(self):
Shad Ansaric9f48d32021-10-25 19:03:34 +000067 self.model_bin = os.path.splitext(self.model_xml)[0] + ".bin"
Shad Ansari47432b62021-09-27 22:46:25 +000068
Shad Ansari30a23732021-09-29 23:07:21 -070069 # Read IR
70 log.info("Reading IR...")
Shad Ansaric9f48d32021-10-25 19:03:34 +000071 net = IECore().read_network(model=self.model_xml, weights=self.model_bin)
Shad Ansari47432b62021-09-27 22:46:25 +000072
Shad Ansari30a23732021-09-29 23:07:21 -070073 assert len(net.inputs.keys()) == 1, "Demo supports only single input topologies"
74 assert len(net.outputs) == 1, "Demo supports only single output topologies"
Shad Ansaria2714652021-10-23 00:10:18 +000075 input_blob = next(iter(net.inputs))
76 out_blob = next(iter(net.outputs))
Shad Ansari47432b62021-09-27 22:46:25 +000077
Shad Ansari30a23732021-09-29 23:07:21 -070078 log.info("Loading IR to the plugin...")
Shad Ansaria2714652021-10-23 00:10:18 +000079 exec_net = IECore().load_network(network=net, device_name="CPU", num_requests=2)
Shad Ansari30a23732021-09-29 23:07:21 -070080 # Read and pre-process input image
Shad Ansaria2714652021-10-23 00:10:18 +000081 shape = Shape(*net.inputs[input_blob].shape)
Shad Ansaric9f48d32021-10-25 19:03:34 +000082
Shad Ansari30a23732021-09-29 23:07:21 -070083 del net
Shad Ansarid3654512021-09-29 10:31:53 -070084
Shad Ansaria2714652021-10-23 00:10:18 +000085 return exec_net, shape, input_blob, out_blob
Shad Ansari47432b62021-09-27 22:46:25 +000086
Shad Ansari30a23732021-09-29 23:07:21 -070087
88 def frames(self):
Shad Ansari79615b92021-09-30 11:15:41 -070089
Shad Ansaria2714652021-10-23 00:10:18 +000090 exec_net, shape, input_blob, out_blob = self.init_inference()
91 stream = self.init_stream()
Shad Ansari79615b92021-09-30 11:15:41 -070092
Shad Ansari30a23732021-09-29 23:07:21 -070093 cur_request_id = 0
94 next_request_id = 1
95
Shad Ansaria2714652021-10-23 00:10:18 +000096 ret, frame = stream.read()
Shad Ansari30a23732021-09-29 23:07:21 -070097
98 while True:
99 if self.is_async_mode:
Shad Ansaria2714652021-10-23 00:10:18 +0000100 ret, next_frame = stream.read()
Shad Ansari47432b62021-09-27 22:46:25 +0000101 else:
Shad Ansaria2714652021-10-23 00:10:18 +0000102 ret, frame = stream.read()
Shad Ansari30a23732021-09-29 23:07:21 -0700103 if not ret:
104 break
Shad Ansaria2714652021-10-23 00:10:18 +0000105 initial_w = stream.get(cv2.CAP_PROP_FRAME_WIDTH)
106 initial_h = stream.get(cv2.CAP_PROP_FRAME_HEIGHT)
Shad Ansari47432b62021-09-27 22:46:25 +0000107
Shad Ansari30a23732021-09-29 23:07:21 -0700108 # Main sync point:
109 # in the truly Async mode we start the NEXT infer request, while waiting for the CURRENT to complete
110 # in the regular mode we start the CURRENT request and immediately wait for it's completion
Shad Ansari30a23732021-09-29 23:07:21 -0700111 if self.is_async_mode:
Shad Ansaria2714652021-10-23 00:10:18 +0000112 in_frame = cv2.resize(next_frame, (shape.w, shape.h))
Shad Ansari341ca3a2021-09-30 12:10:00 -0700113 in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW
Shad Ansaria2714652021-10-23 00:10:18 +0000114 in_frame = in_frame.reshape((shape.n, shape.c, shape.h, shape.w))
115 exec_net.start_async(request_id=next_request_id, inputs={input_blob: in_frame})
Shad Ansari30a23732021-09-29 23:07:21 -0700116 else:
Shad Ansaria2714652021-10-23 00:10:18 +0000117 in_frame = cv2.resize(frame, (shape.w, shape.h))
Shad Ansari341ca3a2021-09-30 12:10:00 -0700118 in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW
Shad Ansaria2714652021-10-23 00:10:18 +0000119 in_frame = in_frame.reshape((shape.n, shape.c, shape.h, shape.w))
120 exec_net.start_async(request_id=cur_request_id, inputs={input_blob: in_frame})
Shad Ansari47432b62021-09-27 22:46:25 +0000121
Shad Ansaria2714652021-10-23 00:10:18 +0000122 if exec_net.requests[cur_request_id].wait(-1) == 0:
Shad Ansari47432b62021-09-27 22:46:25 +0000123
Shad Ansari30a23732021-09-29 23:07:21 -0700124 # Parse detection results of the current request
Shad Ansaria2714652021-10-23 00:10:18 +0000125 res = exec_net.requests[cur_request_id].outputs[out_blob]
Shad Ansari47432b62021-09-27 22:46:25 +0000126
Shad Ansari0bee2f82021-10-25 20:07:22 +0000127 initial_w = 640
128 initial_h = 480
129 frame = cv2.resize(frame, (initial_w, initial_h))
130
Shad Ansari51554d82021-10-26 20:42:18 +0000131 obj_count = 0
132 red = (0, 0, 255)
133 black = (0, 0, 0)
Shad Ansari30a23732021-09-29 23:07:21 -0700134 for obj in res[0][0]:
135 # Draw only objects when probability more than specified threshold
Shad Ansari4ae11682021-10-22 18:51:53 +0000136 if obj[2] > self.prob_threshold:
Shad Ansari30a23732021-09-29 23:07:21 -0700137 xmin = int(obj[3] * initial_w)
138 ymin = int(obj[4] * initial_h)
139 xmax = int(obj[5] * initial_w)
140 ymax = int(obj[6] * initial_h)
Shad Ansari51554d82021-10-26 20:42:18 +0000141 # Draw box and prob
142 cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), red, 2)
143 cv2.putText(frame, str(round(obj[2] * 100, 1)) + ' %', (xmin, ymin - 7),
144 cv2.FONT_HERSHEY_COMPLEX, 0.6, black, 1)
145 obj_count += 1
Shad Ansari47432b62021-09-27 22:46:25 +0000146
Shad Ansari51554d82021-10-26 20:42:18 +0000147 cv2.putText(frame, "persons: {}".format(str(obj_count)), (10, 20),
148 cv2.FONT_HERSHEY_COMPLEX, 0.6, black, 1)
149 cv2.putText(frame, "camera: {}".format(self.device), (10, int(initial_h - 20)),
150 cv2.FONT_HERSHEY_COMPLEX, 0.6, black, 1)
Shad Ansari30a23732021-09-29 23:07:21 -0700151
Shad Ansari3a8a3912021-11-18 00:44:50 +0000152 if obj_count > 0:
153 mqtt.person_detected(self.device, obj_count)
154
Shad Ansari30a23732021-09-29 23:07:21 -0700155 yield cv2.imencode('.jpg', frame)[1].tobytes()
156
Shad Ansari30a23732021-09-29 23:07:21 -0700157 if self.is_async_mode:
158 cur_request_id, next_request_id = next_request_id, cur_request_id
Shad Ansari30a23732021-09-29 23:07:21 -0700159 frame = next_frame