blob: 1b1fbde955f326c3511afa90450ef95c4dfa838e [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:
11 #from camera import Camera
12 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."""
23 return render_template('index.html')
24
25
26def gen(camera):
27 """Video streaming generator function."""
28 print("Video streaming generator function.")
29 while True:
30 frame = camera.get_frame()
31 yield (b'--frame\r\n'
32 b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
33
34
35@app.route('/video_feed')
36def video_feed():
37 """Video streaming route. Put this in the src attribute of an img tag."""
38 print("video_feed()", args)
39 camera = Camera(args)
40 print("Camera: ", camera)
41 return Response(gen(camera),
42 mimetype='multipart/x-mixed-replace; boundary=frame')
43
44def 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)
64 args.add_argument("-ns", help='No show output', action='store_true')
65
66 return parser
67
68if __name__ == '__main__':
69 args = build_argparser().parse_args()
70 app.run(host='0.0.0.0', threaded=True)