# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Graduate Rotational Internship Program - The Sparks Foundation, SG
#
# ## GRIP - Computer Vision & IoT - Task 1 (Object Detection Using MobileNet & SSD)
#
# **<NAME>**\
# [LinkedIn](https://linkedin.com/in/avishj/) [GitHub](https://github.com/avishj/) [Website](https://avishj.dev/)

# Object detection, a subset of computer vision, is an automated method for locating interesting objects in an image with respect to the background. Solving the object detection problem means placing a tight bounding box around these objects and associating the correct object category with each bounding box. Like other computer vision tasks, deep learning is the state-of-art method to perform object detection. MobileNet is an efficient and portable CNN architecture that is used in real world applications. MobileNets primarily use depthwise seperable convolutions in place of the standard convolutions used in earlier architectures to build lighter models. Single Shot Detector's are used to ensure one shot can detect multiple objects, for example YOLO.
#
# In the current task, we shall utilise MobileNetSSD with a DNN Module in OpenCV to build our object detection system.\
# Reference: [Object detection with deep learning and OpenCV - PyImageSearch](https://www.pyimagesearch.com/2017/09/11/object-detection-with-deep-learning-and-opencv/)

# ### Step 1: Handling Imports

import numpy as np
import cv2

# ### Step 2: Initialising Classes

# +
# Generate a set of bounding box colors for each class that was used to train MobileNet

CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
"dog", "horse", "motorbike", "person", "pottedplant", "sheep",
"sofa", "train", "tvmonitor"]
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
# -

# ### Step 3: Loading Pre-Trained Model & Images to be Detected

net = cv2.dnn.readNetFromCaffe('MobileNetSSD_deployprototxt.txt','MobileNetSSD_deploy.caffemodel')
img = cv2.imread('imgs/multi2.jpg')
img= cv2.resize(img, (500, 500))
cv2.imshow("Detected", img)
cv2.waitKey(0)

# ### Step 4: Detecting Objects

# +
# Extracting Dimensions & Creating 500x500 Blob, then passing it to the DNN

(h, w) = img.shape[:2]
blob = cv2.dnn.blobFromImage(img, 0.007843,(500, 500), 127.5)
net.setInput(blob)
detections = net.forward()
# -

# ### Step 5: Labelling Predictions, Boxing Objects & Displaying the Probability  

for i in np.arange(0, detections.shape[2]):
    
    # Extracting Confidence
    
    confidence = detections[0, 0, i, 2]
    
    # Filtering Confidence with minimum 30%
    
    if confidence > 0.3 :
        
        # Extracting Index of Class Label & Computing Dimensions of the Bounding Box
        
        idx = int(detections[0, 0, i, 1])
        box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
        (startX, startY, endX, endY) = box.astype("int")
        
        # Displaying the Probability
        
        label = "{}: {:.2f}%".format(CLASSES[idx], confidence * 100)
        print("{}".format(label))
        cv2.rectangle(img, (startX, startY), (endX, endY), COLORS[idx], 2)
        y = startY - 15 if startY - 15 > 15 else startY + 15
        cv2.putText(img, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)

# ### Step 6: Displaying Output Image

cv2.imshow("Output", img)
cv2.waitKey(0)


# ### Step 7: Function to Detect & Output from Input

def detect_object(imgloc):
    img = cv2.imread("imgs/" + imgloc)
    img = cv2.resize(img, (500, 500))
    cv2.imshow("The Input: ", img)
    cv2.waitKey(0)
    (h, w) = img.shape[:2]
    blob = cv2.dnn.blobFromImage(img, 0.007843,(200, 200), 127.5)
    net.setInput(blob)
    detections = net.forward()
    for i in np.arange(0, detections.shape[2]):
        confidence = detections[0, 0, i, 2]
        if confidence > 0.3:
            idx = int(detections[0, 0, i, 1])
            box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
            (startX, startY, endX, endY) = box.astype("int")
            label = "{}: {:.2f}%".format(CLASSES[idx], confidence * 100)
            print("{}".format(label))
            cv2.rectangle(img, (startX, startY), (endX, endY), COLORS[idx], 2)
            y = startY - 15 if startY - 15 > 15 else startY + 15
            cv2.putText(img, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
    cv2.imshow("The Output:", img)
    cv2.waitKey(0)   
    


# ### Step 8: Testing the Function

print('cars.jpg:')
detect_object('cars.jpg')
print('dhs.jsp:')
detect_object('dhs.jpg')
print('dp.jpg')
detect_object('dp.jpg')
print('horse.jpg:')
detect_object('horse.jpg')
print('multi1.jpg:')
detect_object('multi1.jpg')
print('multi2.jpg:')
detect_object('multi2.jpg')

StarcoderdataJupyterScriptsDedupFiltered