-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
258 lines (213 loc) · 9.16 KB
/
main.py
File metadata and controls
258 lines (213 loc) · 9.16 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
"""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 WARRANTIES 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 socket
import json
import cv2
import logging as log
import paho.mqtt.client as mqtt
from argparse import ArgumentParser
from inference import Network
CPU_EXTENSION = "/opt/intel/openvino/deployment_tools/inference_engine/lib/intel64/libcpu_extension_sse4.so"
# MQTT server environment variables
HOSTNAME = socket.gethostname()
IPADDRESS = socket.gethostbyname(HOSTNAME)
MQTT_HOST = IPADDRESS
MQTT_PORT = 3001
MQTT_KEEPALIVE_INTERVAL = 60
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():
"""
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
"""
request_id = 0
# Flags
is_person_in_frame = False
detection_in_frame = 0
single_image_mode = False
# Counters
none_detection_counter = 0
frame_detection_counter = 0
last_count_of_people_in_frame = 0
total_people_count = 0
# Time trackers
frames_with_person = 0
time_avg = 0
duration = 0
# Initialise the class
infer_network = Network()
# Set Probability threshold for detections
prob_threshold = args.prob_threshold
load_time_start = time.time()
# Load the model through `infer_network`
infer_network.load_model(args.model, args.device, CPU_EXTENSION)
load_time_total = time.time() - load_time_start
# network_shape = infer_network.get_input_shape() # Faster RCNN Model
# input_shape = network_shape['image_tensor'] # Faster RCNN Model
input_shape = infer_network.get_input_shape()
input_name = infer_network.get_input_name()
# Handle the input stream
if args.input == "CAM":
# Checks for Webcam
# input_file = 0
input_file = -1
elif args.input.endswith(".jpg") or args.input.endswith(".png"):
# Checks for input image
single_image_mode = True
input_file = args.input
else:
# Checks for input video
input_file = args.input
cap = cv2.VideoCapture(input_file)
if input_file:
cap.open(input_file)
# Frames per second
fps = cap.get(cv2.CAP_PROP_FPS)
# Loop until stream is over
while cap.isOpened():
# Read from the video capture
flag, frame = cap.read()
if not flag:
break
# Pre-process the image as needed
p_frame = cv2.resize(frame, (input_shape[3], input_shape[2]))
p_frame = p_frame.transpose((2,0,1))
p_frame.reshape(1, *p_frame.shape)
# input_dict = {'image_tensor': p_frame,'image_info': (600,600,1)} # Faster RCNN Model
input_dict = {input_name : p_frame}
infer_start_time = time.time()
# Start asynchronous inference for specified request
infer_network.exec_net(request_id, input_dict)
# Wait for the result...
if infer_network.wait(request_id) == 0:
# Get the results of the inference request
result = infer_network.get_output(request_id)
probs = result[0, 0, :, 2]
# Extract predictions and draw bounding boxes
people_in_frame = 0
h = frame.shape[0]
w = frame.shape[1]
for idx, prob in enumerate(probs):
if prob > prob_threshold:
people_in_frame += 1
box = result[0, 0, idx, 3:]
p1 = (int(box[0] * w), int(box[1] * h))
p2 = (int(box[2] * w), int(box[3] * h))
frame = cv2.rectangle(frame, p1, p2, (0, 255, 0), 3)
detection_in_frame = True
if last_count_of_people_in_frame < people_in_frame:
last_count_of_people_in_frame = people_in_frame
infer_total_time = time.time() - infer_start_time
# A repeated amount of frames will confirm if the person is detected or not
if detection_in_frame:
frame_detection_counter += 1
none_detection_counter = 0
else:
none_detection_counter += 1
frame_detection_counter = 0
# Reset flag
detection_in_frame = False
# If there is a person in frame, start counter
if is_person_in_frame:
frames_with_person += 1
if frame_detection_counter == 20 and is_person_in_frame == False:
# There was no people at frame, but now there is so start timer and count person
is_person_in_frame = True
# Reset counters
frame_detection_counter = 0
none_detection_counter = 0
elif none_detection_counter == 20 and is_person_in_frame == True:
# There was people in frame, now there is not, stop timer and reset counter
# Add # of people to counter
is_person_in_frame = False
total_people_count += last_count_of_people_in_frame
time_avg += (frames_with_person / fps)
duration = round(time_avg / total_people_count)
# Reset counters
frame_detection_counter = 0
none_detection_counter = 0
frames_with_person = 0
### current_count, total_count and duration to the MQTT server ###
### Topic "person": keys of "count" and "total" ###
### Topic "person/duration": key of "duration" ###
client.publish("person", json.dumps({"count": people_in_frame, "total": total_people_count}))
if duration:
client.publish("person/duration", json.dumps({"duration": duration}))
### Send the frame to the FFMPEG server ###
sys.stdout.buffer.write(frame)
sys.stdout.flush()
# Write an output image if `single_image_mode`
if single_image_mode:
cv2.imwrite("images/output.jpg", frame)
print("Total inference time: {0}", infer_total_time)
print("Model time loading: {0}", load_time_total)
# Release the capture and destroy any OpenCV windows
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()