Added Obj detection function 49/3949/1
authorkhemendra kumar <khemendra.kumar@huawei.com>
Tue, 1 Dec 2020 18:03:04 +0000 (23:33 +0530)
committerkhemendra kumar <khemendra.kumar@huawei.com>
Tue, 1 Dec 2020 18:03:04 +0000 (23:33 +0530)
Signed-off-by: khemendra kumar <khemendra.kumar@huawei.com>
Change-Id: I5938bb90fe5dc8803e924c5502f35cff647a5c24

example-apps/ROBO/aPaaS/Obj_Detection_service/detection/obj_detection_service.py

index 271cab8..055f864 100644 (file)
@@ -14,7 +14,9 @@
 # limitations under the License.
 #
 
+
 import os
+import cv2
 import config
 from flask_sslify import SSLify
 from flask import Flask, request, jsonify, Response
@@ -58,6 +60,7 @@ 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)
@@ -71,13 +74,137 @@ 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
 
+    modelInfo = model_info("caffe")
+    ConfPercent = modelInfo.get_confidence_level()
+    model = modelInfo.get_model()
+    prototxt = modelInfo.get_prototxt()
 
-def allowed_file(filename):
-    return '.' in filename and filename.rsplit('.', 1)[1].lower()\
-           in ALLOWED_EXTENSIONS
+    model = '/home/root1/My_Work/Akraino/MEC_BP/Rel4/Retail-apps/aPaaS/' \
+            'src/Obj_Detection_service/' + 'MobileNetSSD_deploy.caffemodel'
+
+    prototxt = '/home/root1/My_Work/Akraino/MEC_BP/Rel4/Retail-apps/aPaaS/' \
+               'src/Obj_Detection_service/' + 'MobileNetSSD_deploy.prototxt'
+    print(ConfPercent)
+    print(model)
+    print(prototxt)
+
+    # Load the Caffe model
+    net = cv2.dnn.readNetFromCaffe(prototxt, model)
+    # Load image fro
+    frame = cv2.imread('/home/root1/My_Work/Akraino/MEC_BP/Rel4/Retail-apps/'
+                       'aPaaS/src/Obj_Detection_service/images/' + img)
+    print('/home/root1/My_Work/Akraino/MEC_BP/Rel4/Retail-apps/aPaaS/'
+          'src/Obj_Detection_service/images/' + img)
+    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)
+    cv2.imwrite("/home/root1/My_Work/Akraino/MEC_BP/tmp/1.jpeg", frame)
+    cv2.imshow("frame", frame)
+    cv2.waitKey(0)
+    cv2.destroyAllWindows()
+    # Detect_result = {'ImposedImage': 'frame', 'ObjCount': count,
+    # 'ObjType': type, 'Time': time}
+    Detect_result = {'ImposedImage': "frame", 'ObjCount': count}
+    return Detect_result
 
 
 @app.route('/mep/v1/obj_detection/uploadModel', methods=['POST'])