-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
296 lines (256 loc) · 9.97 KB
/
main.py
File metadata and controls
296 lines (256 loc) · 9.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"""People Counter."""
"""
Copyright (c) 2018 Intel Corporation.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit person to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIE OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import os
import sys
import time
import math
import socket
import json
import cv2
import numpy as np
import logging as log
import paho.mqtt.client as mqtt
from argparse import ArgumentParser
from inference import Network
# MQTT server environment variables
HOSTNAME = socket.gethostname()
IPADDRESS = socket.gethostbyname(HOSTNAME)
MQTT_HOST = IPADDRESS
MQTT_PORT = 3001
MQTT_KEEPALIVE_INTERVAL = 60
anchors = [
10, 13, 16, 30, 33, 23, 30, 61, 62, 45, 59, 119, 116, 90, 156, 198, 373,
326
]
classes = 80
class DetectionObservation():
time_found = 0.0
last_updated = 0.0
def __init__(self, xmin, ymin, xmax, ymax, conf, label):
self.confidence = conf
self.class_id = label
self.xmin = int(xmin)
self.ymin = int(ymin)
self.xmax = int(xmax)
self.ymax = int(ymax)
def get_area(self):
area = (self.xmax - self.xmin) * (self.ymax - self.ymin)
return area
def calc_iou(self, rect):
min_X = min(self.xmax, rect.xmax)
max_X = max(self.xmin, rect.xmin)
min_Y = min(self.ymax, rect.ymax)
max_Y = max(self.ymin, rect.ymin)
area_of_intersection = abs((max_X - min_X) * (max_Y - min_Y))
if area_of_intersection == 0:
return 0
iou = area_of_intersection / float(rect.get_area() + self.get_area() -
area_of_intersection)
return iou
def parseResult(result, threshold, w_scale, h_scale):
predictions = result[0][0]
observations = []
for p in predictions:
conf = p[2]
if conf > threshold:
label = p[1]
xmin = p[3] * w_scale
ymin = p[4] * h_scale
xmax = p[5] * w_scale
ymax = p[6] * h_scale
obs = DetectionObservation(xmin, ymin, xmax, ymax, conf, label)
observations.append(obs)
return observations
def build_argparser():
"""
Parse command line arguments.
:return: command line arguments
"""
parser = ArgumentParser()
parser.add_argument("-m",
"--model",
required=True,
type=str,
help="Path to an xml file with a trained model.")
parser.add_argument("-i",
"--input",
required=True,
type=str,
help="Path to image or video file")
parser.add_argument("-l",
"--cpu_extension",
required=False,
type=str,
default=None,
help="MKLDNN (CPU)-targeted custom layers."
"Absolute path to a shared library with the"
"kernels impl.")
parser.add_argument("-d",
"--device",
type=str,
default="CPU",
help="Specify the target device to infer on: "
"CPU, GPU, FPGA or MYRIAD is acceptable. Sample "
"will look for a suitable plugin for device "
"specified (CPU by default)")
parser.add_argument("-pt",
"--prob_threshold",
type=float,
default=0.5,
help="Probability threshold for detections filtering"
"(0.5 by default)")
return parser
def connect_mqtt():
### TODO: Connect to the MQTT client ###
client = mqtt.Client()
client.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
return client
def infer_on_stream(args, client):
"""
Initialize the inference network, stream video to network,
and output stats and video.
:param args: Command line arguments parsed by `build_argparser()`
:param client: MQTT client
:return: None
"""
# Initialise the class
infer_network = Network()
# Set Probability threshold for detections
prob_threshold = args.prob_threshold
### TODO: Load the model through `infer_network` ###
infer_network.load_model(args.model, args.cpu_extension, args.device)
# Set as camera stream
input = 0
isImage = False
base, ext = os.path.splitext(args.input)
if ext.lower() in ('.jpg', '.png', '.bmp'):
input = args.input
isImage = True
elif ext.lower() in ('.mp4'):
input = args.input
cap = cv2.VideoCapture(input)
cap.open(args.input)
cap_w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
cap_h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# get frame inforamtion
fps = cap.get(cv2.CAP_PROP_FPS)
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
input_shape = infer_network.get_input_shape()
num = input_shape[0]
channel = input_shape[1]
height = input_shape[2]
width = input_shape[3]
frame_count = 0
people_count = 0
found_people = []
total_inference_time = []
while cap.isOpened():
people_in_frame = 0
frame_count += 1
flag, frame = cap.read()
curr_time = frame_count / fps
if not flag:
break
key_pressed = cv2.waitKey(60)
p_frame = cv2.resize(frame, (width, height))
p_frame = p_frame.transpose((2, 0, 1))
p_frame = p_frame.reshape(num, channel, height, width)
cv2.cvtColor(p_frame, cv2.COLOR_RGB2BGR)
inference_time = time.time()
infer_network.exec_net(0, p_frame)
if infer_network.wait() == 0:
observations = []
outputs = infer_network.get_output()
total_inference_time.append(time.time() - inference_time)
# print(np.mean(total_inference_time))
# print(curr_time)
for result in outputs:
observations = parseResult(result, prob_threshold, cap_w,
cap_h)
# Remove observations from results that overlap
for idx_x, obs_x in enumerate(observations):
if obs_x.confidence <= 0:
continue
for idx_y, obs_y in enumerate(observations[idx_x + 1:]):
# Check the intersection over union to find area of overlapping area
intersection = obs_x.calc_iou(obs_y)
if intersection >= 0.3:
observations[idx_y].confidence = 0
for obs in observations:
people_in_frame += 1
if (obs.confidence > prob_threshold):
found_person = True
for idx, person in enumerate(found_people):
# Check if observation is same as previously founder person.
intersection = obs.calc_iou(person)
# Check previous person is overlapping
if intersection >= 0.3:
# Found previous person, update position
obs.time_found = person.time_found
obs.last_updated = curr_time
found_people[idx] = obs
found_person = False
break
# Add found person to list of activty observations
if found_person or len(found_people) == 0:
obs.time_found = curr_time
obs.last_updated = curr_time
found_people.append(obs)
people_count += 1
# Filter out people that have not been in the frame for 3 seconds
left_people = [
person for person in found_people
if curr_time - person.last_updated >= 1
]
found_people = [
person for person in found_people
if curr_time - person.last_updated < 1
]
client.publish("person", json.dumps({"count": len(found_people)}))
# Draw boxes
for person in found_people:
cv2.rectangle(frame, (person.xmin, person.ymin),
(person.xmax, person.ymax), (125, 250, 0), 1)
for person in left_people:
t = curr_time - person.time_found
client.publish("person/duration", json.dumps({"duration": t}))
frame = cv2.resize(frame, (cap_w, cap_h))
sys.stdout.buffer.write(frame)
sys.stdout.flush()
if isImage:
cv2.imwrite('output.jpg', frame)
if key_pressed == 27:
break
cap.release()
cv2.destroyAllWindows()
def main():
"""
Load the network and parse the output.
:return: None
"""
# Grab command line args
args = build_argparser().parse_args()
# Connect to the MQTT server
client = connect_mqtt()
# Perform inference on the input stream
infer_on_stream(args, client)
if __name__ == '__main__':
main()