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.
19 from flask_sslify import SSLify
20 from flask import Flask, request, jsonify, Response, send_file
21 from flask_cors import CORS
22 from werkzeug import secure_filename
27 app.config['JSON_AS_ASCII'] = False
28 app.config['UPLOAD_PATH'] = '/usr/app/images/input/'
29 app.config['supports_credentials'] = True
30 app.config['CORS_SUPPORTS_CREDENTIALS'] = True
31 app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
32 ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
33 MODEL_PATH = '/usr/app/model/'
34 IMAGE_PATH = '/usr/app/images/result/'
40 def __init__(self, model_name):
41 self.model_name = 'MobileNetSSD_deploy.caffemodel'
42 self.prototxt = 'MobileNetSSD_deploy.prototxt'
43 self.confidenceLevel = 0.2
45 def get_prototxt(self):
48 def get_model_name(self):
49 return self.model_name
51 def set_confidence_level(self, confidenceLevel):
52 self.confidenceLevel = confidenceLevel
54 def get_confidence_level(self):
55 return self.confidenceLevel
57 def update_model(self, model_loc, prototxt, model_name):
58 self.prototxt = prototxt
59 self.model_name = model_name
63 classNames = {0: 'background',
64 1: 'aeroplane', 2: 'bicycle', 3: 'bird', 4: 'boat',
65 5: 'bottle', 6: 'bus', 7: 'car', 8: 'cat', 9: 'chair',
66 10: 'cow', 11: 'diningtable', 12: 'dog', 13: 'horse',
67 14: 'motorbike', 15: 'person', 16: 'pottedplant',
68 17: 'sheep', 18: 'sofa', 19: 'train', 20: 'tvmonitor'}
71 def allowed_file(filename):
72 return '.' in filename and filename.rsplit('.', 1)[1].lower() \
76 # Obj-detection from input frame
78 print ('inside detection func')
79 modelInfo = model_info("caffe")
80 ConfPercent = modelInfo.get_confidence_level()
81 model_name = modelInfo.get_model_name()
82 prototxt_name = modelInfo.get_prototxt()
84 model = MODEL_PATH + model_name
85 prototxt = MODEL_PATH + prototxt_name
86 image = app.config['UPLOAD_PATH'] + img
91 print("image path is" + image)
93 # Load the Caffe model
94 net = cv2.dnn.readNetFromCaffe(prototxt, model)
96 frame = cv2.imread(image)
98 frame_resized = cv2.resize(frame, (300, 300)) # resize frame for
100 heightFactor = frame.shape[0]/300.0
101 widthFactor = frame.shape[1]/300.0
103 # MobileNet requires fixed dimensions for input image(s)
104 # so we have to ensure that it is resized to 300x300 pixels.
105 # set a scale factor to image because network the objects has
107 # We perform a mean subtraction (127.5, 127.5, 127.5)
108 # to normalize the input;
109 # after executing this command our "blob" now has the shape:
111 blob = cv2.dnn.blobFromImage(frame_resized, 0.007843, (300, 300),
112 (127.5, 127.5, 127.5), False)
113 # Set to network the input blob
115 # Prediction of network
116 detections = net.forward()
118 frame_copy = frame.copy()
119 # Size of frame resize (300x300)
120 cols = frame_resized.shape[1]
121 rows = frame_resized.shape[0]
123 # For get the class and location of object detected,
124 # There is a fix index for class, location and confidence
125 # value in @detections array .
126 for i in range(detections.shape[2]):
127 confidence = detections[0, 0, i, 2] # Confidence of prediction
128 if confidence > ConfPercent: # Filter prediction
129 class_id = int(detections[0, 0, i, 1]) # Class label
132 xLeftBottom = int(detections[0, 0, i, 3] * cols)
133 yLeftBottom = int(detections[0, 0, i, 4] * rows)
134 xRightTop = int(detections[0, 0, i, 5] * cols)
135 yRightTop = int(detections[0, 0, i, 6] * rows)
137 xLeftBottom_ = int(widthFactor * xLeftBottom)
138 yLeftBottom_ = int(heightFactor * yLeftBottom)
139 xRightTop_ = int(widthFactor * xRightTop)
140 yRightTop_ = int(heightFactor * yRightTop)
141 # Draw location of object
142 cv2.rectangle(frame_resized, (xLeftBottom, yLeftBottom),
143 (xRightTop, yRightTop),
146 cv2.rectangle(frame_copy, (xLeftBottom_, yLeftBottom_),
147 (xRightTop_, yRightTop_),
150 cv2.addWeighted(frame_copy, opacity, frame, 1 - opacity, 0, frame)
153 for i in range(detections.shape[2]):
154 confidence = detections[0, 0, i, 2] # Confidence of prediction
155 if confidence > ConfPercent: # Filter prediction
156 class_id = int(detections[0, 0, i, 1]) # Class label
159 xLeftBottom = int(detections[0, 0, i, 3] * cols)
160 yLeftBottom = int(detections[0, 0, i, 4] * rows)
161 xRightTop = int(detections[0, 0, i, 5] * cols)
162 yRightTop = int(detections[0, 0, i, 6] * rows)
164 xLeftBottom_ = int(widthFactor * xLeftBottom)
165 yLeftBottom_ = int(heightFactor * yLeftBottom)
166 xRightTop_ = int(widthFactor * xRightTop)
167 yRightTop_ = int(heightFactor * yRightTop)
168 cv2.rectangle(frame, (xLeftBottom_, yLeftBottom_),
169 (xRightTop_, yRightTop_),
172 # Draw label and confidence of prediction in frame resized
173 if class_id in classNames:
174 label = classNames[class_id] + ": " + str(confidence)
175 labelSize, baseLine = cv2.getTextSize(label,
176 cv2.FONT_HERSHEY_TRIPLEX,
179 yLeftBottom_ = max(yLeftBottom_, labelSize[1])
182 (xLeftBottom_, yLeftBottom_ - labelSize[1]),
183 (xLeftBottom_ + labelSize[0], yLeftBottom_ + baseLine),
184 (255, 255, 255), cv2.FILLED)
185 cv2.putText(frame, label, (xLeftBottom_, yLeftBottom_),
186 cv2.FONT_HERSHEY_TRIPLEX, 0.8, (0, 0, 0))
190 print("total item count", count)
191 # cv2.namedWindow("frame", cv2.WINDOW_NORMAL)
192 print("before im write")
193 cv2.imwrite(IMAGE_PATH + "result.jpeg", frame)
194 # cv2.imshow("frame", frame)
196 print("before im before destroy window")
197 # cv2.destroyAllWindows()
198 # Detect_result = {'ImposedImage': 'frame', 'ObjCount': count,
199 # 'ObjType': type, 'Time': time}
200 Detect_result = {'ObjCount': count}
205 @app.route('/mep/v1/obj_detection/uploadModel', methods=['POST'])
211 app.logger.info("Received message from ClientIP [" + request.remote_addr
212 + "] Operation [" + request.method + "]" +
213 " Resource [" + request.url + "]")
215 modelInfo = model_info("caffe")
216 modelInfo.update_model("caffe", "mobilenet_ssd", "prototext")
217 return Response("success")
220 @app.route('/mep/v1/obj_detection/confidencelevel', methods=['POST'])
221 def setConfidenceLevel():
223 Trigger the video_feed() function on opening "0.0.0.0:5000/video_feed" URL
226 app.logger.info("Received message from ClientIP [" + request.remote_addr
227 + "] Operation [" + request.method + "]" +
228 " Resource [" + request.url + "]")
230 confidenceLevel = 0.2
231 modelInfo = model_info("caffe")
232 modelInfo.set_confidence_level(confidenceLevel)
233 return Response("success")
236 @app.route('/mep/v1/obj_detection/detect', methods=['POST'])
239 Trigger the Obj detection on input frame/image
241 :return: imposed frame, count, Obj type, time taken by detection
243 app.logger.info("Received message from ClientIP [" + request.remote_addr
244 + "] Operation [" + request.method + "]" +
245 " Resource [" + request.url + "]")
247 if 'file' not in request.files:
248 raise IOError('No file')
250 file = request.files['file']
251 if file.filename == '':
252 app.logger.info('No file selected for uploading')
253 raise IOError('No file')
254 if file and allowed_file(file.filename):
255 filename = secure_filename(file.filename)
256 print('file name', filename)
257 file.save(os.path.join(app.config['UPLOAD_PATH'], filename))
258 app.logger.info('File successfully uploaded')
259 print('file path', app.config['UPLOAD_PATH'] + filename)
260 Detect_result = Detection(filename)
262 app.logger.info('Allowed file types are txt, pdf, png, jpg, jpeg, gif')
263 return Response("failure")
264 return jsonify(Detect_result)
267 @app.route('/mep/v1/obj_detection/image', methods=['GET'])
268 def image_download():
270 Trigger the Obj detection on input frame/image
272 :return: imposed frame, count, Obj type, time taken by detection
274 app.logger.info("Received message from ClientIP [" + request.remote_addr
275 + "] Operation [" + request.method + "]" +
276 " Resource [" + request.url + "]")
278 return send_file(IMAGE_PATH + "result.jpeg",
279 attachment_filename='result.jpeg')
282 def start_server(handler):
283 app.logger.addHandler(handler)
284 if config.ssl_enabled:
285 context = (config.ssl_certfilepath, config.ssl_keyfilepath)
286 app.run(host=config.server_address, debug=True, ssl_context=context,
287 threaded=True, port=config.server_port)
289 app.run(host=config.server_address, debug=True, threaded=True,
290 port=config.server_port)