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