Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
Shad Ansari | ae90d25 | 2021-10-22 16:40:29 +0000 | [diff] [blame^] | 2 | """ |
| 3 | SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org> |
| 4 | SPDX-License-Identifier: LicenseRef-ONF-Member-1.01 |
| 5 | """ |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 6 | from importlib import import_module |
| 7 | import os |
| 8 | from flask import Flask, render_template, Response |
| 9 | from argparse import ArgumentParser, SUPPRESS |
| 10 | |
| 11 | # import camera driver |
| 12 | if os.environ.get('CAMERA'): |
| 13 | Camera = import_module('camera_' + os.environ['CAMERA']).Camera |
| 14 | else: |
Shad Ansari | b5808d7 | 2021-10-04 12:43:27 -0700 | [diff] [blame] | 15 | # from camera import Camera |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 16 | from person_detection import Camera |
| 17 | |
| 18 | # Raspberry Pi camera module (requires picamera package) |
| 19 | # from camera_pi import Camera |
| 20 | |
| 21 | app = Flask(__name__) |
| 22 | |
| 23 | |
| 24 | @app.route('/') |
| 25 | def index(): |
| 26 | """Video streaming home page.""" |
Shad Ansari | 3334029 | 2021-10-14 22:10:01 +0000 | [diff] [blame] | 27 | return render_template('index.html', devices=[0, 1, 2, 3]) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 28 | |
| 29 | |
| 30 | def gen(camera): |
| 31 | """Video streaming generator function.""" |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 32 | while True: |
| 33 | frame = camera.get_frame() |
| 34 | yield (b'--frame\r\n' |
| 35 | b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') |
| 36 | |
Shad Ansari | b5808d7 | 2021-10-04 12:43:27 -0700 | [diff] [blame] | 37 | @app.route('/video_feed/<device>') |
| 38 | def video_feed(device): |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 39 | """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] | 40 | global args |
Shad Ansari | c0726e6 | 2021-10-04 22:38:53 +0000 | [diff] [blame] | 41 | camera = Camera(device, args) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 42 | return Response(gen(camera), |
| 43 | mimetype='multipart/x-mixed-replace; boundary=frame') |
| 44 | |
Shad Ansari | b5808d7 | 2021-10-04 12:43:27 -0700 | [diff] [blame] | 45 | def name_to_port(name): |
| 46 | return int(name) |
| 47 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 48 | def build_argparser(): |
| 49 | parser = ArgumentParser(add_help=False) |
| 50 | args = parser.add_argument_group('Options') |
| 51 | args.add_argument('-h', '--help', action='help', default=SUPPRESS, help='Show this help message and exit.') |
| 52 | args.add_argument("-m", "--model", help="Required. Path to an .xml file with a trained model.", |
| 53 | required=True, type=str) |
| 54 | args.add_argument("-i", "--input", |
| 55 | help="Required. Path to video file or image. 'cam' for capturing video stream from camera", |
| 56 | required=True, type=str) |
| 57 | args.add_argument("-l", "--cpu_extension", |
| 58 | help="Optional. Required for CPU custom layers. Absolute path to a shared library with the " |
| 59 | "kernels implementations.", type=str, default=None) |
| 60 | args.add_argument("-pp", "--plugin_dir", help="Optional. Path to a plugin folder", type=str, default=None) |
| 61 | args.add_argument("-d", "--device", |
| 62 | help="Optional. Specify the target device to infer on; CPU, GPU, FPGA, HDDL or MYRIAD is " |
| 63 | "acceptable. The demo will look for a suitable plugin for device specified. " |
| 64 | "Default value is CPU", default="CPU", type=str) |
| 65 | args.add_argument("--labels", help="Optional. Path to labels mapping file", default=None, type=str) |
| 66 | args.add_argument("-pt", "--prob_threshold", help="Optional. Probability threshold for detections filtering", |
| 67 | default=0.5, type=float) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 68 | |
| 69 | return parser |
| 70 | |
Shad Ansari | b5808d7 | 2021-10-04 12:43:27 -0700 | [diff] [blame] | 71 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 72 | if __name__ == '__main__': |
| 73 | args = build_argparser().parse_args() |
| 74 | app.run(host='0.0.0.0', threaded=True) |