blob: 0ed28eb22670f4fee121b37f33b4c7a39d271e2e [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 Ansari30a23732021-09-29 23:07:21 -07006from importlib import import_module
7import os
Shad Ansari2eddc1e2022-01-10 17:53:06 +00008import logging as log
9import sys
10import flask
Shad Ansari30a23732021-09-29 23:07:21 -070011from argparse import ArgumentParser, SUPPRESS
12
Shad Ansari9a95c3d2021-12-09 04:32:09 +000013import config
Shad Ansari2ecc6522021-12-03 21:03:48 +000014from person_detection import Camera
shadf64b92a2022-01-13 19:06:29 +000015from roc import Roc
Shad Ansari30a23732021-09-29 23:07:21 -070016
Shad Ansari30a23732021-09-29 23:07:21 -070017
Shad Ansari2eddc1e2022-01-10 17:53:06 +000018app = flask.Flask(__name__)
Shad Ansari30a23732021-09-29 23:07:21 -070019
20
21@app.route('/')
22def index():
23 """Video streaming home page."""
Shad Ansari2eddc1e2022-01-10 17:53:06 +000024 log.info("{} - connected".format(flask.request.remote_addr))
25 return flask.render_template('index.html', devices=config.cameras)
Shad Ansari30a23732021-09-29 23:07:21 -070026
27def gen(camera):
28 """Video streaming generator function."""
Shad Ansari30a23732021-09-29 23:07:21 -070029 while True:
30 frame = camera.get_frame()
31 yield (b'--frame\r\n'
32 b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
33
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 Ansari30a23732021-09-29 23:07:21 -070041 mimetype='multipart/x-mixed-replace; boundary=frame')
42
Shad Ansarib5808d72021-10-04 12:43:27 -070043def name_to_port(name):
44 return int(name)
45
Shad Ansari30a23732021-09-29 23:07:21 -070046def build_argparser():
47 parser = ArgumentParser(add_help=False)
48 args = parser.add_argument_group('Options')
Shad Ansari5e8d0692021-12-08 19:09:34 +000049 args.add_argument('-h', '--help',
50 action = 'help',
51 default = SUPPRESS,
52 help = 'Show this help message and exit.')
53 args.add_argument("-m", "--model",
54 help = "Required. Path to an .xml file with a trained model.",
55 required = True,
56 type = str)
Shad Ansari30a23732021-09-29 23:07:21 -070057 args.add_argument("-i", "--input",
Shad Ansari5e8d0692021-12-08 19:09:34 +000058 help = "Path to video file or image. 'cam' for capturing video stream from camera",
59 default = "gstreamer",
60 type = str)
61 args.add_argument("-pt", "--prob_threshold",
62 help = "Optional. Probability threshold for detections filtering",
63 default = 0.75,
64 type = float)
Shad Ansariec6bbd32021-12-10 20:57:16 +000065 args.add_argument("--noroc",
Shad Ansari2eddc1e2022-01-10 17:53:06 +000066 action = 'store_true',
Shad Ansariec6bbd32021-12-10 20:57:16 +000067 help = "No ROC")
Shad Ansari5e8d0692021-12-08 19:09:34 +000068 args.add_argument("--key",
69 help = "ROC api key",
Shad Ansari5e8d0692021-12-08 19:09:34 +000070 type = str)
shadf64b92a2022-01-13 19:06:29 +000071 args.add_argument("--user",
72 help = "ROC username",
73 type = str)
74 args.add_argument("--password",
75 help = "ROC password",
76 type = str)
Shad Ansari5e8d0692021-12-08 19:09:34 +000077 args.add_argument("--mbrlow",
78 help = "Low range of MBR",
Shad Ansariec6bbd32021-12-10 20:57:16 +000079 default = 7000000,
Shad Ansari5e8d0692021-12-08 19:09:34 +000080 type = int)
81 args.add_argument("--mbrhigh",
82 help = "High range of MBR",
83 default = 10000000,
84 type = int)
85 args.add_argument("--devicegroup",
86 help = "Camera device group",
87 default = "menlo-4g-cameras",
88 type = str)
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:
107 roc = Roc(args.user, args.password)
108 key = roc.get_key()
109 else:
110 log.error("Either key or user/password required")
111 sys.exit()
112
113 log.info("Device group:{} mbr:{}(low)".format(args.devicegroup, args.mbrlow))
114 roc.set_mbr(args.devicegroup, args.mbrlow)
115
Shad Ansarid70cb9e2021-10-26 05:33:02 +0000116 app.run(host='0.0.0.0', debug=True)