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