blob: 87a2cd9a9477f48f3db3ebb23313db04ea170c92 [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 Ansari2ecc6522021-12-03 21:03:48 +000011from person_detection import Camera
Shad Ansari30a23732021-09-29 23:07:21 -070012
Shad Ansari30a23732021-09-29 23:07:21 -070013
14app = Flask(__name__)
15
16
17@app.route('/')
18def index():
19 """Video streaming home page."""
Shad Ansari33340292021-10-14 22:10:01 +000020 return render_template('index.html', devices=[0, 1, 2, 3])
Shad Ansari30a23732021-09-29 23:07:21 -070021
22
23def gen(camera):
24 """Video streaming generator function."""
Shad Ansari30a23732021-09-29 23:07:21 -070025 while True:
26 frame = camera.get_frame()
27 yield (b'--frame\r\n'
28 b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
29
Shad Ansarib5808d72021-10-04 12:43:27 -070030@app.route('/video_feed/<device>')
31def video_feed(device):
Shad Ansari30a23732021-09-29 23:07:21 -070032 """Video streaming route. Put this in the src attribute of an img tag."""
Shad Ansarib5808d72021-10-04 12:43:27 -070033 global args
Shad Ansaric0726e62021-10-04 22:38:53 +000034 camera = Camera(device, args)
Shad Ansari30a23732021-09-29 23:07:21 -070035 return Response(gen(camera),
36 mimetype='multipart/x-mixed-replace; boundary=frame')
37
Shad Ansarib5808d72021-10-04 12:43:27 -070038def name_to_port(name):
39 return int(name)
40
Shad Ansari30a23732021-09-29 23:07:21 -070041def build_argparser():
42 parser = ArgumentParser(add_help=False)
43 args = parser.add_argument_group('Options')
44 args.add_argument('-h', '--help', action='help', default=SUPPRESS, help='Show this help message and exit.')
45 args.add_argument("-m", "--model", help="Required. Path to an .xml file with a trained model.",
46 required=True, type=str)
47 args.add_argument("-i", "--input",
Shad Ansaria2714652021-10-23 00:10:18 +000048 help="Path to video file or image. 'cam' for capturing video stream from camera",
49 default = "gstreamer", type=str)
Shad Ansari30a23732021-09-29 23:07:21 -070050 args.add_argument("-pt", "--prob_threshold", help="Optional. Probability threshold for detections filtering",
Shad Ansaria2714652021-10-23 00:10:18 +000051 default=0.0, type=float)
Shad Ansari60ca8cc2021-11-02 18:46:44 +000052 args.add_argument("--idle", action='store_true', help="Idle if no clients connected")
Shad Ansari30a23732021-09-29 23:07:21 -070053
54 return parser
55
Shad Ansarib5808d72021-10-04 12:43:27 -070056
Shad Ansari30a23732021-09-29 23:07:21 -070057if __name__ == '__main__':
58 args = build_argparser().parse_args()
Shad Ansarid70cb9e2021-10-26 05:33:02 +000059 app.run(host='0.0.0.0', debug=True)