X-Git-Url: https://gerrit.akraino.org/r/gitweb?a=blobdiff_plain;f=example-apps%2FROBO%2FaPaaS%2FObj_Detection_service%2Fdetection%2Fobj_detection_service.py;h=b35bd91eb9e71860d77aa22e203cd70e7ac2c241;hb=e6d40ddb2640f434a9d7d7ed99566e5e8fa60cc1;hp=271cab884ff3be777486d2c003652d652ca0703a;hpb=fd4df94e2b3b03850d00364d64c0c6b78a667bb7;p=ealt-edge.git diff --git a/example-apps/ROBO/aPaaS/Obj_Detection_service/detection/obj_detection_service.py b/example-apps/ROBO/aPaaS/Obj_Detection_service/detection/obj_detection_service.py index 271cab8..b35bd91 100644 --- a/example-apps/ROBO/aPaaS/Obj_Detection_service/detection/obj_detection_service.py +++ b/example-apps/ROBO/aPaaS/Obj_Detection_service/detection/obj_detection_service.py @@ -13,24 +13,34 @@ # See the License for the specific language governing permissions and # limitations under the License. # - import os +import cv2 import config from flask_sslify import SSLify -from flask import Flask, request, jsonify, Response +from flask import Flask, request, jsonify, Response, send_file from flask_cors import CORS from werkzeug import secure_filename +app = Flask(__name__) +CORS(app) +sslify = SSLify(app) +app.config['JSON_AS_ASCII'] = False +app.config['UPLOAD_PATH'] = '/usr/app/images/input/' +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']) +MODEL_PATH = '/usr/app/model/' +IMAGE_PATH = '/usr/app/images/result/' +count = 0 +listOfMsgs = [] + class model_info(): def __init__(self, model_name): - self.model = 'model_info/MobileNetSSD_deploy.caffemodel' - self.model_name = model_name - self.prototxt = 'model_info/MobileNetSSD_deploy.prototxt' - self.confidenceLevel = 80 - - def get_model(self): - return self.model + self.model_name = 'MobileNetSSD_deploy.caffemodel' + self.prototxt = 'MobileNetSSD_deploy.prototxt' + self.confidenceLevel = 0.2 def get_prototxt(self): return self.prototxt @@ -44,9 +54,8 @@ class model_info(): def get_confidence_level(self): return self.confidenceLevel - def update_model(self, model, prototxt, model_name): + def update_model(self, model_loc, prototxt, model_name): self.prototxt = prototxt - self.model = model self.model_name = model_name @@ -58,26 +67,139 @@ classNames = {0: 'background', 14: 'motorbike', 15: 'person', 16: 'pottedplant', 17: 'sheep', 18: 'sofa', 19: 'train', 20: 'tvmonitor'} -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 = [] + +def allowed_file(filename): + return '.' in filename and filename.rsplit('.', 1)[1].lower() \ + in ALLOWED_EXTENSIONS +# Obj-detection from input frame def Detection(img): - return + print ('inside detection func') + modelInfo = model_info("caffe") + ConfPercent = modelInfo.get_confidence_level() + model_name = modelInfo.get_model_name() + prototxt_name = modelInfo.get_prototxt() + model = MODEL_PATH + model_name + prototxt = MODEL_PATH + prototxt_name + image = app.config['UPLOAD_PATH'] + img + label = 'bottels' + print(ConfPercent) + print(model) + print(prototxt) + print("image path is" + image) -def allowed_file(filename): - return '.' in filename and filename.rsplit('.', 1)[1].lower()\ - in ALLOWED_EXTENSIONS + # Load the Caffe model + net = cv2.dnn.readNetFromCaffe(prototxt, model) + # Load image fro + frame = cv2.imread(image) + + frame_resized = cv2.resize(frame, (300, 300)) # resize frame for + # prediction + heightFactor = frame.shape[0]/300.0 + widthFactor = frame.shape[1]/300.0 + + # MobileNet requires fixed dimensions for input image(s) + # so we have to ensure that it is resized to 300x300 pixels. + # set a scale factor to image because network the objects has + # differents size. + # We perform a mean subtraction (127.5, 127.5, 127.5) + # to normalize the input; + # after executing this command our "blob" now has the shape: + # (1, 3, 300, 300) + blob = cv2.dnn.blobFromImage(frame_resized, 0.007843, (300, 300), + (127.5, 127.5, 127.5), False) + # Set to network the input blob + net.setInput(blob) + # Prediction of network + detections = net.forward() + + frame_copy = frame.copy() + # Size of frame resize (300x300) + cols = frame_resized.shape[1] + rows = frame_resized.shape[0] + + # For get the class and location of object detected, + # There is a fix index for class, location and confidence + # value in @detections array . + for i in range(detections.shape[2]): + confidence = detections[0, 0, i, 2] # Confidence of prediction + if confidence > ConfPercent: # Filter prediction + class_id = int(detections[0, 0, i, 1]) # Class label + + # Object location + xLeftBottom = int(detections[0, 0, i, 3] * cols) + yLeftBottom = int(detections[0, 0, i, 4] * rows) + xRightTop = int(detections[0, 0, i, 5] * cols) + yRightTop = int(detections[0, 0, i, 6] * rows) + + xLeftBottom_ = int(widthFactor * xLeftBottom) + yLeftBottom_ = int(heightFactor * yLeftBottom) + xRightTop_ = int(widthFactor * xRightTop) + yRightTop_ = int(heightFactor * yRightTop) + # Draw location of object + cv2.rectangle(frame_resized, (xLeftBottom, yLeftBottom), + (xRightTop, yRightTop), + (0, 255, 0)) + + cv2.rectangle(frame_copy, (xLeftBottom_, yLeftBottom_), + (xRightTop_, yRightTop_), + (0, 255, 0), -1) + opacity = 0.3 + cv2.addWeighted(frame_copy, opacity, frame, 1 - opacity, 0, frame) + + count = 0 + for i in range(detections.shape[2]): + confidence = detections[0, 0, i, 2] # Confidence of prediction + if confidence > ConfPercent: # Filter prediction + class_id = int(detections[0, 0, i, 1]) # Class label + + # Object location + xLeftBottom = int(detections[0, 0, i, 3] * cols) + yLeftBottom = int(detections[0, 0, i, 4] * rows) + xRightTop = int(detections[0, 0, i, 5] * cols) + yRightTop = int(detections[0, 0, i, 6] * rows) + + xLeftBottom_ = int(widthFactor * xLeftBottom) + yLeftBottom_ = int(heightFactor * yLeftBottom) + xRightTop_ = int(widthFactor * xRightTop) + yRightTop_ = int(heightFactor * yRightTop) + cv2.rectangle(frame, (xLeftBottom_, yLeftBottom_), + (xRightTop_, yRightTop_), + (0, 0, 0), 2) + + # Draw label and confidence of prediction in frame resized + if class_id in classNames: + label = classNames[class_id] + ": " + str(confidence) + labelSize, baseLine = cv2.getTextSize(label, + cv2.FONT_HERSHEY_TRIPLEX, + 0.8, 1) + + yLeftBottom_ = max(yLeftBottom_, labelSize[1]) + cv2.rectangle( + frame, + (xLeftBottom_, yLeftBottom_ - labelSize[1]), + (xLeftBottom_ + labelSize[0], yLeftBottom_ + baseLine), + (255, 255, 255), cv2.FILLED) + cv2.putText(frame, label, (xLeftBottom_, yLeftBottom_), + cv2.FONT_HERSHEY_TRIPLEX, 0.8, (0, 0, 0)) + print(label) + count = count + 1 + + print("total item count", count) + # cv2.namedWindow("frame", cv2.WINDOW_NORMAL) + print("before im write") + cv2.imwrite(IMAGE_PATH + "result.jpeg", frame) + # cv2.imshow("frame", frame) + # cv2.waitKey(0) + print("before im before destroy window") + # cv2.destroyAllWindows() + # Detect_result = {'ImposedImage': 'frame', 'ObjCount': count, + # 'ObjType': type, 'Time': time} + Detect_result = {'ObjCount': count} + print(Detect_result) + return Detect_result @app.route('/mep/v1/obj_detection/uploadModel', methods=['POST']) @@ -105,13 +227,13 @@ def setConfidenceLevel(): + "] Operation [" + request.method + "]" + " Resource [" + request.url + "]") - confidenceLevel = 80 + confidenceLevel = 0.2 modelInfo = model_info("caffe") modelInfo.set_confidence_level(confidenceLevel) return Response("success") -@app.route('/mep/v1/obj_detection/detect', methods=['GET']) +@app.route('/mep/v1/obj_detection/detect', methods=['POST']) def Obj_Detection(): """ Trigger the Obj detection on input frame/image @@ -131,16 +253,32 @@ def Obj_Detection(): raise IOError('No file') if file and allowed_file(file.filename): filename = secure_filename(file.filename) + print('file name', filename) file.save(os.path.join(app.config['UPLOAD_PATH'], filename)) app.logger.info('File successfully uploaded') + print('file path', app.config['UPLOAD_PATH'] + filename) Detect_result = Detection(filename) else: app.logger.info('Allowed file types are txt, pdf, png, jpg, jpeg, gif') return Response("failure") - # return jsonify({'Face number': num, 'Result': 'upload success'}) return jsonify(Detect_result) +@app.route('/mep/v1/obj_detection/image', methods=['GET']) +def image_download(): + """ + Trigger the Obj detection on input frame/image + Input: frame/image + :return: imposed frame, count, Obj type, time taken by detection + """ + app.logger.info("Received message from ClientIP [" + request.remote_addr + + "] Operation [" + request.method + "]" + + " Resource [" + request.url + "]") + + return send_file(IMAGE_PATH + "result.jpeg", + attachment_filename='result.jpeg') + + def start_server(handler): app.logger.addHandler(handler) if config.ssl_enabled: