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