blob: 9d4be6a7de6eec8124015750f6d4bd968481cdd6 [file] [log] [blame]
Shad Ansari30a23732021-09-29 23:07:21 -07001#!/usr/bin/env python
Shad Ansariae90d252021-10-22 16:40:29 +00002"""
Shad Ansarif53bc4f2022-06-07 14:31:11 -07003SPDX-FileCopyrightText: 2022-present Intel Corporation
Shad Ansariae90d252021-10-22 16:40:29 +00004SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org>
Shad Ansarif53bc4f2022-06-07 14:31:11 -07005SPDX-License-Identifier: Apache-2.0
Shad Ansariae90d252021-10-22 16:40:29 +00006"""
Shad Ansaric9a2e792022-05-09 20:05:11 +00007import os
Shad Ansari2eddc1e2022-01-10 17:53:06 +00008import logging as log
9import sys
10import flask
Shad Ansari30a23732021-09-29 23:07:21 -070011from argparse import ArgumentParser, SUPPRESS
12
Shad Ansari9a95c3d2021-12-09 04:32:09 +000013import config
Shad Ansari2ecc6522021-12-03 21:03:48 +000014from person_detection import Camera
shadf64b92a2022-01-13 19:06:29 +000015from roc import Roc
Shad Ansari30a23732021-09-29 23:07:21 -070016
Shad Ansari30a23732021-09-29 23:07:21 -070017
Shad Ansari2eddc1e2022-01-10 17:53:06 +000018app = flask.Flask(__name__)
Shad Ansari30a23732021-09-29 23:07:21 -070019
20
21@app.route('/')
22def index():
23 """Video streaming home page."""
Shad Ansaria7170992022-04-15 21:36:01 +000024 global args
Shad Ansaric9a2e792022-05-09 20:05:11 +000025 devices = {str(x): "Camera "+str(x) for x in range(args.num)}
Shad Ansari2eddc1e2022-01-10 17:53:06 +000026 log.info("{} - connected".format(flask.request.remote_addr))
Shad Ansaria7170992022-04-15 21:36:01 +000027 return flask.render_template('index.html', devices=devices)
Shad Ansari30a23732021-09-29 23:07:21 -070028
Shad Ansari35019042022-03-29 20:15:54 +000029
Shad Ansari30a23732021-09-29 23:07:21 -070030def gen(camera):
31 """Video streaming generator function."""
Shad Ansari30a23732021-09-29 23:07:21 -070032 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 Ansari35019042022-03-29 20:15:54 +000037
Shad Ansarib5808d72021-10-04 12:43:27 -070038@app.route('/video_feed/<device>')
39def video_feed(device):
Shad Ansari30a23732021-09-29 23:07:21 -070040 """Video streaming route. Put this in the src attribute of an img tag."""
Shad Ansarib5808d72021-10-04 12:43:27 -070041 global args
Shad Ansari2eddc1e2022-01-10 17:53:06 +000042 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 Ansari35019042022-03-29 20:15:54 +000045 mimetype='multipart/x-mixed-replace; boundary=frame')
46
Shad Ansari30a23732021-09-29 23:07:21 -070047
Shad Ansarib5808d72021-10-04 12:43:27 -070048def name_to_port(name):
49 return int(name)
50
Shad Ansari35019042022-03-29 20:15:54 +000051
Shad Ansari30a23732021-09-29 23:07:21 -070052def build_argparser():
53 parser = ArgumentParser(add_help=False)
54 args = parser.add_argument_group('Options')
Shad Ansaric9a2e792022-05-09 20:05:11 +000055 args.add_argument('-h', '--help', action='help', default=SUPPRESS,
56 help='Show this help message and exit.')
Shad Ansari5e8d0692021-12-08 19:09:34 +000057 args.add_argument("-m", "--model",
Shad Ansari7ee36bf2022-05-10 22:43:42 +000058 default="models/intel/person-detection-retail-0013/FP32/person-detection-retail-0013.xml",
59 help="Path to model", type=str)
Shad Ansaric9a2e792022-05-09 20:05:11 +000060 args.add_argument("-i", "--input",
61 help="Video input", default="gstreamer", type=str)
Shad Ansari7ee36bf2022-05-10 22:43:42 +000062 args.add_argument("-pt", "--prob_threshold", default=0.75,
63 help="Probability threshold for detection", type=float)
Shad Ansaric9a2e792022-05-09 20:05:11 +000064 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 Ansarib652dc72022-05-13 21:11:16 +000083 args.add_argument("--mqttip", default=config.MQTTIP,
84 help="IP address of MQTT",
85 type=str)
Shad Ansaric9a2e792022-05-09 20:05:11 +000086 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 Ansari30a23732021-09-29 23:07:21 -070090
91 return parser
92
Shad Ansarib5808d72021-10-04 12:43:27 -070093
Shad Ansari30a23732021-09-29 23:07:21 -070094if __name__ == '__main__':
shadf64b92a2022-01-13 19:06:29 +000095 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 Ansari2eddc1e2022-01-10 17:53:06 +0000100 log.debug("Starting person detection app")
shadf64b92a2022-01-13 19:06:29 +0000101
Shad Ansari30a23732021-09-29 23:07:21 -0700102 args = build_argparser().parse_args()
shadf64b92a2022-01-13 19:06:29 +0000103
Shad Ansari2eddc1e2022-01-10 17:53:06 +0000104 if not args.noroc:
shadf64b92a2022-01-13 19:06:29 +0000105 key = args.key
106 if key is None:
107 if args.user is not None and args.password is not None:
Shad Ansaric9a2e792022-05-09 20:05:11 +0000108 roc = Roc(args.url, args.keycloak, args.user, args.password,
109 args.enterprise, args.site)
shadf64b92a2022-01-13 19:06:29 +0000110 key = roc.get_key()
111 else:
112 log.error("Either key or user/password required")
113 sys.exit()
114
Shad Ansaric9a2e792022-05-09 20:05:11 +0000115 log.info("Device group:{} mbr:{}(low)".format(args.devicegroup,
116 args.mbrlow))
shadf64b92a2022-01-13 19:06:29 +0000117 roc.set_mbr(args.devicegroup, args.mbrlow)
118
Shad Ansarid70cb9e2021-10-26 05:33:02 +0000119 app.run(host='0.0.0.0', debug=True)