blob: bc72e9630988b8919ee4587f6cd62241a478ec62 [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
8from flask import Flask, render_template, Response
9from 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
Shad Ansari30a23732021-09-29 23:07:21 -070013
Shad Ansari30a23732021-09-29 23:07:21 -070014
15app = Flask(__name__)
16
17
18@app.route('/')
19def index():
Shad Ansari9a95c3d2021-12-09 04:32:09 +000020 global cameras
Shad Ansari30a23732021-09-29 23:07:21 -070021 """Video streaming home page."""
Shad Ansari9a95c3d2021-12-09 04:32:09 +000022 return render_template('index.html', devices=config.cameras)
Shad Ansari30a23732021-09-29 23:07:21 -070023
24def gen(camera):
25 """Video streaming generator function."""
Shad Ansari30a23732021-09-29 23:07:21 -070026 while True:
27 frame = camera.get_frame()
28 yield (b'--frame\r\n'
29 b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
30
Shad Ansarib5808d72021-10-04 12:43:27 -070031@app.route('/video_feed/<device>')
32def video_feed(device):
Shad Ansari30a23732021-09-29 23:07:21 -070033 """Video streaming route. Put this in the src attribute of an img tag."""
Shad Ansarib5808d72021-10-04 12:43:27 -070034 global args
Shad Ansaric0726e62021-10-04 22:38:53 +000035 camera = Camera(device, args)
Shad Ansari30a23732021-09-29 23:07:21 -070036 return Response(gen(camera),
37 mimetype='multipart/x-mixed-replace; boundary=frame')
38
Shad Ansarib5808d72021-10-04 12:43:27 -070039def name_to_port(name):
40 return int(name)
41
Shad Ansari30a23732021-09-29 23:07:21 -070042def build_argparser():
43 parser = ArgumentParser(add_help=False)
44 args = parser.add_argument_group('Options')
Shad Ansari5e8d0692021-12-08 19:09:34 +000045 args.add_argument('-h', '--help',
46 action = 'help',
47 default = SUPPRESS,
48 help = 'Show this help message and exit.')
49 args.add_argument("-m", "--model",
50 help = "Required. Path to an .xml file with a trained model.",
51 required = True,
52 type = str)
Shad Ansari30a23732021-09-29 23:07:21 -070053 args.add_argument("-i", "--input",
Shad Ansari5e8d0692021-12-08 19:09:34 +000054 help = "Path to video file or image. 'cam' for capturing video stream from camera",
55 default = "gstreamer",
56 type = str)
57 args.add_argument("-pt", "--prob_threshold",
58 help = "Optional. Probability threshold for detections filtering",
59 default = 0.75,
60 type = float)
61 args.add_argument("--idle",
62 action = 'store_true',
63 help = "Idle if no clients connected")
64 args.add_argument("--key",
65 help = "ROC api key",
66 required = True,
67 type = str)
68 args.add_argument("--mbrlow",
69 help = "Low range of MBR",
70 default = 5000000,
71 type = int)
72 args.add_argument("--mbrhigh",
73 help = "High range of MBR",
74 default = 10000000,
75 type = int)
76 args.add_argument("--devicegroup",
77 help = "Camera device group",
78 default = "menlo-4g-cameras",
79 type = str)
Shad Ansari30a23732021-09-29 23:07:21 -070080
81 return parser
82
Shad Ansarib5808d72021-10-04 12:43:27 -070083
Shad Ansari30a23732021-09-29 23:07:21 -070084if __name__ == '__main__':
85 args = build_argparser().parse_args()
Shad Ansarid70cb9e2021-10-26 05:33:02 +000086 app.run(host='0.0.0.0', debug=True)