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 | |
Shad Ansari | 5864420 | 2021-11-01 15:04:43 +0000 | [diff] [blame] | 11 | import fpstimer |
| 12 | |
| 13 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 14 | # import camera driver |
| 15 | if os.environ.get('CAMERA'): |
| 16 | Camera = import_module('camera_' + os.environ['CAMERA']).Camera |
| 17 | else: |
Shad Ansari | b5808d7 | 2021-10-04 12:43:27 -0700 | [diff] [blame] | 18 | # from camera import Camera |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 19 | from person_detection import Camera |
| 20 | |
| 21 | # Raspberry Pi camera module (requires picamera package) |
| 22 | # from camera_pi import Camera |
| 23 | |
| 24 | app = Flask(__name__) |
| 25 | |
| 26 | |
| 27 | @app.route('/') |
| 28 | def index(): |
| 29 | """Video streaming home page.""" |
Shad Ansari | 3334029 | 2021-10-14 22:10:01 +0000 | [diff] [blame] | 30 | return render_template('index.html', devices=[0, 1, 2, 3]) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 31 | |
| 32 | |
| 33 | def gen(camera): |
| 34 | """Video streaming generator function.""" |
Shad Ansari | 5864420 | 2021-11-01 15:04:43 +0000 | [diff] [blame] | 35 | timer = fpstimer.FPSTimer(fps) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 36 | while True: |
| 37 | frame = camera.get_frame() |
| 38 | yield (b'--frame\r\n' |
| 39 | b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') |
Shad Ansari | 5864420 | 2021-11-01 15:04:43 +0000 | [diff] [blame] | 40 | timer.sleep() |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 41 | |
Shad Ansari | b5808d7 | 2021-10-04 12:43:27 -0700 | [diff] [blame] | 42 | @app.route('/video_feed/<device>') |
| 43 | def video_feed(device): |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 44 | """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] | 45 | global args |
Shad Ansari | c0726e6 | 2021-10-04 22:38:53 +0000 | [diff] [blame] | 46 | camera = Camera(device, args) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 47 | return Response(gen(camera), |
| 48 | mimetype='multipart/x-mixed-replace; boundary=frame') |
| 49 | |
Shad Ansari | b5808d7 | 2021-10-04 12:43:27 -0700 | [diff] [blame] | 50 | def name_to_port(name): |
| 51 | return int(name) |
| 52 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 53 | def build_argparser(): |
| 54 | parser = ArgumentParser(add_help=False) |
| 55 | args = parser.add_argument_group('Options') |
| 56 | args.add_argument('-h', '--help', action='help', default=SUPPRESS, help='Show this help message and exit.') |
| 57 | args.add_argument("-m", "--model", help="Required. Path to an .xml file with a trained model.", |
| 58 | required=True, type=str) |
| 59 | args.add_argument("-i", "--input", |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 60 | help="Path to video file or image. 'cam' for capturing video stream from camera", |
| 61 | default = "gstreamer", type=str) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 62 | args.add_argument("-pt", "--prob_threshold", help="Optional. Probability threshold for detections filtering", |
Shad Ansari | a271465 | 2021-10-23 00:10:18 +0000 | [diff] [blame] | 63 | default=0.0, type=float) |
Shad Ansari | 5864420 | 2021-11-01 15:04:43 +0000 | [diff] [blame] | 64 | args.add_argument("-fps", "--frames_per_sec", help="Frames per second", |
| 65 | default=15, type=float) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 66 | |
| 67 | return parser |
| 68 | |
Shad Ansari | b5808d7 | 2021-10-04 12:43:27 -0700 | [diff] [blame] | 69 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 70 | if __name__ == '__main__': |
| 71 | args = build_argparser().parse_args() |
Shad Ansari | 5864420 | 2021-11-01 15:04:43 +0000 | [diff] [blame] | 72 | fps = args.frames_per_sec |
Shad Ansari | d70cb9e | 2021-10-26 05:33:02 +0000 | [diff] [blame] | 73 | app.run(host='0.0.0.0', debug=True) |