Image Preprocessing

So we’ll define a few variables for image preprocessing.

#——Class Labels of the model——–#

classNames = { 0: ‘background’,

    1: ‘aeroplane’, 2: ‘bicycle’, 3: ‘bird’, 4: ‘boat’,

    5: ‘bottle’, 6: ‘bus’, 7: ‘car’, 8: ‘cat’, 9: ‘chair’,

    10: ‘cow’, 11: ‘diningtable’, 12: ‘dog’, 13: ‘horse’,

    14: ‘motorbike’, 15: ‘person’, 16: ‘pottedplant’,

    17: ‘sheep’, 18: ‘sofa’, 19: ‘train’, 20: ‘tvmonitor’ }

#——–Scaling parameters——#

input_shape=(300,300) #the required shape for the input image to pass to our model

mean = (127.5,127.5,127.5) #we’ll have to normalize the image pixels, and we’ll use this mean value to do that

scale = 0.007843 # then finally we’ll scale the image to meet the input criteria of the model

The dnn module provides us with the blobFromImage( or blobFromImages if you’re using multiple images) method for the pre-processing steps and we just have to pass the scaling parameters we defined above to complete the preprocessing step, and get the required blob i.e input image.

#——image preprocessing—-#

blob = cv2.dnn.blobFromImage(img,

                             scalefactor=scale,

                             size=input_shape,

                             mean=mean,

                             swapRB=True) #since our image is already in the BGR form because opencv by default reads it in BGR format

Deep Learning with Python OpenCV

Opencv 3.3 brought with a very improved and efficient (dnn) module which makes it very for you to use deep learning with OpenCV. You still cannot train models in OpenCV, and they probably don’t have any intention of doing anything like that, but now you can very easily use image processing and use the pre-trained models to make predictions using the dnn module.

This new version supports a number of large frameworks which include : 

  1. Tensorflow 
  2. Torch
  3. Caffe

Similar Reads

Objective

In this article, we’ll walk you through the entire process of using a pre-trained model, loading it using the dnn module, image preprocessing using the blobfromImage method in OpenCV, and then finally making predictions....

Download the model files and Install Dependencies

You can download the mobile_net_ssd model here: https://github.com/chuanqi305/MobileNet-SSD/...

Load the Model:

Since we’re using caffe model we’ll use the cv2.dnn.readNetFromCaffe module to load our model. You will need these two types of files to work with any pre-trained model using dnn module:...

Image Preprocessing

So we’ll define a few variables for image preprocessing....

Make Predictions using the model

Now that we have our input ready, we have to explicitly set it as input using the setInput() method and then pass it to our model and use the forward method to generate predictions....

Below is the complete Implementation

Python3 import cv2 import dlib from imutils import face_utils   img = cv2.imread('object (1).png')   #--------Model Path---------# proto_file = 'SSD_MobileNet_prototxt.txt' model_file = 'SSD_MobileNet.caffemodel'   #------Variables for the Model ---------# classNames = {0: 'background',               1: 'aeroplane', 2: 'bicycle',               3: 'bird', 4: 'boat',               5: 'bottle', 6: 'bus', 7: 'car',               8: 'cat', 9: 'chair',               10: 'cow', 11: 'diningtable',               12: 'dog', 13: 'horse',               14: 'motorbike', 15: 'person',               16: 'pottedplant',               17: 'sheep', 18: 'sofa',               19: 'train', 20: 'tvmonitor'}   input_shape = (300, 300) mean = (127.5, 127.5, 127.5) scale = 0.007843   #---------Load The Model--------# net = cv2.dnn.readNetFromCaffe(proto_file, model_file)   #------image preprocessing----# blob = cv2.dnn.blobFromImage(img,                              scalefactor=scale,                              size=input_shape,                              mean=mean,                              swapRB=True)  # since our image is already in the BGR form   net.setInput(blob) results = net.forward() for i in range(results.shape[2]):           # confidence     confidence = round(results[0, 0, i, 2],2)     if confidence > 0.7:                   # class id         id = int(results[0, 0, i, 1])                    # 3-6 contains the coordinate         x1, y1, x2, y2 = results[0, 0, i, 3:7]                    # print(x1,y1,x2,y2)         # scale these coordinates to out image pixel         ih, iw, ic = img.shape         x1, x2 = int(x1*iw), int(x2*iw)         y1, y2 = int(y1 * ih), int(y2 * ih)         cv2.rectangle(img,                       (x1, y1),                       (x2, y2),                       (0, 200, 0), 2)         cv2.putText(img, f'{classNames[id]}:{confidence*100}',                     (x1+30, y1-30),                     cv2.FONT_HERSHEY_DUPLEX,                     1, (255, 0, 0), 1)     # print(results[0,0,i,:])   img = cv2.resize(img, (640, 720)) cv2.imshow('Image', img) # cv2.imwrite('output1.jpg',img) # Uncomment this line to save the output cv2.waitKey()...

What next?

...

Contact Us