ROBO usecase: Obj Detection service base framework
[ealt-edge.git] / example-apps / ROBO / aPaaS / Obj_Detection_service / detection / obj_detection_service.py
1 #
2 # Copyright 2020 Huawei Technologies Co., Ltd.
3 #
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
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 #
16
17 import config
18 from flask_sslify import SSLify
19 from flask import Flask, Response
20 from flask_cors import CORS
21
22
23 class model_info():
24     def __init__(self, model_name):
25         self.model = 'model_info/MobileNetSSD_deploy.caffemodel'
26         self.model_name = model_name
27         self.prototxt = 'model_info/MobileNetSSD_deploy.prototxt'
28         self.confidenceLevel = 80
29
30     def get_model(self):
31         return self.model
32
33     def get_prototxt(self):
34         return self.prototxt
35
36     def get_model_name(self):
37         return self.model_name
38
39     def set_confidence_level(self, confidenceLevel):
40         self.confidenceLevel = confidenceLevel
41
42     def get_confidence_level(self):
43         return self.confidenceLevel
44
45     def update_model(self, model, prototxt, model_name):
46         self.prototxt = prototxt
47         self.model = model
48         self.model_name = model_name
49
50
51 # Labels of Network.
52 classNames = {0: 'background',
53               1: 'aeroplane', 2: 'bicycle', 3: 'bird', 4: 'boat',
54               5: 'bottle', 6: 'bus', 7: 'car', 8: 'cat', 9: 'chair',
55               10: 'cow', 11: 'diningtable', 12: 'dog', 13: 'horse',
56               14: 'motorbike', 15: 'person', 16: 'pottedplant',
57               17: 'sheep', 18: 'sofa', 19: 'train', 20: 'tvmonitor'}
58
59 app = Flask(__name__)
60 CORS(app)
61 sslify = SSLify(app)
62 app.config['JSON_AS_ASCII'] = False
63 app.config['UPLOAD_PATH'] = '/usr/app/images/'
64 app.config['supports_credentials'] = True
65 app.config['CORS_SUPPORTS_CREDENTIALS'] = True
66 app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
67 ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
68 count = 0
69 listOfMsgs = []
70
71
72 def allowed_file(filename):
73     return '.' in filename and filename.rsplit('.', 1)[1].lower()\
74            in ALLOWED_EXTENSIONS
75
76
77 @app.route('/mep/v1/obj_detection/uploadModel', methods=['POST'])
78 def uploadModel():
79     """
80     upload model
81     :return: html file
82     """
83
84     return Response("success")
85
86
87 @app.route('/mep/v1/obj_detection/confidencelevel', methods=['POST'])
88 def setConfidenceLevel():
89     """
90     Trigger the video_feed() function on opening "0.0.0.0:5000/video_feed" URL
91     :return:
92     """
93
94     return Response("success")
95
96
97 @app.route('/mep/v1/obj_detection/detect', methods=['GET'])
98 def Obj_Detection():
99     """
100     Trigger the Obj detection on input frame/image
101     Input: frame/image
102     :return: imposed frame, count, Obj type, time taken by detection
103     """
104     return Response("success")
105
106
107 def start_server(handler):
108     app.logger.addHandler(handler)
109     if config.ssl_enabled:
110         context = (config.ssl_certfilepath, config.ssl_keyfilepath)
111         app.run(host=config.server_address, debug=True, ssl_context=context,
112                 threaded=True, port=config.server_port)
113     else:
114         app.run(host=config.server_address, debug=True, threaded=True,
115                 port=config.server_port)