blob: 393b5d25c463596d72f4be8af013100e5260fec8 [file] [log] [blame]
Shad Ansari30a23732021-09-29 23:07:21 -07001#!/usr/bin/env python
Shad Ansariae90d252021-10-22 16:40:29 +00002"""
3SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org>
4SPDX-License-Identifier: LicenseRef-ONF-Member-1.01
5"""
Shad Ansaric9a2e792022-05-09 20:05:11 +00006import os
Shad Ansari2eddc1e2022-01-10 17:53:06 +00007import logging as log
8import sys
9import flask
Shad Ansari30a23732021-09-29 23:07:21 -070010from argparse import ArgumentParser, SUPPRESS
11
Shad Ansari9a95c3d2021-12-09 04:32:09 +000012import config
Shad Ansari2ecc6522021-12-03 21:03:48 +000013from person_detection import Camera
shadf64b92a2022-01-13 19:06:29 +000014from roc import Roc
Shad Ansari30a23732021-09-29 23:07:21 -070015
Shad Ansari30a23732021-09-29 23:07:21 -070016
Shad Ansari2eddc1e2022-01-10 17:53:06 +000017app = flask.Flask(__name__)
Shad Ansari30a23732021-09-29 23:07:21 -070018
19
20@app.route('/')
21def index():
22 """Video streaming home page."""
Shad Ansaria7170992022-04-15 21:36:01 +000023 global args
Shad Ansaric9a2e792022-05-09 20:05:11 +000024 devices = {str(x): "Camera "+str(x) for x in range(args.num)}
Shad Ansari2eddc1e2022-01-10 17:53:06 +000025 log.info("{} - connected".format(flask.request.remote_addr))
Shad Ansaria7170992022-04-15 21:36:01 +000026 return flask.render_template('index.html', devices=devices)
Shad Ansari30a23732021-09-29 23:07:21 -070027
Shad Ansari35019042022-03-29 20:15:54 +000028
Shad Ansari30a23732021-09-29 23:07:21 -070029def gen(camera):
30 """Video streaming generator function."""
Shad Ansari30a23732021-09-29 23:07:21 -070031 while True:
32 frame = camera.get_frame()
33 yield (b'--frame\r\n'
34 b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
35
Shad Ansari35019042022-03-29 20:15:54 +000036
Shad Ansarib5808d72021-10-04 12:43:27 -070037@app.route('/video_feed/<device>')
38def video_feed(device):
Shad Ansari30a23732021-09-29 23:07:21 -070039 """Video streaming route. Put this in the src attribute of an img tag."""
Shad Ansarib5808d72021-10-04 12:43:27 -070040 global args
Shad Ansari2eddc1e2022-01-10 17:53:06 +000041 log.debug("{} - video feed {}".format(flask.request.remote_addr, device))
42 camera = Camera(device, flask.request.remote_addr, args)
43 return flask.Response(gen(camera),
Shad Ansari35019042022-03-29 20:15:54 +000044 mimetype='multipart/x-mixed-replace; boundary=frame')
45
Shad Ansari30a23732021-09-29 23:07:21 -070046
Shad Ansarib5808d72021-10-04 12:43:27 -070047def name_to_port(name):
48 return int(name)
49
Shad Ansari35019042022-03-29 20:15:54 +000050
Shad Ansari30a23732021-09-29 23:07:21 -070051def build_argparser():
52 parser = ArgumentParser(add_help=False)
53 args = parser.add_argument_group('Options')
Shad Ansaric9a2e792022-05-09 20:05:11 +000054 args.add_argument('-h', '--help', action='help', default=SUPPRESS,
55 help='Show this help message and exit.')
Shad Ansari5e8d0692021-12-08 19:09:34 +000056 args.add_argument("-m", "--model",
Shad Ansari7ee36bf2022-05-10 22:43:42 +000057 default="models/intel/person-detection-retail-0013/FP32/person-detection-retail-0013.xml",
58 help="Path to model", type=str)
Shad Ansaric9a2e792022-05-09 20:05:11 +000059 args.add_argument("-i", "--input",
60 help="Video input", default="gstreamer", type=str)
Shad Ansari7ee36bf2022-05-10 22:43:42 +000061 args.add_argument("-pt", "--prob_threshold", default=0.75,
62 help="Probability threshold for detection", type=float)
Shad Ansaric9a2e792022-05-09 20:05:11 +000063 args.add_argument("--noroc", action='store_true', help="No ROC")
64 args.add_argument("--key", help="ROC api key", type=str)
65 args.add_argument("--url", default=config.ROCURL,
66 help="ROC url", type=str)
67 args.add_argument("--keycloak", default=config.KEYCLOAKURL,
68 help="Keycloak url", type=str)
69 args.add_argument("--enterprise", default=config.ENTERPRISE,
70 help="Enterprise ID", type=str)
71 args.add_argument("--site", default=config.SITE,
72 help="Site ID", type=str)
73 args.add_argument("--devicegroup", default=config.DEVICEGROUP,
74 help="Camera device group", type=str)
75 args.add_argument("--user", default=config.ROCUSER,
76 help="ROC username", type=str)
77 args.add_argument("--password", default=config.ROCPASSWORD,
78 help="ROC password", type=str)
79 args.add_argument("--num", default=config.NUMDEVICES,
80 help="Number of camera devices",
81 type=int)
Shad Ansarib652dc72022-05-13 21:11:16 +000082 args.add_argument("--mqttip", default=config.MQTTIP,
83 help="IP address of MQTT",
84 type=str)
Shad Ansaric9a2e792022-05-09 20:05:11 +000085 args.add_argument("--mbrlow", help="Low range of MBR", default=7000000,
86 type=int)
87 args.add_argument("--mbrhigh", help="High range of MBR", default=10000000,
88 type=int)
Shad Ansari30a23732021-09-29 23:07:21 -070089
90 return parser
91
Shad Ansarib5808d72021-10-04 12:43:27 -070092
Shad Ansari30a23732021-09-29 23:07:21 -070093if __name__ == '__main__':
shadf64b92a2022-01-13 19:06:29 +000094 log.basicConfig(
95 format='%(asctime)s %(levelname)-8s %(message)s',
96 level=log.DEBUG,
97 datefmt='%Y-%m-%d %H:%M:%S',
98 stream=sys.stdout)
Shad Ansari2eddc1e2022-01-10 17:53:06 +000099 log.debug("Starting person detection app")
shadf64b92a2022-01-13 19:06:29 +0000100
Shad Ansari30a23732021-09-29 23:07:21 -0700101 args = build_argparser().parse_args()
shadf64b92a2022-01-13 19:06:29 +0000102
Shad Ansari2eddc1e2022-01-10 17:53:06 +0000103 if not args.noroc:
shadf64b92a2022-01-13 19:06:29 +0000104 key = args.key
105 if key is None:
106 if args.user is not None and args.password is not None:
Shad Ansaric9a2e792022-05-09 20:05:11 +0000107 roc = Roc(args.url, args.keycloak, args.user, args.password,
108 args.enterprise, args.site)
shadf64b92a2022-01-13 19:06:29 +0000109 key = roc.get_key()
110 else:
111 log.error("Either key or user/password required")
112 sys.exit()
113
Shad Ansaric9a2e792022-05-09 20:05:11 +0000114 log.info("Device group:{} mbr:{}(low)".format(args.devicegroup,
115 args.mbrlow))
shadf64b92a2022-01-13 19:06:29 +0000116 roc.set_mbr(args.devicegroup, args.mbrlow)
117
Shad Ansarid70cb9e2021-10-26 05:33:02 +0000118 app.run(host='0.0.0.0', debug=True)