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: |
Shad Ansari | b5808d7 | 2021-10-04 12:43:27 -0700 | [diff] [blame^] | 11 | # from camera import Camera |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 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.""" |
Shad Ansari | b5808d7 | 2021-10-04 12:43:27 -0700 | [diff] [blame^] | 23 | return render_template('index.html', devices=[0, 1]) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 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 | |
Shad Ansari | b5808d7 | 2021-10-04 12:43:27 -0700 | [diff] [blame^] | 33 | @app.route('/video_feed/<device>') |
| 34 | def video_feed(device): |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 35 | """Video streaming route. Put this in the src attribute of an img tag.""" |
Shad Ansari | b5808d7 | 2021-10-04 12:43:27 -0700 | [diff] [blame^] | 36 | global args |
| 37 | camera = Camera(int(device), 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 | |
Shad Ansari | b5808d7 | 2021-10-04 12:43:27 -0700 | [diff] [blame^] | 41 | def name_to_port(name): |
| 42 | return int(name) |
| 43 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 44 | def 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 Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 64 | |
| 65 | return parser |
| 66 | |
Shad Ansari | b5808d7 | 2021-10-04 12:43:27 -0700 | [diff] [blame^] | 67 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 68 | if __name__ == '__main__': |
| 69 | args = build_argparser().parse_args() |
| 70 | app.run(host='0.0.0.0', threaded=True) |