blob: 83a8d3db3d80889cb0b3863a71df4fb6065bb5c9 [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 Ansari58644202021-11-01 15:04:43 +000011import fpstimer
12
13
Shad Ansari30a23732021-09-29 23:07:21 -070014# import camera driver
15if os.environ.get('CAMERA'):
16 Camera = import_module('camera_' + os.environ['CAMERA']).Camera
17else:
Shad Ansarib5808d72021-10-04 12:43:27 -070018 # from camera import Camera
Shad Ansari30a23732021-09-29 23:07:21 -070019 from person_detection import Camera
20
21# Raspberry Pi camera module (requires picamera package)
22# from camera_pi import Camera
23
24app = Flask(__name__)
25
26
27@app.route('/')
28def index():
29 """Video streaming home page."""
Shad Ansari33340292021-10-14 22:10:01 +000030 return render_template('index.html', devices=[0, 1, 2, 3])
Shad Ansari30a23732021-09-29 23:07:21 -070031
32
33def gen(camera):
34 """Video streaming generator function."""
Shad Ansari58644202021-11-01 15:04:43 +000035 timer = fpstimer.FPSTimer(fps)
Shad Ansari30a23732021-09-29 23:07:21 -070036 while True:
37 frame = camera.get_frame()
38 yield (b'--frame\r\n'
39 b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
Shad Ansari58644202021-11-01 15:04:43 +000040 timer.sleep()
Shad Ansari30a23732021-09-29 23:07:21 -070041
Shad Ansarib5808d72021-10-04 12:43:27 -070042@app.route('/video_feed/<device>')
43def video_feed(device):
Shad Ansari30a23732021-09-29 23:07:21 -070044 """Video streaming route. Put this in the src attribute of an img tag."""
Shad Ansarib5808d72021-10-04 12:43:27 -070045 global args
Shad Ansaric0726e62021-10-04 22:38:53 +000046 camera = Camera(device, args)
Shad Ansari30a23732021-09-29 23:07:21 -070047 return Response(gen(camera),
48 mimetype='multipart/x-mixed-replace; boundary=frame')
49
Shad Ansarib5808d72021-10-04 12:43:27 -070050def name_to_port(name):
51 return int(name)
52
Shad Ansari30a23732021-09-29 23:07:21 -070053def build_argparser():
54 parser = ArgumentParser(add_help=False)
55 args = parser.add_argument_group('Options')
56 args.add_argument('-h', '--help', action='help', default=SUPPRESS, help='Show this help message and exit.')
57 args.add_argument("-m", "--model", help="Required. Path to an .xml file with a trained model.",
58 required=True, type=str)
59 args.add_argument("-i", "--input",
Shad Ansaria2714652021-10-23 00:10:18 +000060 help="Path to video file or image. 'cam' for capturing video stream from camera",
61 default = "gstreamer", type=str)
Shad Ansari30a23732021-09-29 23:07:21 -070062 args.add_argument("-pt", "--prob_threshold", help="Optional. Probability threshold for detections filtering",
Shad Ansaria2714652021-10-23 00:10:18 +000063 default=0.0, type=float)
Shad Ansari58644202021-11-01 15:04:43 +000064 args.add_argument("-fps", "--frames_per_sec", help="Frames per second",
65 default=15, type=float)
Shad Ansari30a23732021-09-29 23:07:21 -070066
67 return parser
68
Shad Ansarib5808d72021-10-04 12:43:27 -070069
Shad Ansari30a23732021-09-29 23:07:21 -070070if __name__ == '__main__':
71 args = build_argparser().parse_args()
Shad Ansari58644202021-11-01 15:04:43 +000072 fps = args.frames_per_sec
Shad Ansarid70cb9e2021-10-26 05:33:02 +000073 app.run(host='0.0.0.0', debug=True)