blob: 1cd5bb5145a67e2deabdce2ead43d8e93733e64e [file] [log] [blame]
Shad Ansari30a23732021-09-29 23:07:21 -07001#!/usr/bin/env python
2from importlib import import_module
3import os
4from flask import Flask, render_template, Response
5from argparse import ArgumentParser, SUPPRESS
6
7# import camera driver
8if os.environ.get('CAMERA'):
9 Camera = import_module('camera_' + os.environ['CAMERA']).Camera
10else:
Shad Ansarib5808d72021-10-04 12:43:27 -070011 # from camera import Camera
Shad Ansari30a23732021-09-29 23:07:21 -070012 from person_detection import Camera
13
14# Raspberry Pi camera module (requires picamera package)
15# from camera_pi import Camera
16
17app = Flask(__name__)
18
19
20@app.route('/')
21def index():
22 """Video streaming home page."""
Shad Ansari33340292021-10-14 22:10:01 +000023 return render_template('index.html', devices=[0, 1, 2, 3])
Shad Ansari30a23732021-09-29 23:07:21 -070024
25
26def gen(camera):
27 """Video streaming generator function."""
Shad Ansari30a23732021-09-29 23:07:21 -070028 while True:
29 frame = camera.get_frame()
30 yield (b'--frame\r\n'
31 b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
32
Shad Ansarib5808d72021-10-04 12:43:27 -070033@app.route('/video_feed/<device>')
34def video_feed(device):
Shad Ansari30a23732021-09-29 23:07:21 -070035 """Video streaming route. Put this in the src attribute of an img tag."""
Shad Ansarib5808d72021-10-04 12:43:27 -070036 global args
Shad Ansaric0726e62021-10-04 22:38:53 +000037 camera = Camera(device, args)
Shad Ansari30a23732021-09-29 23:07:21 -070038 return Response(gen(camera),
39 mimetype='multipart/x-mixed-replace; boundary=frame')
40
Shad Ansarib5808d72021-10-04 12:43:27 -070041def name_to_port(name):
42 return int(name)
43
Shad Ansari30a23732021-09-29 23:07:21 -070044def build_argparser():
45 parser = ArgumentParser(add_help=False)
46 args = parser.add_argument_group('Options')
47 args.add_argument('-h', '--help', action='help', default=SUPPRESS, help='Show this help message and exit.')
48 args.add_argument("-m", "--model", help="Required. Path to an .xml file with a trained model.",
49 required=True, type=str)
50 args.add_argument("-i", "--input",
51 help="Required. Path to video file or image. 'cam' for capturing video stream from camera",
52 required=True, type=str)
53 args.add_argument("-l", "--cpu_extension",
54 help="Optional. Required for CPU custom layers. Absolute path to a shared library with the "
55 "kernels implementations.", type=str, default=None)
56 args.add_argument("-pp", "--plugin_dir", help="Optional. Path to a plugin folder", type=str, default=None)
57 args.add_argument("-d", "--device",
58 help="Optional. Specify the target device to infer on; CPU, GPU, FPGA, HDDL or MYRIAD is "
59 "acceptable. The demo will look for a suitable plugin for device specified. "
60 "Default value is CPU", default="CPU", type=str)
61 args.add_argument("--labels", help="Optional. Path to labels mapping file", default=None, type=str)
62 args.add_argument("-pt", "--prob_threshold", help="Optional. Probability threshold for detections filtering",
63 default=0.5, type=float)
Shad Ansari30a23732021-09-29 23:07:21 -070064
65 return parser
66
Shad Ansarib5808d72021-10-04 12:43:27 -070067
Shad Ansari30a23732021-09-29 23:07:21 -070068if __name__ == '__main__':
69 args = build_argparser().parse_args()
70 app.run(host='0.0.0.0', threaded=True)