blob: b1e9b2d1ea3a1658c29d45304dab872553662b83 [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
Shad Ansari2eddc1e2022-01-10 17:53:06 +000015import 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():
Shad Ansari9a95c3d2021-12-09 04:32:09 +000023 global cameras
Shad Ansari30a23732021-09-29 23:07:21 -070024 """Video streaming home page."""
Shad Ansari2eddc1e2022-01-10 17:53:06 +000025 log.info("{} - connected".format(flask.request.remote_addr))
26 return flask.render_template('index.html', devices=config.cameras)
Shad Ansari30a23732021-09-29 23:07:21 -070027
28def 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 Ansarib5808d72021-10-04 12:43:27 -070035@app.route('/video_feed/<device>')
36def video_feed(device):
Shad Ansari30a23732021-09-29 23:07:21 -070037 """Video streaming route. Put this in the src attribute of an img tag."""
Shad Ansarib5808d72021-10-04 12:43:27 -070038 global args
Shad Ansari2eddc1e2022-01-10 17:53:06 +000039 log.debug("{} - video feed {}".format(flask.request.remote_addr, device))
40 camera = Camera(device, flask.request.remote_addr, args)
41 return flask.Response(gen(camera),
Shad Ansari30a23732021-09-29 23:07:21 -070042 mimetype='multipart/x-mixed-replace; boundary=frame')
43
Shad Ansarib5808d72021-10-04 12:43:27 -070044def name_to_port(name):
45 return int(name)
46
Shad Ansari30a23732021-09-29 23:07:21 -070047def build_argparser():
48 parser = ArgumentParser(add_help=False)
49 args = parser.add_argument_group('Options')
Shad Ansari5e8d0692021-12-08 19:09:34 +000050 args.add_argument('-h', '--help',
51 action = 'help',
52 default = SUPPRESS,
53 help = 'Show this help message and exit.')
54 args.add_argument("-m", "--model",
55 help = "Required. Path to an .xml file with a trained model.",
56 required = True,
57 type = str)
Shad Ansari30a23732021-09-29 23:07:21 -070058 args.add_argument("-i", "--input",
Shad Ansari5e8d0692021-12-08 19:09:34 +000059 help = "Path to video file or image. 'cam' for capturing video stream from camera",
60 default = "gstreamer",
61 type = str)
62 args.add_argument("-pt", "--prob_threshold",
63 help = "Optional. Probability threshold for detections filtering",
64 default = 0.75,
65 type = float)
Shad Ansariec6bbd32021-12-10 20:57:16 +000066 args.add_argument("--noroc",
Shad Ansari2eddc1e2022-01-10 17:53:06 +000067 action = 'store_true',
Shad Ansariec6bbd32021-12-10 20:57:16 +000068 help = "No ROC")
Shad Ansari5e8d0692021-12-08 19:09:34 +000069 args.add_argument("--key",
70 help = "ROC api key",
Shad Ansari5e8d0692021-12-08 19:09:34 +000071 type = str)
72 args.add_argument("--mbrlow",
73 help = "Low range of MBR",
Shad Ansariec6bbd32021-12-10 20:57:16 +000074 default = 7000000,
Shad Ansari5e8d0692021-12-08 19:09:34 +000075 type = int)
76 args.add_argument("--mbrhigh",
77 help = "High range of MBR",
78 default = 10000000,
79 type = int)
80 args.add_argument("--devicegroup",
81 help = "Camera device group",
82 default = "menlo-4g-cameras",
83 type = str)
Shad Ansari30a23732021-09-29 23:07:21 -070084
85 return parser
86
Shad Ansarib5808d72021-10-04 12:43:27 -070087
Shad Ansari30a23732021-09-29 23:07:21 -070088if __name__ == '__main__':
Shad Ansari2eddc1e2022-01-10 17:53:06 +000089 log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.DEBUG, stream=sys.stdout)
90 log.debug("Starting person detection app")
Shad Ansari30a23732021-09-29 23:07:21 -070091 args = build_argparser().parse_args()
Shad Ansari2eddc1e2022-01-10 17:53:06 +000092 if not args.noroc:
93 roc.set_mbr(args.key, args.devicegroup, args.mbrlow)
Shad Ansarid70cb9e2021-10-26 05:33:02 +000094 app.run(host='0.0.0.0', debug=True)