blob: 2ef9b874d4bda1a3d427911b8035560fbef800fb [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)
shadf64b92a2022-01-13 19:06:29 +000073 args.add_argument("--user",
74 help = "ROC username",
75 type = str)
76 args.add_argument("--password",
77 help = "ROC password",
78 type = str)
Shad Ansari5e8d0692021-12-08 19:09:34 +000079 args.add_argument("--mbrlow",
Shad Ansari35019042022-03-29 20:15:54 +000080 help="Low range of MBR",
81 default=7000000,
82 type=int)
Shad Ansari5e8d0692021-12-08 19:09:34 +000083 args.add_argument("--mbrhigh",
Shad Ansari35019042022-03-29 20:15:54 +000084 help="High range of MBR",
85 default=10000000,
86 type=int)
Shad Ansari5e8d0692021-12-08 19:09:34 +000087 args.add_argument("--devicegroup",
Shad Ansari35019042022-03-29 20:15:54 +000088 help="Camera device group",
89 default="menlo-4g-cameras",
90 type=str)
Shad Ansari30a23732021-09-29 23:07:21 -070091
92 return parser
93
Shad Ansarib5808d72021-10-04 12:43:27 -070094
Shad Ansari30a23732021-09-29 23:07:21 -070095if __name__ == '__main__':
shadf64b92a2022-01-13 19:06:29 +000096 log.basicConfig(
97 format='%(asctime)s %(levelname)-8s %(message)s',
98 level=log.DEBUG,
99 datefmt='%Y-%m-%d %H:%M:%S',
100 stream=sys.stdout)
Shad Ansari2eddc1e2022-01-10 17:53:06 +0000101 log.debug("Starting person detection app")
shadf64b92a2022-01-13 19:06:29 +0000102
Shad Ansari30a23732021-09-29 23:07:21 -0700103 args = build_argparser().parse_args()
shadf64b92a2022-01-13 19:06:29 +0000104
Shad Ansari2eddc1e2022-01-10 17:53:06 +0000105 if not args.noroc:
shadf64b92a2022-01-13 19:06:29 +0000106 key = args.key
107 if key is None:
108 if args.user is not None and args.password is not None:
Shad Ansari35019042022-03-29 20:15:54 +0000109 roc = Roc(config.url, args.user, args.password, config.enterprise, config.site)
shadf64b92a2022-01-13 19:06:29 +0000110 key = roc.get_key()
111 else:
112 log.error("Either key or user/password required")
113 sys.exit()
114
115 log.info("Device group:{} mbr:{}(low)".format(args.devicegroup, args.mbrlow))
116 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)