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 :
- Tensorflow
- Torch
- Caffe
Contact Us