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 | 2ecc652 | 2021-12-03 21:03:48 +0000 | [diff] [blame] | 11 | from person_detection import Camera |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 12 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 13 | |
| 14 | app = Flask(__name__) |
| 15 | |
| 16 | |
| 17 | @app.route('/') |
| 18 | def index(): |
| 19 | """Video streaming home page.""" |
Shad Ansari | 3334029 | 2021-10-14 22:10:01 +0000 | [diff] [blame] | 20 | return render_template('index.html', devices=[0, 1, 2, 3]) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 21 | |
| 22 | |
| 23 | def gen(camera): |
| 24 | """Video streaming generator function.""" |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 25 | while True: |
| 26 | frame = camera.get_frame() |
| 27 | yield (b'--frame\r\n' |
| 28 | b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') |
| 29 | |
Shad Ansari | b5808d7 | 2021-10-04 12:43:27 -0700 | [diff] [blame] | 30 | @app.route('/video_feed/<device>') |
| 31 | def video_feed(device): |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 32 | """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] | 33 | global args |
Shad Ansari | c0726e6 | 2021-10-04 22:38:53 +0000 | [diff] [blame] | 34 | camera = Camera(device, args) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 35 | return Response(gen(camera), |
| 36 | mimetype='multipart/x-mixed-replace; boundary=frame') |
| 37 | |
Shad Ansari | b5808d7 | 2021-10-04 12:43:27 -0700 | [diff] [blame] | 38 | def name_to_port(name): |
| 39 | return int(name) |
| 40 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 41 | def build_argparser(): |
| 42 | parser = ArgumentParser(add_help=False) |
| 43 | args = parser.add_argument_group('Options') |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame^] | 44 | args.add_argument('-h', '--help', |
| 45 | action = 'help', |
| 46 | default = SUPPRESS, |
| 47 | help = 'Show this help message and exit.') |
| 48 | args.add_argument("-m", "--model", |
| 49 | help = "Required. Path to an .xml file with a trained model.", |
| 50 | required = True, |
| 51 | type = str) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 52 | args.add_argument("-i", "--input", |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame^] | 53 | help = "Path to video file or image. 'cam' for capturing video stream from camera", |
| 54 | default = "gstreamer", |
| 55 | type = str) |
| 56 | args.add_argument("-pt", "--prob_threshold", |
| 57 | help = "Optional. Probability threshold for detections filtering", |
| 58 | default = 0.75, |
| 59 | type = float) |
| 60 | args.add_argument("--idle", |
| 61 | action = 'store_true', |
| 62 | help = "Idle if no clients connected") |
| 63 | args.add_argument("--key", |
| 64 | help = "ROC api key", |
| 65 | required = True, |
| 66 | type = str) |
| 67 | args.add_argument("--mbrlow", |
| 68 | help = "Low range of MBR", |
| 69 | default = 5000000, |
| 70 | type = int) |
| 71 | args.add_argument("--mbrhigh", |
| 72 | help = "High range of MBR", |
| 73 | default = 10000000, |
| 74 | type = int) |
| 75 | args.add_argument("--devicegroup", |
| 76 | help = "Camera device group", |
| 77 | default = "menlo-4g-cameras", |
| 78 | type = str) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 79 | |
| 80 | return parser |
| 81 | |
Shad Ansari | b5808d7 | 2021-10-04 12:43:27 -0700 | [diff] [blame] | 82 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 83 | if __name__ == '__main__': |
| 84 | args = build_argparser().parse_args() |
Shad Ansari | d70cb9e | 2021-10-26 05:33:02 +0000 | [diff] [blame] | 85 | app.run(host='0.0.0.0', debug=True) |