-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
502 lines (442 loc) · 13.8 KB
/
demo.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#!/usr/bin/env python
import argparse
import logging
import os
import sys
import threading
import time
from datetime import datetime
import cv2
import paho.mqtt.client as mqtt
import requests
import tritonclient.grpc as grpcclient
import tritonclient.http as httpclient
from flask import Flask, Response, render_template
from tritonclient.utils import InferenceServerException
from classify_image import *
# Set arg parse arguments
parser = argparse.ArgumentParser()
parser.add_argument(
"-f", "--flask", action="store_true", help="enable flask app"
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
required=False,
default=False,
help="Enable verbose output",
)
parser.add_argument(
"-i",
"--ip",
type=str,
required=False,
default=os.getenv("LISTEN_IP", "0.0.0.0"),
help="listen ip address",
)
parser.add_argument(
"--port",
type=int,
required=False,
default=os.getenv("LISTEN_PORT", "8080"),
help="ephemeral port number of the server (1024 to 65535) default 8080",
)
parser.add_argument(
"-d",
"--devno",
type=int,
default=os.getenv("DEVNO", "-1"),
help=(
"device number for camera (typically -1=find first available,"
" 0=internal, 1=external)"
),
)
parser.add_argument(
"-n",
"--capture-string",
type=str,
default=os.getenv("CAPTURE_STRING"),
help=(
"Any valid VideoCapture string(IP camera connection, RTSP connection"
" string, etc"
),
)
parser.add_argument(
"-c", "--confidence", type=float, default=os.getenv("CONFIDENCE", "0.3")
)
parser.add_argument("-p", "--publish", action="store_true")
parser.add_argument(
"-s", "--sleep", type=float, default=os.getenv("SLEEP", "1.0")
)
parser.add_argument(
"--protocol", type=str, default=os.getenv("PROTOCOL", "HTTP")
)
parser.add_argument(
"-m",
"--model-name",
type=str,
required=False,
default=os.getenv("MODEL_NAME", "ssd_mobilenet_coco"),
help="Name of model",
)
parser.add_argument(
"-x",
"--model-version",
type=str,
required=False,
default=os.getenv("MODEL_VERSION", ""),
help="Version of model. Default is to use latest version.",
)
parser.add_argument(
"-u",
"--triton-url",
type=str,
required=False,
default=os.getenv("TRITON_URL", "localhost:8000"),
help="Inference server URL. Default is localhost:8000.",
)
parser.add_argument(
"--smarter-inference-url",
type=str,
required=False,
default=os.getenv("SMARTER_INFERENCE_URL", ""),
help="smarter-inferenceler URL. Default is localhost:2520.",
)
parser.add_argument(
"-b",
"--mqtt-broker-host",
type=str,
required=False,
default=os.getenv("MQTT_BROKER_HOST", "fluent-bit"),
help="mqtt broker host",
)
parser.add_argument(
"--mqtt-broker-port",
type=int,
required=False,
default=os.getenv("MQTT_BROKER_PORT", "1883"),
help="port number of the mqtt server (1024 to 65535) default 1883",
)
parser.add_argument(
"-t",
"--mqtt-topic",
type=str,
required=False,
default=os.getenv("MQTT_TOPIC", "/demo"),
help="mqtt broker topic",
)
parser.add_argument("-db1", "--detect-car", action="store_true")
parser.add_argument("-db2", "--detect-person", action="store_true")
parser.add_argument("-db3", "--detect-bus", action="store_true")
parser.add_argument("-db4", "--detect-bicycle", action="store_true")
parser.add_argument("-db5", "--detect-motorcycle", action="store_true")
args = parser.parse_args()
# initialize the output frame and a lock used to ensure thread-safe
# exchanges of the output frames (useful for multiple browsers/tabs
# are viewing tthe stream)
outputFrame = None
lock = threading.Lock()
# initialize a flask object
app = Flask(__name__)
# Flask routes
@app.route("/")
def index():
# return the rendered template
return render_template("index.html")
@app.route("/video_feed")
def video_feed():
# return the response generated along with the specific media
# type (mime type)
return Response(
generate(), mimetype="multipart/x-mixed-replace; boundary=frame"
)
def detection_loop():
try:
for img in getframe():
detected_objects = infer_image(
tritonclass,
triton_client,
endpoint_uuid,
args.model_version,
input_name,
output_names,
img,
args.confidence,
classes,
)
post_process(img, detected_objects)
if args.sleep:
time.sleep(args.sleep)
except:
os._exit(1)
def getframe():
if args.capture_string:
cam = cv2.VideoCapture(args.capture_string)
else:
cam = cv2.VideoCapture(args.devno)
if cam.isOpened() == False:
print(
"Error opening video stream! Capture String:"
f" {args.capture_string} Video devno: {args.devno}"
)
sys.exit(-1)
while True:
ret, frame = cam.read()
if not ret:
print("No camera found")
sys.exit(-1)
yield frame
cam.release()
def generate():
# grab global references to the output frame and lock variables
global outputFrame, lock
# loop over frames from the output stream
while True:
# wait until the lock is acquired
with lock:
# check if the output frame is available, otherwise skip
# the iteration of the loop
if outputFrame is None:
continue
# encode the frame in JPEG format
(flag, encodedImage) = cv2.imencode(".jpg", outputFrame)
# ensure the frame was successfully encoded
if not flag:
continue
# yield the output frame in the byte format
yield (
b"--frame\r\nContent-Type: image/jpeg\r\n\r\n"
+ bytearray(encodedImage)
+ b"\r\n"
)
def log_it(sensor, label, value):
# log something vaguely resembling waggle logs
# timestamp,node_id,subsystem,sensor,parameter,label,value
timestamp = (
'"timestamp":"' + datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") + '"'
)
node_id = '"nodeid":"0"'
subsystem = '"nodeid":"0"'
dataJson = '"' + label + '":"' + str(value) + '"'
sensorJson = '"sensor":"' + str(sensor) + '"'
mylist = [timestamp, node_id, subsystem, sensorJson, dataJson]
mystr = "{" + ",".join(map(str, mylist)) + "}"
print(mystr)
if mqtt_client:
mqtt_client.publish("{}/{}".format(args.mqtt_topic, label), mystr)
def annotate(img, bbox, color, thickness=2):
cv2.rectangle(
img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color, thickness
)
def post_process(img, detected_objects):
# grab global references to the output frame and lock variables
global outputFrame, lock
if args.detect_car:
cars = detected_objects.get("car", {})
ncar = len(cars)
log_it("image", "car_count", ncar)
if args.detect_person:
people = detected_objects.get("person", {})
nperson = len(people)
log_it("image", "person_count", nperson)
if args.detect_bicycle:
bicycles = detected_objects.get("bicycle", {})
nbicycle = len(bicycles)
log_it("image", "bicycle_count", nbicycle)
if args.detect_bus:
buses = detected_objects.get("bus", {})
nbus = len(buses)
log_it("image", "bus_count", nbus)
if args.detect_motorcycle:
motorcycles = detected_objects.get("motorcycles", {})
nmotorcycle = len(motorcycles)
log_it("image", "motorcycle_count", nmotorcycle)
if args.detect_car:
for i in range(ncar):
bbox = cars[i]
annotate(img, bbox, (0, 255, 0))
if args.detect_person:
for i in range(nperson):
bbox = people[i]
annotate(img, bbox, (0, 0, 255))
if args.detect_bicycle:
for i in range(nbicycle):
bbox = bicycles[i]
annotate(img, bbox, (255, 0, 0))
if args.detect_bus:
for i in range(nbus):
bbox = buses[i]
annotate(img, bbox, (255, 0, 255))
if args.detect_motorcycle:
for i in range(nmotorcycle):
bbox = motorcycles[i]
annotate(img, bbox, (0, 255, 255))
with lock:
outputFrame = img
def upload_model(
url,
model_type,
model_name,
model_filepath,
model_config_filepath,
profile_data_filepath=None,
):
"""
Upload a model and its triton model config to the AC
Returns status code
"""
url = f"http://{url}/upload/{model_type}"
req_params = {"model_name": model_name}
upload_files = [
("files", open(model_filepath, "rb")),
("files", open(model_config_filepath, "rb")),
]
if profile_data_filepath:
upload_files.append(
(
"files",
open(profile_data_filepath, "rb"),
)
)
return requests.post(url, params=req_params, files=upload_files)
def load_model(
url,
model_name,
load_type,
method,
request_batch_size=1,
throughput_objective_weight=1,
latency_objective_weight=1,
latency_constraint=1,
):
# Create a load request
load_request = {
"model_name": model_name,
"load_type": load_type,
"method": method,
"batch_size": request_batch_size,
"perf_targets": {
"objectives": {
"perf_throughput": throughput_objective_weight,
"perf_latency": latency_objective_weight,
},
"constraints": {
"perf_throughput": 1 / latency_constraint,
"perf_latency": latency_constraint,
},
},
}
url = f"http://{url}/load"
return requests.post(url, json=load_request)
if __name__ == "__main__":
# open up camera
if not args.capture_string:
if args.devno < 0:
video_entries = [
entry
for entry in os.listdir("/dev")
if entry.startswith("video")
]
if len(video_entries) == 0:
logging.error("No cameras available")
exit(0)
args.devno = int(video_entries[0][len("video") :])
print("Using entry " + str(args.devno))
# Register MQTT client
mqtt_client = None
if args.publish:
mqtt_client = mqtt.Client()
mqtt_client.connect(args.mqtt_broker_host, args.mqtt_broker_port, 60)
mqtt_client.loop_start()
classes = read_classes("ssd_mobilenet_coco.classes")
if args.smarter_inference_url:
# Use smarter-inference api to upload model then request to load it
res = upload_model(
args.smarter_inference_url,
"tf",
"ssd_mobilenet_coco",
"model.graphdef",
"config.pbtxt",
)
try:
assert res.status_code in [201, 303]
except AssertionError:
logging.error(f"Upload model failed with response {res.text}")
sys.exit(-1)
print("Model upload successful")
res = load_model(
args.smarter_inference_url,
"ssd_mobilenet_coco",
"auto_gen",
"passthrough",
latency_constraint=args.sleep,
)
# endpoint_uuid holds the translated model name after loading
# for the client to request from using the triton client api
try:
assert res.status_code in [201, 303]
except AssertionError:
logging.error(f"Load model failed with response {res.text}")
sys.exit(-1)
res_json = res.json()
endpoint_uuid = res_json["request_uuid"]
triton_url = args.smarter_inference_url.split(":")[0] + ":" + "2521"
print(res_json["model_config"])
print("Model load successful")
else:
endpoint_uuid = args.model_name
triton_url = args.triton_url
if args.protocol.lower() == "grpc":
# Create gRPC client for communicating with the server
triton_client = grpcclient.InferenceServerClient(
url=triton_url, verbose=args.verbose
)
else:
# Create HTTP client for communicating with the server
triton_client = httpclient.InferenceServerClient(
url=triton_url, verbose=args.verbose
)
# Make sure the model matches our requirements, and get some
# properties of the model that we need for preprocessing
try:
model_metadata = triton_client.get_model_metadata(
model_name=args.model_name, model_version=args.model_version
)
except InferenceServerException as e:
print("failed to retrieve the metadata: " + str(e))
sys.exit(1)
try:
model_config = triton_client.get_model_config(
model_name=args.model_name, model_version=args.model_version
)
except InferenceServerException as e:
print("failed to retrieve the config: " + str(e))
sys.exit(1)
if args.protocol.lower() == "grpc":
input_name, output_names = validate_model_grpc(
model_metadata, model_config.config
)
tritonclass = grpcclient
else:
input_name, output_names = validate_model_http(
model_metadata, model_config
)
tritonclass = httpclient
# Read from camera and serve flask app
if args.flask:
# start a thread that will perform object detection
t = threading.Thread(target=detection_loop)
t.start()
# start the flask app
app.run(
host=args.ip,
port=args.port,
debug=True,
threaded=True,
use_reloader=False,
)
else:
detection_loop()
if args.publish:
mqtt_client.disconnect()