blob: 0c15bec12bcbed049d2a37f14ab935af5ec4db4f [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 Ansari2eddc1e2022-01-10 17:53:06 +000022 log.info("{} - connected".format(flask.request.remote_addr))
23 return flask.render_template('index.html', devices=config.cameras)
Shad Ansari30a23732021-09-29 23:07:21 -070024
Shad Ansari35019042022-03-29 20:15:54 +000025
Shad Ansari30a23732021-09-29 23:07:21 -070026def gen(camera):
27 """Video streaming generator function."""
Shad Ansari30a23732021-09-29 23:07:21 -070028 while True:
29 frame = camera.get_frame()
30 yield (b'--frame\r\n'
31 b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
32
Shad Ansari35019042022-03-29 20:15:54 +000033
Shad Ansarib5808d72021-10-04 12:43:27 -070034@app.route('/video_feed/<device>')
35def video_feed(device):
Shad Ansari30a23732021-09-29 23:07:21 -070036 """Video streaming route. Put this in the src attribute of an img tag."""
Shad Ansarib5808d72021-10-04 12:43:27 -070037 global args
Shad Ansari2eddc1e2022-01-10 17:53:06 +000038 log.debug("{} - video feed {}".format(flask.request.remote_addr, device))
39 camera = Camera(device, flask.request.remote_addr, args)
40 return flask.Response(gen(camera),
Shad Ansari35019042022-03-29 20:15:54 +000041 mimetype='multipart/x-mixed-replace; boundary=frame')
42
Shad Ansari30a23732021-09-29 23:07:21 -070043
Shad Ansarib5808d72021-10-04 12:43:27 -070044def name_to_port(name):
45 return int(name)
46
Shad Ansari35019042022-03-29 20:15:54 +000047
Shad Ansari30a23732021-09-29 23:07:21 -070048def build_argparser():
49 parser = ArgumentParser(add_help=False)
50 args = parser.add_argument_group('Options')
Shad Ansari5e8d0692021-12-08 19:09:34 +000051 args.add_argument('-h', '--help',
52 action = 'help',
53 default = SUPPRESS,
54 help = 'Show this help message and exit.')
55 args.add_argument("-m", "--model",
56 help = "Required. Path to an .xml file with a trained model.",
57 required = True,
58 type = str)
Shad Ansari30a23732021-09-29 23:07:21 -070059 args.add_argument("-i", "--input",
Shad Ansari5e8d0692021-12-08 19:09:34 +000060 help = "Path to video file or image. 'cam' for capturing video stream from camera",
61 default = "gstreamer",
62 type = str)
63 args.add_argument("-pt", "--prob_threshold",
64 help = "Optional. Probability threshold for detections filtering",
65 default = 0.75,
66 type = float)
Shad Ansariec6bbd32021-12-10 20:57:16 +000067 args.add_argument("--noroc",
Shad Ansari2eddc1e2022-01-10 17:53:06 +000068 action = 'store_true',
Shad Ansariec6bbd32021-12-10 20:57:16 +000069 help = "No ROC")
Shad Ansari5e8d0692021-12-08 19:09:34 +000070 args.add_argument("--key",
71 help = "ROC api key",
Shad Ansari5e8d0692021-12-08 19:09:34 +000072 type = str)
Shad Ansari2948cc02022-04-14 21:33:37 +000073 args.add_argument("--url",
74 help = "ROC url",
75 type = str)
76 args.add_argument("--keycloak",
77 help = "Keycloak url",
78 type = str)
79 args.add_argument("--enterprise",
80 help = "Enterprise ID",
81 type = str)
82 args.add_argument("--site",
83 help = "Site ID",
84 type = str)
shadf64b92a2022-01-13 19:06:29 +000085 args.add_argument("--user",
86 help = "ROC username",
87 type = str)
88 args.add_argument("--password",
89 help = "ROC password",
90 type = str)
Shad Ansari5e8d0692021-12-08 19:09:34 +000091 args.add_argument("--mbrlow",
Shad Ansari35019042022-03-29 20:15:54 +000092 help="Low range of MBR",
93 default=7000000,
94 type=int)
Shad Ansari5e8d0692021-12-08 19:09:34 +000095 args.add_argument("--mbrhigh",
Shad Ansari35019042022-03-29 20:15:54 +000096 help="High range of MBR",
97 default=10000000,
98 type=int)
Shad Ansari5e8d0692021-12-08 19:09:34 +000099 args.add_argument("--devicegroup",
Shad Ansari35019042022-03-29 20:15:54 +0000100 help="Camera device group",
Shad Ansari35019042022-03-29 20:15:54 +0000101 type=str)
Shad Ansari30a23732021-09-29 23:07:21 -0700102
103 return parser
104
Shad Ansarib5808d72021-10-04 12:43:27 -0700105
Shad Ansari30a23732021-09-29 23:07:21 -0700106if __name__ == '__main__':
shadf64b92a2022-01-13 19:06:29 +0000107 log.basicConfig(
108 format='%(asctime)s %(levelname)-8s %(message)s',
109 level=log.DEBUG,
110 datefmt='%Y-%m-%d %H:%M:%S',
111 stream=sys.stdout)
Shad Ansari2eddc1e2022-01-10 17:53:06 +0000112 log.debug("Starting person detection app")
shadf64b92a2022-01-13 19:06:29 +0000113
Shad Ansari30a23732021-09-29 23:07:21 -0700114 args = build_argparser().parse_args()
shadf64b92a2022-01-13 19:06:29 +0000115
Shad Ansari2eddc1e2022-01-10 17:53:06 +0000116 if not args.noroc:
shadf64b92a2022-01-13 19:06:29 +0000117 key = args.key
118 if key is None:
119 if args.user is not None and args.password is not None:
Shad Ansari2948cc02022-04-14 21:33:37 +0000120 roc = Roc(args.url, args.keycloak, args.user, args.password, args.enterprise, args.site)
shadf64b92a2022-01-13 19:06:29 +0000121 key = roc.get_key()
122 else:
123 log.error("Either key or user/password required")
124 sys.exit()
125
126 log.info("Device group:{} mbr:{}(low)".format(args.devicegroup, args.mbrlow))
127 roc.set_mbr(args.devicegroup, args.mbrlow)
128
Shad Ansarid70cb9e2021-10-26 05:33:02 +0000129 app.run(host='0.0.0.0', debug=True)