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 | 2eddc1e | 2022-01-10 17:53:06 +0000 | [diff] [blame] | 6 | import logging as log |
| 7 | import sys |
| 8 | import flask |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 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 | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 13 | from roc import Roc |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 14 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 15 | |
Shad Ansari | 2eddc1e | 2022-01-10 17:53:06 +0000 | [diff] [blame] | 16 | app = flask.Flask(__name__) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 17 | |
| 18 | |
| 19 | @app.route('/') |
| 20 | def index(): |
| 21 | """Video streaming home page.""" |
Shad Ansari | 2eddc1e | 2022-01-10 17:53:06 +0000 | [diff] [blame] | 22 | log.info("{} - connected".format(flask.request.remote_addr)) |
| 23 | return flask.render_template('index.html', devices=config.cameras) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 24 | |
Shad Ansari | 3501904 | 2022-03-29 20:15:54 +0000 | [diff] [blame] | 25 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 26 | def gen(camera): |
| 27 | """Video streaming generator function.""" |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 28 | while True: |
| 29 | frame = camera.get_frame() |
| 30 | yield (b'--frame\r\n' |
| 31 | b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') |
| 32 | |
Shad Ansari | 3501904 | 2022-03-29 20:15:54 +0000 | [diff] [blame] | 33 | |
Shad Ansari | b5808d7 | 2021-10-04 12:43:27 -0700 | [diff] [blame] | 34 | @app.route('/video_feed/<device>') |
| 35 | def video_feed(device): |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 36 | """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] | 37 | global args |
Shad Ansari | 2eddc1e | 2022-01-10 17:53:06 +0000 | [diff] [blame] | 38 | log.debug("{} - video feed {}".format(flask.request.remote_addr, device)) |
| 39 | camera = Camera(device, flask.request.remote_addr, args) |
| 40 | return flask.Response(gen(camera), |
Shad Ansari | 3501904 | 2022-03-29 20:15:54 +0000 | [diff] [blame] | 41 | mimetype='multipart/x-mixed-replace; boundary=frame') |
| 42 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 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 | 3501904 | 2022-03-29 20:15:54 +0000 | [diff] [blame] | 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') |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame] | 51 | args.add_argument('-h', '--help', |
| 52 | action = 'help', |
| 53 | default = SUPPRESS, |
| 54 | help = 'Show this help message and exit.') |
| 55 | args.add_argument("-m", "--model", |
| 56 | help = "Required. Path to an .xml file with a trained model.", |
| 57 | required = True, |
| 58 | type = str) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 59 | args.add_argument("-i", "--input", |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame] | 60 | help = "Path to video file or image. 'cam' for capturing video stream from camera", |
| 61 | default = "gstreamer", |
| 62 | type = str) |
| 63 | args.add_argument("-pt", "--prob_threshold", |
| 64 | help = "Optional. Probability threshold for detections filtering", |
| 65 | default = 0.75, |
| 66 | type = float) |
Shad Ansari | ec6bbd3 | 2021-12-10 20:57:16 +0000 | [diff] [blame] | 67 | args.add_argument("--noroc", |
Shad Ansari | 2eddc1e | 2022-01-10 17:53:06 +0000 | [diff] [blame] | 68 | action = 'store_true', |
Shad Ansari | ec6bbd3 | 2021-12-10 20:57:16 +0000 | [diff] [blame] | 69 | help = "No ROC") |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame] | 70 | args.add_argument("--key", |
| 71 | help = "ROC api key", |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame] | 72 | type = str) |
Shad Ansari | 2948cc0 | 2022-04-14 21:33:37 +0000 | [diff] [blame^] | 73 | args.add_argument("--url", |
| 74 | help = "ROC url", |
| 75 | type = str) |
| 76 | args.add_argument("--keycloak", |
| 77 | help = "Keycloak url", |
| 78 | type = str) |
| 79 | args.add_argument("--enterprise", |
| 80 | help = "Enterprise ID", |
| 81 | type = str) |
| 82 | args.add_argument("--site", |
| 83 | help = "Site ID", |
| 84 | type = str) |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 85 | args.add_argument("--user", |
| 86 | help = "ROC username", |
| 87 | type = str) |
| 88 | args.add_argument("--password", |
| 89 | help = "ROC password", |
| 90 | type = str) |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame] | 91 | args.add_argument("--mbrlow", |
Shad Ansari | 3501904 | 2022-03-29 20:15:54 +0000 | [diff] [blame] | 92 | help="Low range of MBR", |
| 93 | default=7000000, |
| 94 | type=int) |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame] | 95 | args.add_argument("--mbrhigh", |
Shad Ansari | 3501904 | 2022-03-29 20:15:54 +0000 | [diff] [blame] | 96 | help="High range of MBR", |
| 97 | default=10000000, |
| 98 | type=int) |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame] | 99 | args.add_argument("--devicegroup", |
Shad Ansari | 3501904 | 2022-03-29 20:15:54 +0000 | [diff] [blame] | 100 | help="Camera device group", |
Shad Ansari | 3501904 | 2022-03-29 20:15:54 +0000 | [diff] [blame] | 101 | type=str) |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 102 | |
| 103 | return parser |
| 104 | |
Shad Ansari | b5808d7 | 2021-10-04 12:43:27 -0700 | [diff] [blame] | 105 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 106 | if __name__ == '__main__': |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 107 | log.basicConfig( |
| 108 | format='%(asctime)s %(levelname)-8s %(message)s', |
| 109 | level=log.DEBUG, |
| 110 | datefmt='%Y-%m-%d %H:%M:%S', |
| 111 | stream=sys.stdout) |
Shad Ansari | 2eddc1e | 2022-01-10 17:53:06 +0000 | [diff] [blame] | 112 | log.debug("Starting person detection app") |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 113 | |
Shad Ansari | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 114 | args = build_argparser().parse_args() |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 115 | |
Shad Ansari | 2eddc1e | 2022-01-10 17:53:06 +0000 | [diff] [blame] | 116 | if not args.noroc: |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 117 | key = args.key |
| 118 | if key is None: |
| 119 | if args.user is not None and args.password is not None: |
Shad Ansari | 2948cc0 | 2022-04-14 21:33:37 +0000 | [diff] [blame^] | 120 | roc = Roc(args.url, args.keycloak, args.user, args.password, args.enterprise, args.site) |
shad | f64b92a | 2022-01-13 19:06:29 +0000 | [diff] [blame] | 121 | key = roc.get_key() |
| 122 | else: |
| 123 | log.error("Either key or user/password required") |
| 124 | sys.exit() |
| 125 | |
| 126 | log.info("Device group:{} mbr:{}(low)".format(args.devicegroup, args.mbrlow)) |
| 127 | roc.set_mbr(args.devicegroup, args.mbrlow) |
| 128 | |
Shad Ansari | d70cb9e | 2021-10-26 05:33:02 +0000 | [diff] [blame] | 129 | app.run(host='0.0.0.0', debug=True) |