blob: 1386d63af53bd789aa6963c3762465e76a761e8b [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
11# import camera driver
12if os.environ.get('CAMERA'):
13 Camera = import_module('camera_' + os.environ['CAMERA']).Camera
14else:
Shad Ansarib5808d72021-10-04 12:43:27 -070015 # from camera import Camera
Shad Ansari30a23732021-09-29 23:07:21 -070016 from person_detection import Camera
17
18# Raspberry Pi camera module (requires picamera package)
19# from camera_pi import Camera
20
21app = Flask(__name__)
22
23
24@app.route('/')
25def index():
26 """Video streaming home page."""
Shad Ansari33340292021-10-14 22:10:01 +000027 return render_template('index.html', devices=[0, 1, 2, 3])
Shad Ansari30a23732021-09-29 23:07:21 -070028
29
30def gen(camera):
31 """Video streaming generator function."""
Shad Ansari30a23732021-09-29 23:07:21 -070032 while True:
33 frame = camera.get_frame()
34 yield (b'--frame\r\n'
35 b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
36
Shad Ansarib5808d72021-10-04 12:43:27 -070037@app.route('/video_feed/<device>')
38def video_feed(device):
Shad Ansari30a23732021-09-29 23:07:21 -070039 """Video streaming route. Put this in the src attribute of an img tag."""
Shad Ansarib5808d72021-10-04 12:43:27 -070040 global args
Shad Ansaric0726e62021-10-04 22:38:53 +000041 camera = Camera(device, args)
Shad Ansari30a23732021-09-29 23:07:21 -070042 return Response(gen(camera),
43 mimetype='multipart/x-mixed-replace; boundary=frame')
44
Shad Ansarib5808d72021-10-04 12:43:27 -070045def name_to_port(name):
46 return int(name)
47
Shad Ansari30a23732021-09-29 23:07:21 -070048def build_argparser():
49 parser = ArgumentParser(add_help=False)
50 args = parser.add_argument_group('Options')
51 args.add_argument('-h', '--help', action='help', default=SUPPRESS, help='Show this help message and exit.')
52 args.add_argument("-m", "--model", help="Required. Path to an .xml file with a trained model.",
53 required=True, type=str)
54 args.add_argument("-i", "--input",
Shad Ansaria2714652021-10-23 00:10:18 +000055 help="Path to video file or image. 'cam' for capturing video stream from camera",
56 default = "gstreamer", type=str)
Shad Ansari30a23732021-09-29 23:07:21 -070057 args.add_argument("-pt", "--prob_threshold", help="Optional. Probability threshold for detections filtering",
Shad Ansaria2714652021-10-23 00:10:18 +000058 default=0.0, type=float)
Shad Ansari60ca8cc2021-11-02 18:46:44 +000059 args.add_argument("--idle", action='store_true', help="Idle if no clients connected")
Shad Ansari30a23732021-09-29 23:07:21 -070060
61 return parser
62
Shad Ansarib5808d72021-10-04 12:43:27 -070063
Shad Ansari30a23732021-09-29 23:07:21 -070064if __name__ == '__main__':
65 args = build_argparser().parse_args()
Shad Ansarid70cb9e2021-10-26 05:33:02 +000066 app.run(host='0.0.0.0', debug=True)