| #!/usr/bin/env python |
| from importlib import import_module |
| import os |
| from flask import Flask, render_template, Response |
| from argparse import ArgumentParser, SUPPRESS |
| |
| # import camera driver |
| if os.environ.get('CAMERA'): |
| Camera = import_module('camera_' + os.environ['CAMERA']).Camera |
| else: |
| # from camera import Camera |
| from person_detection import Camera |
| |
| # Raspberry Pi camera module (requires picamera package) |
| # from camera_pi import Camera |
| |
| app = Flask(__name__) |
| |
| |
| @app.route('/') |
| def index(): |
| """Video streaming home page.""" |
| return render_template('index.html', devices=[0, 1]) |
| |
| |
| def gen(camera): |
| """Video streaming generator function.""" |
| while True: |
| frame = camera.get_frame() |
| yield (b'--frame\r\n' |
| b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') |
| |
| @app.route('/video_feed/<device>') |
| def video_feed(device): |
| """Video streaming route. Put this in the src attribute of an img tag.""" |
| global args |
| camera = Camera(int(device), args) |
| return Response(gen(camera), |
| mimetype='multipart/x-mixed-replace; boundary=frame') |
| |
| def name_to_port(name): |
| return int(name) |
| |
| def build_argparser(): |
| parser = ArgumentParser(add_help=False) |
| args = parser.add_argument_group('Options') |
| args.add_argument('-h', '--help', action='help', default=SUPPRESS, help='Show this help message and exit.') |
| args.add_argument("-m", "--model", help="Required. Path to an .xml file with a trained model.", |
| required=True, type=str) |
| args.add_argument("-i", "--input", |
| help="Required. Path to video file or image. 'cam' for capturing video stream from camera", |
| required=True, type=str) |
| args.add_argument("-l", "--cpu_extension", |
| help="Optional. Required for CPU custom layers. Absolute path to a shared library with the " |
| "kernels implementations.", type=str, default=None) |
| args.add_argument("-pp", "--plugin_dir", help="Optional. Path to a plugin folder", type=str, default=None) |
| args.add_argument("-d", "--device", |
| help="Optional. Specify the target device to infer on; CPU, GPU, FPGA, HDDL or MYRIAD is " |
| "acceptable. The demo will look for a suitable plugin for device specified. " |
| "Default value is CPU", default="CPU", type=str) |
| args.add_argument("--labels", help="Optional. Path to labels mapping file", default=None, type=str) |
| args.add_argument("-pt", "--prob_threshold", help="Optional. Probability threshold for detections filtering", |
| default=0.5, type=float) |
| |
| return parser |
| |
| |
| if __name__ == '__main__': |
| args = build_argparser().parse_args() |
| app.run(host='0.0.0.0', threaded=True) |