Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 1 | """ |
Shad Ansari | f53bc4f | 2022-06-07 14:31:11 -0700 | [diff] [blame] | 2 | SPDX-FileCopyrightText: 2022-present Intel Corporation |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 3 | SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org> |
Shad Ansari | f53bc4f | 2022-06-07 14:31:11 -0700 | [diff] [blame] | 4 | SPDX-License-Identifier: Apache-2.0 |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 5 | """ |
| 6 | |
| 7 | from __future__ import print_function |
| 8 | |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 9 | from collections import namedtuple |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 10 | import logging as log |
| 11 | import os |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 12 | from argparse import ArgumentParser, SUPPRESS |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 13 | from imutils import build_montages |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 14 | |
| 15 | import cv2 |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 16 | from openvino.inference_engine import IECore |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 17 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 18 | from base_camera import BaseCamera |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 19 | |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 20 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 21 | class Camera(BaseCamera): |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame] | 22 | Shape = namedtuple('Shape', ['n','c','h','w']) |
Shad Ansari | d365451 | 2021-09-29 10:31:53 -0700 | [diff] [blame] | 23 | |
Shad Ansari | 2eddc1e | 2022-01-10 17:53:06 +0000 | [diff] [blame] | 24 | def __init__(self, device, client, args): |
Shad Ansari | c9f48d3 | 2021-10-25 19:03:34 +0000 | [diff] [blame] | 25 | self.model_xml = args.model |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 26 | self.input = args.input |
| 27 | self.prob_threshold = args.prob_threshold |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 28 | self.is_async_mode = True |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 29 | self.device = device |
Shad Ansari | 2eddc1e | 2022-01-10 17:53:06 +0000 | [diff] [blame] | 30 | self.client = client |
Shad Ansari | 2948cc0 | 2022-04-14 21:33:37 +0000 | [diff] [blame] | 31 | 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 Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 32 | |
| 33 | def __del__(self): |
| 34 | # stream.release() |
| 35 | cv2.destroyAllWindows() |
| 36 | |
| 37 | def init_stream(self): |
| 38 | if self.input == 'cam': |
| 39 | input_stream = 0 |
| 40 | elif self.input == 'gstreamer': |
Shad Ansari | f763389 | 2021-11-02 19:31:00 +0000 | [diff] [blame] | 41 | 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 Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 42 | else: |
| 43 | input_stream = self.input |
| 44 | assert os.path.isfile(self.input), "Specified input file doesn't exist" |
| 45 | |
| 46 | if self.input == 'gstreamer': |
| 47 | stream = cv2.VideoCapture(input_stream, cv2.CAP_GSTREAMER) |
| 48 | else: |
| 49 | stream = cv2.VideoCapture(input_stream) |
| 50 | |
| 51 | return stream |
| 52 | |
| 53 | |
| 54 | def init_inference(self): |
Shad Ansari | c9f48d3 | 2021-10-25 19:03:34 +0000 | [diff] [blame] | 55 | self.model_bin = os.path.splitext(self.model_xml)[0] + ".bin" |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 56 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 57 | # Read IR |
| 58 | log.info("Reading IR...") |
Shad Ansari | c9f48d3 | 2021-10-25 19:03:34 +0000 | [diff] [blame] | 59 | net = IECore().read_network(model=self.model_xml, weights=self.model_bin) |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 60 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 61 | assert len(net.inputs.keys()) == 1, "Demo supports only single input topologies" |
| 62 | assert len(net.outputs) == 1, "Demo supports only single output topologies" |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 63 | input_blob = next(iter(net.inputs)) |
| 64 | out_blob = next(iter(net.outputs)) |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 65 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 66 | log.info("Loading IR to the plugin...") |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 67 | exec_net = IECore().load_network(network=net, device_name="CPU", num_requests=2) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 68 | # Read and pre-process input image |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame] | 69 | shape = Camera.Shape(*net.inputs[input_blob].shape) |
Shad Ansari | c9f48d3 | 2021-10-25 19:03:34 +0000 | [diff] [blame] | 70 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 71 | del net |
Shad Ansari | d365451 | 2021-09-29 10:31:53 -0700 | [diff] [blame] | 72 | |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 73 | return exec_net, shape, input_blob, out_blob |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 74 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 75 | |
| 76 | def frames(self): |
Shad Ansari | 79615b9 | 2021-09-30 11:15:41 -0700 | [diff] [blame] | 77 | |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 78 | exec_net, shape, input_blob, out_blob = self.init_inference() |
| 79 | stream = self.init_stream() |
Shad Ansari | 79615b9 | 2021-09-30 11:15:41 -0700 | [diff] [blame] | 80 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 81 | cur_request_id = 0 |
| 82 | next_request_id = 1 |
| 83 | |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 84 | ret, frame = stream.read() |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 85 | |
| 86 | while True: |
| 87 | if self.is_async_mode: |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 88 | ret, next_frame = stream.read() |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 89 | else: |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 90 | ret, frame = stream.read() |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 91 | if not ret: |
| 92 | break |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 93 | initial_w = stream.get(cv2.CAP_PROP_FRAME_WIDTH) |
| 94 | initial_h = stream.get(cv2.CAP_PROP_FRAME_HEIGHT) |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 95 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 96 | # Main sync point: |
| 97 | # in the truly Async mode we start the NEXT infer request, while waiting for the CURRENT to complete |
| 98 | # in the regular mode we start the CURRENT request and immediately wait for it's completion |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 99 | if self.is_async_mode: |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 100 | in_frame = cv2.resize(next_frame, (shape.w, shape.h)) |
Shad Ansari | 341ca3a | 2021-09-30 12:10:00 -0700 | [diff] [blame] | 101 | in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 102 | in_frame = in_frame.reshape((shape.n, shape.c, shape.h, shape.w)) |
| 103 | exec_net.start_async(request_id=next_request_id, inputs={input_blob: in_frame}) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 104 | else: |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 105 | in_frame = cv2.resize(frame, (shape.w, shape.h)) |
Shad Ansari | 341ca3a | 2021-09-30 12:10:00 -0700 | [diff] [blame] | 106 | in_frame = in_frame.transpose((2, 0, 1)) # Change data layout from HWC to CHW |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 107 | in_frame = in_frame.reshape((shape.n, shape.c, shape.h, shape.w)) |
| 108 | exec_net.start_async(request_id=cur_request_id, inputs={input_blob: in_frame}) |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 109 | |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 110 | if exec_net.requests[cur_request_id].wait(-1) == 0: |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 111 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 112 | # Parse detection results of the current request |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 113 | res = exec_net.requests[cur_request_id].outputs[out_blob] |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 114 | |
Shad Ansari | 0bee2f8 | 2021-10-25 20:07:22 +0000 | [diff] [blame] | 115 | initial_w = 640 |
| 116 | initial_h = 480 |
| 117 | frame = cv2.resize(frame, (initial_w, initial_h)) |
| 118 | |
Shad Ansari | 51554d8 | 2021-10-26 20:42:18 +0000 | [diff] [blame] | 119 | obj_count = 0 |
| 120 | red = (0, 0, 255) |
| 121 | black = (0, 0, 0) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 122 | for obj in res[0][0]: |
| 123 | # Draw only objects when probability more than specified threshold |
Shad Ansari | 4ae1168 | 2021-10-22 18:51:53 +0000 | [diff] [blame] | 124 | if obj[2] > self.prob_threshold: |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 125 | xmin = int(obj[3] * initial_w) |
| 126 | ymin = int(obj[4] * initial_h) |
| 127 | xmax = int(obj[5] * initial_w) |
| 128 | ymax = int(obj[6] * initial_h) |
Shad Ansari | 51554d8 | 2021-10-26 20:42:18 +0000 | [diff] [blame] | 129 | # Draw box and prob |
| 130 | cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), red, 2) |
| 131 | cv2.putText(frame, str(round(obj[2] * 100, 1)) + ' %', (xmin, ymin - 7), |
| 132 | cv2.FONT_HERSHEY_COMPLEX, 0.6, black, 1) |
| 133 | obj_count += 1 |
Shad Ansari | 47432b6 | 2021-09-27 22:46:25 +0000 | [diff] [blame] | 134 | |
Shad Ansari | 51554d8 | 2021-10-26 20:42:18 +0000 | [diff] [blame] | 135 | cv2.putText(frame, "persons: {}".format(str(obj_count)), (10, 20), |
| 136 | cv2.FONT_HERSHEY_COMPLEX, 0.6, black, 1) |
| 137 | cv2.putText(frame, "camera: {}".format(self.device), (10, int(initial_h - 20)), |
| 138 | cv2.FONT_HERSHEY_COMPLEX, 0.6, black, 1) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 139 | |
Shad Ansari | 3a8a391 | 2021-11-18 00:44:50 +0000 | [diff] [blame] | 140 | if obj_count > 0: |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame] | 141 | self.person_detected(obj_count) |
Shad Ansari | 3a8a391 | 2021-11-18 00:44:50 +0000 | [diff] [blame] | 142 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 143 | yield cv2.imencode('.jpg', frame)[1].tobytes() |
| 144 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 145 | if self.is_async_mode: |
| 146 | cur_request_id, next_request_id = next_request_id, cur_request_id |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 147 | frame = next_frame |