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