From da76bbe93ae0886adb8b8c2d093cfc821bbac007 Mon Sep 17 00:00:00 2001 From: khemendra kumar Date: Wed, 2 Dec 2020 09:49:29 +0530 Subject: [PATCH] Example App: Retail store inventry Signed-off-by: khemendra kumar Change-Id: I9b437dc40ff73a5c49332211848eb4f77a467d61 --- example-apps/ROBO/retail_app/.gitignore | 3 + .../ROBO/retail_app/camera_driver/capture_frame.py | 51 ++++++++ example-apps/ROBO/retail_app/config.py | 40 ++++++ example-apps/ROBO/retail_app/config.pyc | Bin 0 -> 971 bytes example-apps/ROBO/retail_app/configs/start.sh | 106 ++++++++++++++++ example-apps/ROBO/retail_app/inventry/__init__.py | 0 example-apps/ROBO/retail_app/inventry/__init__.pyc | Bin 0 -> 193 bytes .../ROBO/retail_app/inventry/retail_app.py | 136 +++++++++++++++++++++ example-apps/ROBO/retail_app/requirements.txt | 21 ++++ example-apps/ROBO/retail_app/retail_app.log | 0 example-apps/ROBO/retail_app/run.py | 25 ++++ 11 files changed, 382 insertions(+) create mode 100644 example-apps/ROBO/retail_app/.gitignore create mode 100644 example-apps/ROBO/retail_app/camera_driver/capture_frame.py create mode 100644 example-apps/ROBO/retail_app/config.py create mode 100644 example-apps/ROBO/retail_app/config.pyc create mode 100644 example-apps/ROBO/retail_app/configs/start.sh create mode 100644 example-apps/ROBO/retail_app/inventry/__init__.py create mode 100644 example-apps/ROBO/retail_app/inventry/__init__.pyc create mode 100644 example-apps/ROBO/retail_app/inventry/retail_app.py create mode 100644 example-apps/ROBO/retail_app/requirements.txt create mode 100644 example-apps/ROBO/retail_app/retail_app.log create mode 100644 example-apps/ROBO/retail_app/run.py diff --git a/example-apps/ROBO/retail_app/.gitignore b/example-apps/ROBO/retail_app/.gitignore new file mode 100644 index 0000000..329ec83 --- /dev/null +++ b/example-apps/ROBO/retail_app/.gitignore @@ -0,0 +1,3 @@ +venv +__pycache__ +.idea diff --git a/example-apps/ROBO/retail_app/camera_driver/capture_frame.py b/example-apps/ROBO/retail_app/camera_driver/capture_frame.py new file mode 100644 index 0000000..ed2ae69 --- /dev/null +++ b/example-apps/ROBO/retail_app/camera_driver/capture_frame.py @@ -0,0 +1,51 @@ +# +# Copyright 2020 Huawei Technologies Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + + +class VideoCamera(object): + """ + opneCV to capture frame from a camera + """ + """ + def __init__(self, url): + # self.video = cv2.VideoCapture(url) + + def delete(self): + # self.video.release() + + def get_frame(self): + # get a frane from camera url + # success, image = self.video.read() + return + """ + + +class VideoFile(object): + """ + opneCV to capture frame from a video stream + """ + """ + def __init__(self, video_name): + # self.video = cv2.VideoCapture("./test/resources/" + video_name) + + def delete(self): + # self.video.release() + + def get_frame(self): + # get a frane from stream + # success, image = self.video.read() + return + """ diff --git a/example-apps/ROBO/retail_app/config.py b/example-apps/ROBO/retail_app/config.py new file mode 100644 index 0000000..5249ff7 --- /dev/null +++ b/example-apps/ROBO/retail_app/config.py @@ -0,0 +1,40 @@ +# +# Copyright 2020 Huawei Technologies Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import os + +# [Server Configurations] +server_port = 9999 +server_address = os.environ.get('LISTEN_IP') + +# [SSL Configurations] +ssl_enabled = False +ssl_protocol = "TLSv1.2" +ssl_ciphers = ["TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", + "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", + "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", + "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384"] +ssl_certfilepath = "/usr/app/ssl/server_tls.crt" +ssl_keyfilepath = "/usr/app/ssl/server_tls.key" +ssl_cacertpath = "/usr/app/ssl/ca.crt" +ssl_server_name = os.environ.get('SERVER_NAME', "ealtedge") + +# [Service Configurations] +api_gateway = os.environ.get("API_GATEWAY", "apigw.mep.org") +Obj_Det = os.environ.get("OBJ_DETECTION", "objdetection") + +# [Constants] +detection_url = "http://" + api_gateway + "/" + Obj_Det diff --git a/example-apps/ROBO/retail_app/config.pyc b/example-apps/ROBO/retail_app/config.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6df092dca0939106122a376180a41051a703b54c GIT binary patch literal 971 zcmaJ<&2AGh5T5)rZPO%eL6wj=BYGhwp+FA^u}zmIh%`|)B4VY=a@Njf)6IHiyRCBO z$ekD9!FUKB0LGg%DjZ^aXJ@{d`DXl6_+2gk`1O+puo^}Df519dO0BgYa zGqCyqJOF-}f#}VFcl-$ZiT>cYjB3xZzg@H&&A0WJI2(E!t>I_ywoz}IXG@*Bt@E}^ z2cAPmonf0gu16cqQ+mv;~We;GxKzPQODh zoS{2%zUnGESsI0R^|?svQiaAMy|YjB+#R~DVW)R#Du~GOj0+hHV2l?+CrI0 z6pNIZsj=lPh=;Jova&Xi+0ZP^z6+7uMoK28fI9Aeox18UVe@5EcmyC=83@7kJB> zus&lR(JCOl@r<6|pD4BcM1dfIOeRrSPZ!y(y_-vZ6N~qUSWQ1cQ7M%2r9vrZXC;%R J+)?2u_ZP$n{Hp)} literal 0 HcmV?d00001 diff --git a/example-apps/ROBO/retail_app/configs/start.sh b/example-apps/ROBO/retail_app/configs/start.sh new file mode 100644 index 0000000..fd38b71 --- /dev/null +++ b/example-apps/ROBO/retail_app/configs/start.sh @@ -0,0 +1,106 @@ +#!/bin/bash +# Copyright 2020 Huawei Technologies Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Validates if ip is valid +validate_ip() +{ + ip_var="$1" + # checks if variable is unset + if [ -z "$ip_var" ] ; then + echo "ip is not set" + return 1 + fi + + if ! echo "$ip_var" | grep -qE '^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.)' ; then + return 1 + fi + return 0 +} + +validate_name() { + hostname="$1" + len="${#hostname}" + if [ "${len}" -gt "64" ]; then + return 1 + fi + if ! echo "$hostname" | grep -qE '^[a-zA-Z0-9]*$|^[a-zA-Z0-9][a-zA-Z0-9_\-]*[a-zA-Z0-9]$'; then + return 1 + fi + return 0 +} + +# validates whether file exist +validate_file_exists() { + file_path="$1" + + # checks variable is unset + if [ -z "$file_path" ]; then + echo "file path variable is not set" + return 1 + fi + + # checks if file exists + if [ ! -f "$file_path" ]; then + echo "file does not exist" + return 1 + fi + + return 0 +} + +validate_ip "$LISTEN_IP" +valid_listen_ip="$?" +if [ ! "$valid_listen_ip" -eq "0" ]; then + echo "invalid ip address for listen ip" + exit 1 +fi + +if [ ! -z "$SERVER_NAME" ]; then + validate_name "$SERVER_NAME" + valid_name="$?" + if [ ! "$valid_name" -eq "0" ]; then + echo "invalid ssl server name" + exit 1 + fi +fi + +# ssl parameters validation +validate_file_exists "/usr/app/ssl/server_tls.crt" +valid_ssl_server_cert="$?" +if [ ! "$valid_ssl_server_cert" -eq "0" ]; then + echo "invalid ssl server certificate" + exit 1 +fi + +# ssl parameters validation +validate_file_exists "/usr/app/ssl/server_tls.key" +valid_ssl_server_key="$?" +if [ ! "$valid_ssl_server_key" -eq "0" ]; then + echo "invalid ssl server key" + exit 1 +fi + +# ssl parameters validation +validate_file_exists "/usr/app/ssl/ca.crt" +valid_ssl_ca_crt="$?" +if [ ! "$valid_ssl_ca_crt" -eq "0" ]; then + echo "invalid ssl ca cert" + exit 1 +fi + +echo "Running Retail App" +umask 0027 +cd /usr/app || exit +python run.py \ No newline at end of file diff --git a/example-apps/ROBO/retail_app/inventry/__init__.py b/example-apps/ROBO/retail_app/inventry/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/example-apps/ROBO/retail_app/inventry/__init__.pyc b/example-apps/ROBO/retail_app/inventry/__init__.pyc new file mode 100644 index 0000000000000000000000000000000000000000..4a779cf6b02eac2a15d92dc5891df77092773bc8 GIT binary patch literal 193 zcmZ9Gy9&ZU5JgwC5W#;)nMMb}Qp8uvXh;gnVY3-<^V+aG3i+6Pr9a>Xv~l2`3#Yj= zeJ>ZmFrX6OoA^HXxS2;GL>7+b8hVI7xY9d#n1;X_!yAZuyc=7=v9eOP2IA<9rv%cd z4z`dIOI@#I*Ex`hlo>dy;FgzoAtuGzwCKpbv?3^e1`u_t8FAh9Joj_E#uU$*_Ty9R FBR-hGFu4E# literal 0 HcmV?d00001 diff --git a/example-apps/ROBO/retail_app/inventry/retail_app.py b/example-apps/ROBO/retail_app/inventry/retail_app.py new file mode 100644 index 0000000..8fe0a6f --- /dev/null +++ b/example-apps/ROBO/retail_app/inventry/retail_app.py @@ -0,0 +1,136 @@ +# +# Copyright 2020 Huawei Technologies Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import config +from flask_sslify import SSLify +from flask import Flask, request, Response +from flask_cors import CORS + + +app = Flask(__name__) +CORS(app) +sslify = SSLify(app) +app.config['JSON_AS_ASCII'] = False +app.config['UPLOAD_PATH'] = '/usr/app/images/' +app.config['supports_credentials'] = True +app.config['CORS_SUPPORTS_CREDENTIALS'] = True +app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 +ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg']) +count = 0 +listOfMsgs = [] + + +class shelf_inventry(): + """ + def __init__(self, url): + # self.video = cv2.VideoCapture(url) + + def delete(self): + # self.video.release() + return + """ + + +def store_data(): + """ + store time series data in influx db + """ + # TODO config, schema table, DB, fill data set + + +def obj_detect(): + """ + detect obj and count for self + """ + + +@app.route('/v1/monitor/cameras', methods=['POST']) +def add_camera(): + camera_info = request.json + app.logger.info("Received message from ClientIP [" + request.remote_addr + + "] Operation [" + request.method + "]" + + " Resource [" + request.url + "]") + camera_info = {"name": camera_info["name"], + "rtspurl": camera_info["rtspurl"], + "location": camera_info["location"]} + # listOfCameras.append(camera_info) + return Response("success") + + +@app.route('/v1/monitor/cameras///', methods=['GET']) +def get_camera(name, rtspurl, location): + """ + register camera with location + """ + app.logger.info("Received message from ClientIP [" + request.remote_addr + + "] Operation [" + request.method + "]" + + " Resource [" + request.url + "]") + # camera_info = {"name": name, "rtspurl": rtspurl, "location": location} + """ + if "mp4" in camera_info["rtspurl"]: + # video_file = VideoFile(camera_info["rtspurl"]) + # video_dict = {camera_info["name"]:video_file} + # listOfVideos.append(video_dict) + # return Response(video(video_file, camera_info["name"]), + # mimetype='multipart/x-mixed-replace; boundary=frame') + else: + # video_file = VideoCamera(camera_info["rtspurl"]) + # video_dict = {camera_info["name"]: video_file} + # listOfVideos.append(video_dict) + # return Response(video(video_file, camera_info["name"]), + # mimetype='multipart/x-mixed-replace; boundary=frame') + return Response("success") + """ + + +@app.route('/v1/monitor/cameras/', methods=['DELETE']) +def delete_camera(camera_name): + app.logger.info("Received message from ClientIP [" + request.remote_addr + + "] Operation [" + request.method + "]" + + " Resource [" + request.url + "]") + """ + for video1 in listOfVideos: + if camera_name in video1: + video_obj = video1[camera_name] + video_obj.delete() + for camera in listOfCameras: + if camera_name == camera["name"]: + listOfCameras.remove(camera) + for msg in listOfMsgs: + if camera_name in msg["msg"]: + listOfMsgs.remove(msg) + return Response("success") + """ + + +@app.route('/v1/monitor/cameras') +def query_cameras(): + app.logger.info("Received message from ClientIP [" + request.remote_addr + + "] Operation [" + request.method + "]" + + " Resource [" + request.url + "]") + # return jsonify(listOfCameras) + return Response("success") + + +def start_server(handler): + app.logger.addHandler(handler) + if config.ssl_enabled: + context = (config.ssl_certfilepath, config.ssl_keyfilepath) + app.run(host=config.server_address, debug=True, ssl_context=context, + threaded=True, port=config.server_port) + else: + app.run(host=config.server_address, debug=True, threaded=True, + port=config.server_port) diff --git a/example-apps/ROBO/retail_app/requirements.txt b/example-apps/ROBO/retail_app/requirements.txt new file mode 100644 index 0000000..9e7cae3 --- /dev/null +++ b/example-apps/ROBO/retail_app/requirements.txt @@ -0,0 +1,21 @@ +# +# Copyright 2020 Huawei Technologies Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +flask +requests +flask_sslify +opencv-python +flask_cors diff --git a/example-apps/ROBO/retail_app/retail_app.log b/example-apps/ROBO/retail_app/retail_app.log new file mode 100644 index 0000000..e69de29 diff --git a/example-apps/ROBO/retail_app/run.py b/example-apps/ROBO/retail_app/run.py new file mode 100644 index 0000000..ca3ad61 --- /dev/null +++ b/example-apps/ROBO/retail_app/run.py @@ -0,0 +1,25 @@ +# +# Copyright 2020 Huawei Technologies Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import logging +from logging.handlers import RotatingFileHandler +from inventry.retail_app import start_server + +if __name__ == '__main__': + handler = RotatingFileHandler('/usr/app/log/obj_detection.log', + maxBytes=10000000, backupCount=10) + handler.setLevel(logging.INFO) + start_server(handler) -- 2.16.6