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):
78 def store_data(inventry_info):
80 store time series data in influx db
82 # TODO config, schema table, DB, fill data set
84 store_info_db(inventry_info)
87 def shelf_inventry(video_capture, camera_info):
93 process_this_frame = 0
95 success, frame = video_capture.get_frame()
98 if process_this_frame == 0:
99 url = config.detection_url + "/v1/obj_detection/detect"
100 # info1 = cv2.imencode(".jpg", rgb_small_frame)[1].tobytes()
101 data = json.loads(requests.post(url, data=frame,
102 verify=config.ssl_cacertpath).text)
103 inven_info = inventry_info()
104 current_count = data[count]
105 labels = data[labels]
106 total_count = inven_info.current_count + inven_info.total_count
107 inven_info.setcurrentcount(current_count)
108 inven_info.settotalcount(total_count)
109 inven_info.setlabel(labels)
110 inven_info.utime = time.time()
111 store_data(inven_info)
114 def store_info_db(inven_info):
116 Send "shelf" data to InfluxDB
118 :param inven_info: Inventry object
124 "measurement": inven_info.type,
129 "time": inven_info.time,
130 "Current Count": inven_info.current_count,
131 "Total Count": inven_info.total_count,
134 db_client.write_points(json_body)
137 def create_database():
139 Connect to InfluxDB and create the database
145 proxy = {"http": "http://{}:{}".format(config.IPADDRESS, config.PORT)}
146 db_client = InfluxDBClient(host=config.IPADDRESS, port=config.PORT,
147 proxies=proxy, database=config.DATABASE_NAME)
148 db_client.create_database(config.DATABASE_NAME)
151 @app.route('/v1/monitor/cameras', methods=['POST'])
153 camera_info = request.json
154 app.logger.info("Received message from ClientIP [" + request.remote_addr
155 + "] Operation [" + request.method + "]" +
156 " Resource [" + request.url + "]")
157 camera_info = {"name": camera_info["name"],
158 "rtspurl": camera_info["rtspurl"],
159 "location": camera_info["location"]}
160 listOfCameras.append(camera_info)
161 return Response("success")
164 @app.route('/v1/monitor/cameras/<name>/<rtspurl>/<location>', methods=['GET'])
165 def get_camera(name, rtspurl, location):
167 register camera with location
169 app.logger.info("Received message from ClientIP [" + request.remote_addr
170 + "] Operation [" + request.method + "]" +
171 " Resource [" + request.url + "]")
172 camera_info = {"name": name, "rtspurl": rtspurl, "location": location}
173 if "mp4" in camera_info["rtspurl"]:
174 video_file = VideoFile(camera_info["rtspurl"])
175 video_dict = {camera_info["name"]: video_file}
176 listOfVideos.append(video_dict)
177 return Response(shelf_inventry(video_file, camera_info["name"]),
178 mimetype='multipart/x-mixed-replace; boundary=frame')
180 video_file = VideoCamera(camera_info["rtspurl"])
181 video_dict = {camera_info["name"]: video_file}
182 listOfVideos.append(video_dict)
183 return Response(shelf_inventry(video_file, camera_info["name"]),
184 mimetype='multipart/x-mixed-replace; boundary=frame')
187 @app.route('/v1/monitor/cameras/<camera_name>', methods=['DELETE'])
188 def delete_camera(camera_name):
189 app.logger.info("Received message from ClientIP [" + request.remote_addr
190 + "] Operation [" + request.method + "]" +
191 " Resource [" + request.url + "]")
192 for video1 in listOfVideos:
193 if camera_name in video1:
194 video_obj = video1[camera_name]
196 for camera in listOfCameras:
197 if camera_name == camera["name"]:
198 listOfCameras.remove(camera)
199 for msg in listOfMsgs:
200 if camera_name in msg["msg"]:
201 listOfMsgs.remove(msg)
202 return Response("success")
205 @app.route('/v1/monitor/cameras')
207 app.logger.info("Received message from ClientIP [" + request.remote_addr
208 + "] Operation [" + request.method + "]" +
209 " Resource [" + request.url + "]")
210 return jsonify(listOfCameras)
211 return Response("success")
214 def start_server(handler):
215 app.logger.addHandler(handler)
216 if config.ssl_enabled:
217 context = (config.ssl_certfilepath, config.ssl_keyfilepath)
218 app.run(host=config.server_address, debug=True, ssl_context=context,
219 threaded=True, port=config.server_port)
221 app.run(host=config.server_address, debug=True, threaded=True,
222 port=config.server_port)