blob: 437374ebfc19d7c65a35bd91b84f5bc69dd92013 [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 Ansaric9a2e792022-05-09 20:05:11 +000051
52
Shad Ansari30a23732021-09-29 23:07:21 -070053def build_argparser():
54 parser = ArgumentParser(add_help=False)
55 args = parser.add_argument_group('Options')
Shad Ansaric9a2e792022-05-09 20:05:11 +000056 args.add_argument('-h', '--help', action='help', default=SUPPRESS,
57 help='Show this help message and exit.')
Shad Ansari5e8d0692021-12-08 19:09:34 +000058 args.add_argument("-m", "--model",
Shad Ansaric9a2e792022-05-09 20:05:11 +000059 help="Path to model", required=True,
Shad Ansari35019042022-03-29 20:15:54 +000060 type=str)
Shad Ansaric9a2e792022-05-09 20:05:11 +000061 args.add_argument("-i", "--input",
62 help="Video input", default="gstreamer", type=str)
63 args.add_argument("-pt", "--prob_threshold",
64 help="Probability threshold for detection",
65 default=0.75, type=float)
66 args.add_argument("--noroc", action='store_true', help="No ROC")
67 args.add_argument("--key", help="ROC api key", type=str)
68 args.add_argument("--url", default=config.ROCURL,
69 help="ROC url", type=str)
70 args.add_argument("--keycloak", default=config.KEYCLOAKURL,
71 help="Keycloak url", type=str)
72 args.add_argument("--enterprise", default=config.ENTERPRISE,
73 help="Enterprise ID", type=str)
74 args.add_argument("--site", default=config.SITE,
75 help="Site ID", type=str)
76 args.add_argument("--devicegroup", default=config.DEVICEGROUP,
77 help="Camera device group", type=str)
78 args.add_argument("--user", default=config.ROCUSER,
79 help="ROC username", type=str)
80 args.add_argument("--password", default=config.ROCPASSWORD,
81 help="ROC password", type=str)
82 args.add_argument("--num", default=config.NUMDEVICES,
83 help="Number of camera devices",
84 type=int)
85 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)