blob: 13478e0f36280f230c43965c3d337095a906bcbb [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
Shad Ansari47432b62021-09-27 22:46:25 +000011from argparse import ArgumentParser, SUPPRESS
Shad Ansari47432b62021-09-27 22:46:25 +000012from imutils import build_montages
Shad Ansaria2714652021-10-23 00:10:18 +000013
14import cv2
Shad Ansari47432b62021-09-27 22:46:25 +000015from openvino.inference_engine import IECore
Shad Ansaria2714652021-10-23 00:10:18 +000016
Shad Ansari30a23732021-09-29 23:07:21 -070017from base_camera import BaseCamera
Shad Ansari47432b62021-09-27 22:46:25 +000018
Shad Ansari47432b62021-09-27 22:46:25 +000019
Shad Ansari30a23732021-09-29 23:07:21 -070020class Camera(BaseCamera):
Shad Ansari5e8d0692021-12-08 19:09:34 +000021 Shape = namedtuple('Shape', ['n','c','h','w'])
Shad Ansarid3654512021-09-29 10:31:53 -070022
Shad Ansari2eddc1e2022-01-10 17:53:06 +000023 def __init__(self, device, client, args):
Shad Ansaric9f48d32021-10-25 19:03:34 +000024 self.model_xml = args.model
Shad Ansaria2714652021-10-23 00:10:18 +000025 self.input = args.input
26 self.prob_threshold = args.prob_threshold
Shad Ansaria2714652021-10-23 00:10:18 +000027 self.is_async_mode = True
Shad Ansaria2714652021-10-23 00:10:18 +000028 self.device = device
Shad Ansari2eddc1e2022-01-10 17:53:06 +000029 self.client = client
Shad Ansari2948cc02022-04-14 21:33:37 +000030 super(Camera, self).__init__(device, args.url, args.keycloak, args.enterprise, args.site, args.user, args.password, args.mbrlow, args.mbrhigh, args.devicegroup, args.noroc)
Shad Ansaria2714652021-10-23 00:10:18 +000031
32 def __del__(self):
33 # stream.release()
34 cv2.destroyAllWindows()
35
36 def init_stream(self):
37 if self.input == 'cam':
38 input_stream = 0
39 elif self.input == 'gstreamer':
Shad Ansarif7633892021-11-02 19:31:00 +000040 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 +000041 else:
42 input_stream = self.input
43 assert os.path.isfile(self.input), "Specified input file doesn't exist"
44
45 if self.input == 'gstreamer':
46 stream = cv2.VideoCapture(input_stream, cv2.CAP_GSTREAMER)
47 else:
48 stream = cv2.VideoCapture(input_stream)
49
50 return stream
51
52
53 def init_inference(self):
Shad Ansaric9f48d32021-10-25 19:03:34 +000054 self.model_bin = os.path.splitext(self.model_xml)[0] + ".bin"
Shad Ansari47432b62021-09-27 22:46:25 +000055
Shad Ansari30a23732021-09-29 23:07:21 -070056 # Read IR
57 log.info("Reading IR...")
Shad Ansaric9f48d32021-10-25 19:03:34 +000058 net = IECore().read_network(model=self.model_xml, weights=self.model_bin)
Shad Ansari47432b62021-09-27 22:46:25 +000059
Shad Ansari30a23732021-09-29 23:07:21 -070060 assert len(net.inputs.keys()) == 1, "Demo supports only single input topologies"
61 assert len(net.outputs) == 1, "Demo supports only single output topologies"
Shad Ansaria2714652021-10-23 00:10:18 +000062 input_blob = next(iter(net.inputs))
63 out_blob = next(iter(net.outputs))
Shad Ansari47432b62021-09-27 22:46:25 +000064
Shad Ansari30a23732021-09-29 23:07:21 -070065 log.info("Loading IR to the plugin...")
Shad Ansaria2714652021-10-23 00:10:18 +000066 exec_net = IECore().load_network(network=net, device_name="CPU", num_requests=2)
Shad Ansari30a23732021-09-29 23:07:21 -070067 # Read and pre-process input image
Shad Ansari5e8d0692021-12-08 19:09:34 +000068 shape = Camera.Shape(*net.inputs[input_blob].shape)
Shad Ansaric9f48d32021-10-25 19:03:34 +000069
Shad Ansari30a23732021-09-29 23:07:21 -070070 del net
Shad Ansarid3654512021-09-29 10:31:53 -070071
Shad Ansaria2714652021-10-23 00:10:18 +000072 return exec_net, shape, input_blob, out_blob
Shad Ansari47432b62021-09-27 22:46:25 +000073
Shad Ansari30a23732021-09-29 23:07:21 -070074
75 def frames(self):
Shad Ansari79615b92021-09-30 11:15:41 -070076
Shad Ansaria2714652021-10-23 00:10:18 +000077 exec_net, shape, input_blob, out_blob = self.init_inference()
78 stream = self.init_stream()
Shad Ansari79615b92021-09-30 11:15:41 -070079
Shad Ansari30a23732021-09-29 23:07:21 -070080 cur_request_id = 0
81 next_request_id = 1
82
Shad Ansaria2714652021-10-23 00:10:18 +000083 ret, frame = stream.read()
Shad Ansari30a23732021-09-29 23:07:21 -070084
85 while True:
86 if self.is_async_mode:
Shad Ansaria2714652021-10-23 00:10:18 +000087 ret, next_frame = stream.read()
Shad Ansari47432b62021-09-27 22:46:25 +000088 else:
Shad Ansaria2714652021-10-23 00:10:18 +000089 ret, frame = stream.read()
Shad Ansari30a23732021-09-29 23:07:21 -070090 if not ret:
91 break
Shad Ansaria2714652021-10-23 00:10:18 +000092 initial_w = stream.get(cv2.CAP_PROP_FRAME_WIDTH)
93 initial_h = stream.get(cv2.CAP_PROP_FRAME_HEIGHT)
Shad Ansari47432b62021-09-27 22:46:25 +000094
Shad Ansari30a23732021-09-29 23:07:21 -070095 # Main sync point:
96 # in the truly Async mode we start the NEXT infer request, while waiting for the CURRENT to complete
97 # in the regular mode we start the CURRENT request and immediately wait for it's completion
Shad Ansari30a23732021-09-29 23:07:21 -070098 if self.is_async_mode:
Shad Ansaria2714652021-10-23 00:10:18 +000099 in_frame = cv2.resize(next_frame, (shape.w, shape.h))
Shad Ansari341ca3a2021-09-30 12:10:00 -0700100 in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW
Shad Ansaria2714652021-10-23 00:10:18 +0000101 in_frame = in_frame.reshape((shape.n, shape.c, shape.h, shape.w))
102 exec_net.start_async(request_id=next_request_id, inputs={input_blob: in_frame})
Shad Ansari30a23732021-09-29 23:07:21 -0700103 else:
Shad Ansaria2714652021-10-23 00:10:18 +0000104 in_frame = cv2.resize(frame, (shape.w, shape.h))
Shad Ansari341ca3a2021-09-30 12:10:00 -0700105 in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW
Shad Ansaria2714652021-10-23 00:10:18 +0000106 in_frame = in_frame.reshape((shape.n, shape.c, shape.h, shape.w))
107 exec_net.start_async(request_id=cur_request_id, inputs={input_blob: in_frame})
Shad Ansari47432b62021-09-27 22:46:25 +0000108
Shad Ansaria2714652021-10-23 00:10:18 +0000109 if exec_net.requests[cur_request_id].wait(-1) == 0:
Shad Ansari47432b62021-09-27 22:46:25 +0000110
Shad Ansari30a23732021-09-29 23:07:21 -0700111 # Parse detection results of the current request
Shad Ansaria2714652021-10-23 00:10:18 +0000112 res = exec_net.requests[cur_request_id].outputs[out_blob]
Shad Ansari47432b62021-09-27 22:46:25 +0000113
Shad Ansari0bee2f82021-10-25 20:07:22 +0000114 initial_w = 640
115 initial_h = 480
116 frame = cv2.resize(frame, (initial_w, initial_h))
117
Shad Ansari51554d82021-10-26 20:42:18 +0000118 obj_count = 0
119 red = (0, 0, 255)
120 black = (0, 0, 0)
Shad Ansari30a23732021-09-29 23:07:21 -0700121 for obj in res[0][0]:
122 # Draw only objects when probability more than specified threshold
Shad Ansari4ae11682021-10-22 18:51:53 +0000123 if obj[2] > self.prob_threshold:
Shad Ansari30a23732021-09-29 23:07:21 -0700124 xmin = int(obj[3] * initial_w)
125 ymin = int(obj[4] * initial_h)
126 xmax = int(obj[5] * initial_w)
127 ymax = int(obj[6] * initial_h)
Shad Ansari51554d82021-10-26 20:42:18 +0000128 # Draw box and prob
129 cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), red, 2)
130 cv2.putText(frame, str(round(obj[2] * 100, 1)) + ' %', (xmin, ymin - 7),
131 cv2.FONT_HERSHEY_COMPLEX, 0.6, black, 1)
132 obj_count += 1
Shad Ansari47432b62021-09-27 22:46:25 +0000133
Shad Ansari51554d82021-10-26 20:42:18 +0000134 cv2.putText(frame, "persons: {}".format(str(obj_count)), (10, 20),
135 cv2.FONT_HERSHEY_COMPLEX, 0.6, black, 1)
136 cv2.putText(frame, "camera: {}".format(self.device), (10, int(initial_h - 20)),
137 cv2.FONT_HERSHEY_COMPLEX, 0.6, black, 1)
Shad Ansari30a23732021-09-29 23:07:21 -0700138
Shad Ansari3a8a3912021-11-18 00:44:50 +0000139 if obj_count > 0:
Shad Ansari5e8d0692021-12-08 19:09:34 +0000140 self.person_detected(obj_count)
Shad Ansari3a8a3912021-11-18 00:44:50 +0000141
Shad Ansari30a23732021-09-29 23:07:21 -0700142 yield cv2.imencode('.jpg', frame)[1].tobytes()
143
Shad Ansari30a23732021-09-29 23:07:21 -0700144 if self.is_async_mode:
145 cur_request_id, next_request_id = next_request_id, cur_request_id
Shad Ansari30a23732021-09-29 23:07:21 -0700146 frame = next_frame