-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
236 lines (209 loc) · 9.84 KB
/
main.py
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
"""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 numpy as np
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
LABELS = {'1': 'person', '2': 'bicycle', '3': 'car', '4': 'motorcycle', '5': 'airplane', '6': 'bus', '7': 'train', '8': 'truck', '9': 'boat', '10': 'traffic', '11': 'fire', '13': 'stop', '14': 'parking', '15': 'bench', '16': 'bird', '17': 'cat', '18': 'dog', '19': 'horse', '20': 'sheep', '21': 'cow', '22': 'elephant', '23': 'bear', '24': 'zebra', '25': 'giraffe', '27': 'backpack', '28': 'umbrella', '31': 'handbag', '32': 'tie', '33': 'suitcase', '34': 'frisbee', '35': 'skis', '36': 'snowboard', '37': 'sports', '38': 'kite', '39': 'baseball', '40': 'baseball', '41': 'skateboard', '42': 'surfboard', '43': 'tennis', '44': 'bottle', '46': 'wine', '47': 'cup', '48': 'fork', '49': 'knife', '50': 'spoon', '51': 'bowl', '52': 'banana', '53': 'apple', '54': 'sandwich', '55': 'orange', '56': 'broccoli', '57': 'carrot', '58': 'hot', '59': 'pizza', '60': 'donut', '61': 'cake', '62': 'chair', '63': 'couch', '64': 'potted', '65': 'bed', '67': 'dining', '70': 'toilet', '72': 'tv', '73': 'laptop', '74': 'mouse', '75': 'remote', '76': 'keyboard', '77': 'cell', '78': 'microwave', '79': 'oven', '80': 'toaster', '81': 'sink', '82': 'refrigerator', '84': 'book', '85': 'clock', '86': 'vase', '87': 'scissors', '88': 'teddy', '89': 'hair', '90': 'toothbrush','0':'None'}
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 or enter cam for webcam")
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 extract_box(img, output, conf_level=0.35):
h = img.shape[0]
w = img.shape[1]
box = output[0,0,:,3:7] * np.array([w, h, w, h])
box = box.astype(np.int32)
cls = output[0,0,:,1]
conf = output[0,0,:,2]
count=0
p1 = None
p2 = None
for i in range(len(box)):
label = LABELS[str(int(cls[i]))]
if (not label=='person') or conf[i]<conf_level:
continue
p1 = (box[i][0], box[i][1])
p2 = (box[i][2], box[i][3])
cv2.rectangle(img, p1, p2, (0,255,0))
count+=1
return img, count, (p1,p2)
def connect_mqtt():
### TODO: Connect to the MQTT client ###
client = mqtt.Client()
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
"""
modelPath = args.model
deviceType = args.device
cpuExt = args.cpu_extension
probThresh = args.prob_threshold
filePath = args.input
# Initialise the class
infer_network = Network()
# Set Probability threshold for detections
prob_threshold = probThresh
### TODO: Load the model through `infer_network` ###
if filePath.lower()=="cam":
camera = cv2.VideoCapture(0)
elif filePath.split(".")[-1].lower() in ['jpg', 'jpeg', 'png', 'bmp']:
infer_network.load_model(modelPath, 1, deviceType, cpuExt)
image_input_shape = infer_network.get_input_shape()
#print(image_input_shape)
img = cv2.imread(filePath, cv2.IMREAD_COLOR)
resized_frame = cv2.resize(img, (image_input_shape[3], image_input_shape[2]))
frame_preproc = np.transpose(np.expand_dims(resized_frame.copy(), axis=0), (0,3,1,2))
infer_network.exec_net(frame_preproc)
if infer_network.wait()==0:
outputs = infer_network.get_output()
box_frame, count, bbox = extract_box(img, outputs, prob_threshold)
cv2.putText(box_frame, "Count:"+str(count), (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 3)
cv2.imwrite('output.jpg', box_frame)
return
else:
if not os.path.isfile(filePath):
#print(" Given input file is not present.")
exit(1)
camera = cv2.VideoCapture(filePath)
### TODO: Handle the input stream ###
client.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
if (camera.isOpened()== False):
#print("Error opening video stream or file")
exit(1)
cur_req_id=0
next_req_id=1
num_requests=2
infer_network.load_model(modelPath, num_requests, deviceType, cpuExt)
image_input_shape = infer_network.get_input_shape()
#print(image_input_shape)
ret, frame = camera.read()
### TODO: Loop until stream is over ###
total_count=0
pres_count = 0
prev_count=0
start_time=0
no_bbox=0
duration=0
prev_bbox_x = 0
while camera.isOpened():
### TODO: Read from the video capture ###
ret, next_frame = camera.read()
if not ret:
break
key = cv2.waitKey(60)
### TODO: Pre-process the image as needed ###
resized_frame = cv2.resize(next_frame.copy(), (image_input_shape[3], image_input_shape[2]))
frame_preproc = np.transpose(np.expand_dims(resized_frame.copy(), axis=0), (0,3,1,2))
### TODO: Start asynchronous inference for specified request ###
infer_network.exec_net(frame_preproc.copy(), req_id=next_req_id)
### TODO: Wait for the result ###
if infer_network.wait(cur_req_id)==0:
### TODO: Get the results of the inference request ###
outputs = infer_network.get_output(cur_req_id)
### TODO: Extract any desired stats from the results ###
frame, pres_count, bbox = extract_box(frame.copy(), outputs[0], prob_threshold)
box_w = frame.shape[1]
tl, br = bbox #top_left, bottom_right
if pres_count>prev_count:
start_time = time.time()
total_count+=pres_count-prev_count
no_bbox=0
client.publish("person", json.dumps({"total":total_count}))
elif pres_count<prev_count:
if no_bbox<=20:
pres_count=prev_count
no_bbox+=1
elif prev_bbox_x<box_w-200:
pres_count=prev_count
no_bbox=0
else:
duration = int(time.time()-start_time)
client.publish("person/duration", json.dumps({"duration":duration}))
if not (tl==None and br==None):
prev_bbox_x=int((tl[0]+br[0])/2)
prev_count=pres_count
client.publish("person", json.dumps({"count":pres_count}))
### TODO: Calculate and send relevant information on ###
### current_count, total_count and duration to the MQTT server ###
### Topic "person": keys of "count" and "total" ###
### Topic "person/duration": key of "duration" ###
### TODO: Send the frame to the FFMPEG server ###
sys.stdout.buffer.write(frame)
sys.stdout.flush()
### TODO: Write an output image if `single_image_mode` ###
cur_req_id, next_req_id = next_req_id, cur_req_id
frame = next_frame
if key==27:
break
#output_video.release()
camera.release()
client.disconnect()
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()