Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | from importlib import import_module |
| 3 | import os |
| 4 | from flask import Flask, render_template, Response |
| 5 | from argparse import ArgumentParser, SUPPRESS |
| 6 | |
| 7 | # import camera driver |
| 8 | if os.environ.get('CAMERA'): |
| 9 | Camera = import_module('camera_' + os.environ['CAMERA']).Camera |
| 10 | else: |
| 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 | |
| 17 | app = Flask(__name__) |
| 18 | |
| 19 | |
| 20 | @app.route('/') |
| 21 | def index(): |
| 22 | """Video streaming home page.""" |
| 23 | return render_template('index.html') |
| 24 | |
| 25 | |
| 26 | def gen(camera): |
| 27 | """Video streaming generator function.""" |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 28 | 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 | |
| 33 | |
| 34 | @app.route('/video_feed') |
| 35 | def video_feed(): |
| 36 | """Video streaming route. Put this in the src attribute of an img tag.""" |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 37 | camera = Camera(args) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 38 | return Response(gen(camera), |
| 39 | mimetype='multipart/x-mixed-replace; boundary=frame') |
| 40 | |
| 41 | def build_argparser(): |
| 42 | parser = ArgumentParser(add_help=False) |
| 43 | args = parser.add_argument_group('Options') |
| 44 | args.add_argument('-h', '--help', action='help', default=SUPPRESS, help='Show this help message and exit.') |
| 45 | args.add_argument("-m", "--model", help="Required. Path to an .xml file with a trained model.", |
| 46 | required=True, type=str) |
| 47 | args.add_argument("-i", "--input", |
| 48 | help="Required. Path to video file or image. 'cam' for capturing video stream from camera", |
| 49 | required=True, type=str) |
| 50 | args.add_argument("-l", "--cpu_extension", |
| 51 | help="Optional. Required for CPU custom layers. Absolute path to a shared library with the " |
| 52 | "kernels implementations.", type=str, default=None) |
| 53 | args.add_argument("-pp", "--plugin_dir", help="Optional. Path to a plugin folder", type=str, default=None) |
| 54 | args.add_argument("-d", "--device", |
| 55 | help="Optional. Specify the target device to infer on; CPU, GPU, FPGA, HDDL or MYRIAD is " |
| 56 | "acceptable. The demo will look for a suitable plugin for device specified. " |
| 57 | "Default value is CPU", default="CPU", type=str) |
| 58 | args.add_argument("--labels", help="Optional. Path to labels mapping file", default=None, type=str) |
| 59 | args.add_argument("-pt", "--prob_threshold", help="Optional. Probability threshold for detections filtering", |
| 60 | default=0.5, type=float) |
| 61 | args.add_argument("-ns", help='No show output', action='store_true') |
| 62 | |
| 63 | return parser |
| 64 | |
| 65 | if __name__ == '__main__': |
| 66 | args = build_argparser().parse_args() |
| 67 | app.run(host='0.0.0.0', threaded=True) |