blob: 59cc96682bdbe8f6f55e005b2f40ae1529e9b075 [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 Ansari2eddc1e2022-01-10 17:53:06 +00006import logging as log
7import sys
8import flask
Shad Ansari30a23732021-09-29 23:07:21 -07009from argparse import ArgumentParser, SUPPRESS
10
Shad Ansari9a95c3d2021-12-09 04:32:09 +000011import config
Shad Ansari2ecc6522021-12-03 21:03:48 +000012from person_detection import Camera
shadf64b92a2022-01-13 19:06:29 +000013from roc import Roc
Shad Ansari30a23732021-09-29 23:07:21 -070014
Shad Ansari30a23732021-09-29 23:07:21 -070015
Shad Ansari2eddc1e2022-01-10 17:53:06 +000016app = flask.Flask(__name__)
Shad Ansari30a23732021-09-29 23:07:21 -070017
18
19@app.route('/')
20def index():
21 """Video streaming home page."""
Shad Ansaria7170992022-04-15 21:36:01 +000022 global args
23 devices = {str(x):"Camera "+str(x) for x in range(args.num)}
Shad Ansari2eddc1e2022-01-10 17:53:06 +000024 log.info("{} - connected".format(flask.request.remote_addr))
Shad Ansaria7170992022-04-15 21:36:01 +000025 return flask.render_template('index.html', devices=devices)
Shad Ansari30a23732021-09-29 23:07:21 -070026
Shad Ansari35019042022-03-29 20:15:54 +000027
Shad Ansari30a23732021-09-29 23:07:21 -070028def gen(camera):
29 """Video streaming generator function."""
Shad Ansari30a23732021-09-29 23:07:21 -070030 while True:
31 frame = camera.get_frame()
32 yield (b'--frame\r\n'
33 b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
34
Shad Ansari35019042022-03-29 20:15:54 +000035
Shad Ansarib5808d72021-10-04 12:43:27 -070036@app.route('/video_feed/<device>')
37def video_feed(device):
Shad Ansari30a23732021-09-29 23:07:21 -070038 """Video streaming route. Put this in the src attribute of an img tag."""
Shad Ansarib5808d72021-10-04 12:43:27 -070039 global args
Shad Ansari2eddc1e2022-01-10 17:53:06 +000040 log.debug("{} - video feed {}".format(flask.request.remote_addr, device))
41 camera = Camera(device, flask.request.remote_addr, args)
42 return flask.Response(gen(camera),
Shad Ansari35019042022-03-29 20:15:54 +000043 mimetype='multipart/x-mixed-replace; boundary=frame')
44
Shad Ansari30a23732021-09-29 23:07:21 -070045
Shad Ansarib5808d72021-10-04 12:43:27 -070046def name_to_port(name):
47 return int(name)
48
Shad Ansari35019042022-03-29 20:15:54 +000049
Shad Ansari30a23732021-09-29 23:07:21 -070050def build_argparser():
51 parser = ArgumentParser(add_help=False)
52 args = parser.add_argument_group('Options')
Shad Ansari5e8d0692021-12-08 19:09:34 +000053 args.add_argument('-h', '--help',
54 action = 'help',
55 default = SUPPRESS,
56 help = 'Show this help message and exit.')
57 args.add_argument("-m", "--model",
58 help = "Required. Path to an .xml file with a trained model.",
59 required = True,
60 type = str)
Shad Ansari30a23732021-09-29 23:07:21 -070061 args.add_argument("-i", "--input",
Shad Ansari5e8d0692021-12-08 19:09:34 +000062 help = "Path to video file or image. 'cam' for capturing video stream from camera",
63 default = "gstreamer",
64 type = str)
65 args.add_argument("-pt", "--prob_threshold",
66 help = "Optional. Probability threshold for detections filtering",
67 default = 0.75,
68 type = float)
Shad Ansariec6bbd32021-12-10 20:57:16 +000069 args.add_argument("--noroc",
Shad Ansari2eddc1e2022-01-10 17:53:06 +000070 action = 'store_true',
Shad Ansariec6bbd32021-12-10 20:57:16 +000071 help = "No ROC")
Shad Ansari5e8d0692021-12-08 19:09:34 +000072 args.add_argument("--key",
73 help = "ROC api key",
Shad Ansari5e8d0692021-12-08 19:09:34 +000074 type = str)
Shad Ansari2948cc02022-04-14 21:33:37 +000075 args.add_argument("--url",
76 help = "ROC url",
77 type = str)
78 args.add_argument("--keycloak",
79 help = "Keycloak url",
80 type = str)
81 args.add_argument("--enterprise",
82 help = "Enterprise ID",
83 type = str)
84 args.add_argument("--site",
85 help = "Site ID",
86 type = str)
shadf64b92a2022-01-13 19:06:29 +000087 args.add_argument("--user",
88 help = "ROC username",
89 type = str)
90 args.add_argument("--password",
91 help = "ROC password",
92 type = str)
Shad Ansaria7170992022-04-15 21:36:01 +000093 args.add_argument("--num",
94 help="Number of camera devices",
95 default=1,
96 type=int)
Shad Ansari5e8d0692021-12-08 19:09:34 +000097 args.add_argument("--mbrlow",
Shad Ansari35019042022-03-29 20:15:54 +000098 help="Low range of MBR",
99 default=7000000,
100 type=int)
Shad Ansari5e8d0692021-12-08 19:09:34 +0000101 args.add_argument("--mbrhigh",
Shad Ansari35019042022-03-29 20:15:54 +0000102 help="High range of MBR",
103 default=10000000,
104 type=int)
Shad Ansari5e8d0692021-12-08 19:09:34 +0000105 args.add_argument("--devicegroup",
Shad Ansari35019042022-03-29 20:15:54 +0000106 help="Camera device group",
Shad Ansari35019042022-03-29 20:15:54 +0000107 type=str)
Shad Ansari30a23732021-09-29 23:07:21 -0700108
109 return parser
110
Shad Ansarib5808d72021-10-04 12:43:27 -0700111
Shad Ansari30a23732021-09-29 23:07:21 -0700112if __name__ == '__main__':
shadf64b92a2022-01-13 19:06:29 +0000113 log.basicConfig(
114 format='%(asctime)s %(levelname)-8s %(message)s',
115 level=log.DEBUG,
116 datefmt='%Y-%m-%d %H:%M:%S',
117 stream=sys.stdout)
Shad Ansari2eddc1e2022-01-10 17:53:06 +0000118 log.debug("Starting person detection app")
shadf64b92a2022-01-13 19:06:29 +0000119
Shad Ansari30a23732021-09-29 23:07:21 -0700120 args = build_argparser().parse_args()
shadf64b92a2022-01-13 19:06:29 +0000121
Shad Ansari2eddc1e2022-01-10 17:53:06 +0000122 if not args.noroc:
shadf64b92a2022-01-13 19:06:29 +0000123 key = args.key
124 if key is None:
125 if args.user is not None and args.password is not None:
Shad Ansari2948cc02022-04-14 21:33:37 +0000126 roc = Roc(args.url, args.keycloak, args.user, args.password, args.enterprise, args.site)
shadf64b92a2022-01-13 19:06:29 +0000127 key = roc.get_key()
128 else:
129 log.error("Either key or user/password required")
130 sys.exit()
131
132 log.info("Device group:{} mbr:{}(low)".format(args.devicegroup, args.mbrlow))
133 roc.set_mbr(args.devicegroup, args.mbrlow)
134
Shad Ansarid70cb9e2021-10-26 05:33:02 +0000135 app.run(host='0.0.0.0', debug=True)