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 | 30a2373 | 2021-09-29 23:07:21 -0700 | [diff] [blame] | 51 | def build_argparser(): |
| 52 | parser = ArgumentParser(add_help=False) |
| 53 | args = parser.add_argument_group('Options') |
Shad Ansari | c9a2e79 | 2022-05-09 20:05:11 +0000 | [diff] [blame] | 54 | args.add_argument('-h', '--help', action='help', default=SUPPRESS, |
| 55 | help='Show this help message and exit.') |
Shad Ansari | 5e8d069 | 2021-12-08 19:09:34 +0000 | [diff] [blame] | 56 | args.add_argument("-m", "--model", |
Shad Ansari | 7ee36bf | 2022-05-10 22:43:42 +0000 | [diff] [blame] | 57 | default="models/intel/person-detection-retail-0013/FP32/person-detection-retail-0013.xml", |
| 58 | help="Path to model", type=str) |
Shad Ansari | c9a2e79 | 2022-05-09 20:05:11 +0000 | [diff] [blame] | 59 | args.add_argument("-i", "--input", |
| 60 | help="Video input", default="gstreamer", type=str) |
Shad Ansari | 7ee36bf | 2022-05-10 22:43:42 +0000 | [diff] [blame] | 61 | args.add_argument("-pt", "--prob_threshold", default=0.75, |
| 62 | help="Probability threshold for detection", type=float) |
Shad Ansari | c9a2e79 | 2022-05-09 20:05:11 +0000 | [diff] [blame] | 63 | args.add_argument("--noroc", action='store_true', help="No ROC") |
| 64 | args.add_argument("--key", help="ROC api key", type=str) |
| 65 | args.add_argument("--url", default=config.ROCURL, |
| 66 | help="ROC url", type=str) |
| 67 | args.add_argument("--keycloak", default=config.KEYCLOAKURL, |
| 68 | help="Keycloak url", type=str) |
| 69 | args.add_argument("--enterprise", default=config.ENTERPRISE, |
| 70 | help="Enterprise ID", type=str) |
| 71 | args.add_argument("--site", default=config.SITE, |
| 72 | help="Site ID", type=str) |
| 73 | args.add_argument("--devicegroup", default=config.DEVICEGROUP, |
| 74 | help="Camera device group", type=str) |
| 75 | args.add_argument("--user", default=config.ROCUSER, |
| 76 | help="ROC username", type=str) |
| 77 | args.add_argument("--password", default=config.ROCPASSWORD, |
| 78 | help="ROC password", type=str) |
| 79 | args.add_argument("--num", default=config.NUMDEVICES, |
| 80 | help="Number of camera devices", |
| 81 | type=int) |
Shad Ansari | b652dc7 | 2022-05-13 21:11:16 +0000 | [diff] [blame^] | 82 | args.add_argument("--mqttip", default=config.MQTTIP, |
| 83 | help="IP address of MQTT", |
| 84 | type=str) |
Shad Ansari | c9a2e79 | 2022-05-09 20:05:11 +0000 | [diff] [blame] | 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) |