blob: 765295077d671ace03dd7f9efa18eed89182077e [file] [log] [blame]
Shad Ansari30a23732021-09-29 23:07:21 -07001#!/usr/bin/env python
Shad Ansariae90d252021-10-22 16:40:29 +00002"""
3SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org>
4SPDX-License-Identifier: LicenseRef-ONF-Member-1.01
5"""
Shad Ansaric9a2e792022-05-09 20:05:11 +00006import os
Shad Ansari2eddc1e2022-01-10 17:53:06 +00007import logging as log
8import sys
9import flask
Shad Ansari30a23732021-09-29 23:07:21 -070010from argparse import ArgumentParser, SUPPRESS
11
Shad Ansari9a95c3d2021-12-09 04:32:09 +000012import config
Shad Ansari2ecc6522021-12-03 21:03:48 +000013from person_detection import Camera
shadf64b92a2022-01-13 19:06:29 +000014from roc import Roc
Shad Ansari30a23732021-09-29 23:07:21 -070015
Shad Ansari30a23732021-09-29 23:07:21 -070016
Shad Ansari2eddc1e2022-01-10 17:53:06 +000017app = flask.Flask(__name__)
Shad Ansari30a23732021-09-29 23:07:21 -070018
19
20@app.route('/')
21def index():
22 """Video streaming home page."""
Shad Ansaria7170992022-04-15 21:36:01 +000023 global args
Shad Ansaric9a2e792022-05-09 20:05:11 +000024 devices = {str(x): "Camera "+str(x) for x in range(args.num)}
Shad Ansari2eddc1e2022-01-10 17:53:06 +000025 log.info("{} - connected".format(flask.request.remote_addr))
Shad Ansaria7170992022-04-15 21:36:01 +000026 return flask.render_template('index.html', devices=devices)
Shad Ansari30a23732021-09-29 23:07:21 -070027
Shad Ansari35019042022-03-29 20:15:54 +000028
Shad Ansari30a23732021-09-29 23:07:21 -070029def gen(camera):
30 """Video streaming generator function."""
Shad Ansari30a23732021-09-29 23:07:21 -070031 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 Ansari35019042022-03-29 20:15:54 +000036
Shad Ansarib5808d72021-10-04 12:43:27 -070037@app.route('/video_feed/<device>')
38def video_feed(device):
Shad Ansari30a23732021-09-29 23:07:21 -070039 """Video streaming route. Put this in the src attribute of an img tag."""
Shad Ansarib5808d72021-10-04 12:43:27 -070040 global args
Shad Ansari2eddc1e2022-01-10 17:53:06 +000041 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 Ansari35019042022-03-29 20:15:54 +000044 mimetype='multipart/x-mixed-replace; boundary=frame')
45
Shad Ansari30a23732021-09-29 23:07:21 -070046
Shad Ansarib5808d72021-10-04 12:43:27 -070047def name_to_port(name):
48 return int(name)
49
Shad Ansari35019042022-03-29 20:15:54 +000050
Shad Ansari30a23732021-09-29 23:07:21 -070051def build_argparser():
52 parser = ArgumentParser(add_help=False)
53 args = parser.add_argument_group('Options')
Shad Ansaric9a2e792022-05-09 20:05:11 +000054 args.add_argument('-h', '--help', action='help', default=SUPPRESS,
55 help='Show this help message and exit.')
Shad Ansari5e8d0692021-12-08 19:09:34 +000056 args.add_argument("-m", "--model",
Shad Ansari7ee36bf2022-05-10 22:43:42 +000057 default="models/intel/person-detection-retail-0013/FP32/person-detection-retail-0013.xml",
58 help="Path to model", type=str)
Shad Ansaric9a2e792022-05-09 20:05:11 +000059 args.add_argument("-i", "--input",
60 help="Video input", default="gstreamer", type=str)
Shad Ansari7ee36bf2022-05-10 22:43:42 +000061 args.add_argument("-pt", "--prob_threshold", default=0.75,
62 help="Probability threshold for detection", type=float)
Shad Ansaric9a2e792022-05-09 20:05:11 +000063 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)
82 args.add_argument("--mbrlow", help="Low range of MBR", default=7000000,
83 type=int)
84 args.add_argument("--mbrhigh", help="High range of MBR", default=10000000,
85 type=int)
Shad Ansari30a23732021-09-29 23:07:21 -070086
87 return parser
88
Shad Ansarib5808d72021-10-04 12:43:27 -070089
Shad Ansari30a23732021-09-29 23:07:21 -070090if __name__ == '__main__':
shadf64b92a2022-01-13 19:06:29 +000091 log.basicConfig(
92 format='%(asctime)s %(levelname)-8s %(message)s',
93 level=log.DEBUG,
94 datefmt='%Y-%m-%d %H:%M:%S',
95 stream=sys.stdout)
Shad Ansari2eddc1e2022-01-10 17:53:06 +000096 log.debug("Starting person detection app")
shadf64b92a2022-01-13 19:06:29 +000097
Shad Ansari30a23732021-09-29 23:07:21 -070098 args = build_argparser().parse_args()
shadf64b92a2022-01-13 19:06:29 +000099
Shad Ansari2eddc1e2022-01-10 17:53:06 +0000100 if not args.noroc:
shadf64b92a2022-01-13 19:06:29 +0000101 key = args.key
102 if key is None:
103 if args.user is not None and args.password is not None:
Shad Ansaric9a2e792022-05-09 20:05:11 +0000104 roc = Roc(args.url, args.keycloak, args.user, args.password,
105 args.enterprise, args.site)
shadf64b92a2022-01-13 19:06:29 +0000106 key = roc.get_key()
107 else:
108 log.error("Either key or user/password required")
109 sys.exit()
110
Shad Ansaric9a2e792022-05-09 20:05:11 +0000111 log.info("Device group:{} mbr:{}(low)".format(args.devicegroup,
112 args.mbrlow))
shadf64b92a2022-01-13 19:06:29 +0000113 roc.set_mbr(args.devicegroup, args.mbrlow)
114
Shad Ansarid70cb9e2021-10-26 05:33:02 +0000115 app.run(host='0.0.0.0', debug=True)