2 # Copyright 2020 Huawei Technologies Co., Ltd.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
18 from flask_sslify import SSLify
19 from flask import Flask, request, jsonify, Response
20 from flask_cors import CORS
21 from camera_driver.capture_frame import VideoCamera, VideoFile
22 from influxdb import InfluxDBClient
31 app.config['JSON_AS_ASCII'] = False
32 app.config['UPLOAD_PATH'] = '/usr/app/images/'
33 app.config['supports_credentials'] = True
34 app.config['CORS_SUPPORTS_CREDENTIALS'] = True
35 app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
36 ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
45 Store the data and manage multiple input video feeds
47 def __init__(self, current_count=0, total_count=0, time=0):
48 self.type = "Shelf_INV"
49 self.labels = "Bottles"
50 self.current_count = current_count
51 self.total_count = total_count
54 def setcurrentcount(self, current_count):
55 self.current_count = current_count
57 def settotalcount(self, total_count):
58 self.total_count = total_count
60 def getcurrentcount(self):
61 return self.current_count
63 def gettotalcount(self):
64 return self.total_count
66 def setlabel(self, labels):
72 def settime(self, time):
79 def store_data(inventry_info):
81 store time series data in influx db
83 # TODO config, schema table, DB, fill data set
85 store_info_db(inventry_info)
88 def shelf_inventry(video_capture, camera_info):
94 process_this_frame = 0
96 success, frame = video_capture.get_frame()
99 if process_this_frame == 0:
100 url = config.detection_url + "/v1/obj_detection/detect"
101 # info1 = cv2.imencode(".jpg", rgb_small_frame)[1].tobytes()
102 data = json.loads(requests.post(url, data=frame,
103 verify=config.ssl_cacertpath).text)
104 inven_info = inventry_info()
105 current_count = data[count]
106 labels = data[labels]
107 total_count = inven_info.current_count + inven_info.total_count
108 inven_info.setcurrentcount(current_count)
109 inven_info.settotalcount(total_count)
110 inven_info.setlabel(labels)
111 inven_info.utime = time.time()
112 store_data(inven_info)
115 def store_info_db(inven_info):
117 Send "shelf" data to InfluxDB
119 :param inven_info: Inventry object
125 "measurement": inven_info.type,
130 "time": inven_info.time,
131 "Current Count": inven_info.current_count,
132 "Total Count": inven_info.total_count,
135 db_client.write_points(json_body)
138 def create_database():
140 Connect to InfluxDB and create the database
146 proxy = {"http": "http://{}:{}".format(config.IPADDRESS, config.PORT)}
147 db_client = InfluxDBClient(host=config.IPADDRESS, port=config.PORT,
148 proxies=proxy, database=config.DATABASE_NAME)
149 db_client.create_database(config.DATABASE_NAME)
152 @app.route('/v1/monitor/cameras', methods=['POST'])
154 camera_info = request.json
155 app.logger.info("Received message from ClientIP [" + request.remote_addr
156 + "] Operation [" + request.method + "]" +
157 " Resource [" + request.url + "]")
158 camera_info = {"name": camera_info["name"],
159 "rtspurl": camera_info["rtspurl"],
160 "location": camera_info["location"]}
161 listOfCameras.append(camera_info)
162 return Response("success")
165 @app.route('/v1/monitor/cameras/<name>/<rtspurl>/<location>', methods=['GET'])
166 def get_camera(name, rtspurl, location):
168 register camera with location
170 app.logger.info("Received message from ClientIP [" + request.remote_addr
171 + "] Operation [" + request.method + "]" +
172 " Resource [" + request.url + "]")
173 camera_info = {"name": name, "rtspurl": rtspurl, "location": location}
174 if "mp4" in camera_info["rtspurl"]:
175 video_file = VideoFile(camera_info["rtspurl"])
176 video_dict = {camera_info["name"]: video_file}
177 listOfVideos.append(video_dict)
178 return Response(shelf_inventry(video_file, camera_info["name"]),
179 mimetype='multipart/x-mixed-replace; boundary=frame')
181 video_file = VideoCamera(camera_info["rtspurl"])
182 video_dict = {camera_info["name"]: video_file}
183 listOfVideos.append(video_dict)
184 return Response(shelf_inventry(video_file, camera_info["name"]),
185 mimetype='multipart/x-mixed-replace; boundary=frame')
188 @app.route('/v1/monitor/cameras/<camera_name>', methods=['DELETE'])
189 def delete_camera(camera_name):
190 app.logger.info("Received message from ClientIP [" + request.remote_addr
191 + "] Operation [" + request.method + "]" +
192 " Resource [" + request.url + "]")
193 for video1 in listOfVideos:
194 if camera_name in video1:
195 video_obj = video1[camera_name]
197 for camera in listOfCameras:
198 if camera_name == camera["name"]:
199 listOfCameras.remove(camera)
200 for msg in listOfMsgs:
201 if camera_name in msg["msg"]:
202 listOfMsgs.remove(msg)
203 return Response("success")
206 @app.route('/v1/monitor/cameras')
208 app.logger.info("Received message from ClientIP [" + request.remote_addr
209 + "] Operation [" + request.method + "]" +
210 " Resource [" + request.url + "]")
211 return jsonify(listOfCameras)
212 return Response("success")
215 def start_server(handler):
216 app.logger.addHandler(handler)
217 if config.ssl_enabled:
218 context = (config.ssl_certfilepath, config.ssl_keyfilepath)
219 app.run(host=config.server_address, debug=True, ssl_context=context,
220 threaded=True, port=config.server_port)
222 app.run(host=config.server_address, debug=True, threaded=True,
223 port=config.server_port)