Zephyrnet Logo

Auto Rotate Images Using Deep Learning

Date:

Auto Rotate Images Using Deep Learning

Follow these 5 simple steps to auto rotate images and get the right angle in human photos using computer vision.


By Bala Venkatesh, Data Scientist

Demo

Demo

Let’s see how to auto rotate the image without manual rotation. These days, computer vision has evolved so much all over the world. I just played and implemented a small technique to auto rotate images using computer vision.

Steps to auto rotate the images:

  1. Read the input image.
  2. Detect Face by Caffe model.
  3. If the face is not detected then rotate the image.
  4. Again detect face with rotated images.
  5. Rotate the image with three angles until face detect.

Before going to implement this technique we will see what are the dependency libraries and model needed.

  1. OpenCV
  2. Numpy
  3. Caffe model(Deep learning)

 
Step 1: Import all the above required libraries.

import cv2
import numpy as np


 
Step 2: Download the Caffe model and file and prototxt file. Let us see why we need those two files and what that is.

What is the Caffe model file?

Caffe is a deep learning framework developed by the Berkeley Vision and Learning Center (BVLC). It is written in C++ and has Python and Matlab bindings. After training the model with our data set, we will get the trained model in a file with an extension.

What is deploy.prototxt file?

The prototxt is a text file that holds information about the structure of the neural network: A list of layers in the neural network. The parameters of each layer, such as its name, type, input dimensions, and output dimensions. The connections between the layers. That prototxt file is only to deploy the model and cannot be used to train it.

 
Step 3: This is the main method to read an image file using OpenCV. Then pass image into detect_face method(Step 4) it will give you True(Detected) or False(Not Detected). If it returns FALSE then the image is not the correct angle hence we need to rotate the image angle as per below angles step by step.

Rotate Angle -> 90 -> 180 -> 270

Figure

Rotate angle

def main(): frame = cv2.imread(‘6.jpg’) original_status = detect_face(frame) (h, w) = frame.shape[:2] # calculate the center of the image center = (w / 2, h / 2) scale = 1.0 angle_90 = 90 angle_180 = 180 angle_270 = 270 if original_status is None: status_90 = rotate_image(frame,center,scale,angle_90) if status_90 is None: status_180 = rotate_image(frame,center,scale,angle_180) if status_180 is None: status_270 = rotate_image(frame,center,scale, angle_270)


 
Step 4: Here is the detect_face method to detect face using the Caffe model. We can use OpenCV dnn module to read Caffe models using the readNetFromCaffe method. Then convert our image into the blob to pass neural network based on output weight it will return probability values. I have used 0.7 as min accuracy values. If the value is more than that we can detect face images. The Caffe model was trained by right angle faces images so it will detect only if the face image is the correct angle.

def detect_face(frame):net = cv2.dnn.readNetFromCaffe(‘deploy.prototxt’, ‘res10_300x300_ssd_iter_140000.caffemodel’) (h, w) = frame.shape[:2]blob = cv2.dnn.blobFromImage(cv2.resize(frame,(300,300)), 1.0, (300,300), (104.0,177.0,123.0))net.setInput(blob)faces = net.forward()for i in range(0, faces.shape[2]): confidence = faces[0,0,i,2] if confidence < 0.7: continuebox = faces[0,0,i,3:7] * np.array([w,h,w,h]) (startX, startY, endX, endY) = box.astype(‘int’)text = “face “ + “{:.2f}%”.format(confidence * 100)cv2.imwrite(‘test.jpg’,frame) return True


 
Step 5: Let us see how to rotate the image using OpenCV.

def rotate_image(frame,center,scale,angle): (h, w) = frame.shape[:2] M = cv2.getRotationMatrix2D(center, angle, scale) frame = cv2.warpAffine(frame, M, (h, w)) return detect_face(frame)


Conclusion

 
That’s it. You made it!! Finally, you can see the correct angle image.

Image

Happy Learning to all!! if you need any help or assistance please connect with me on LinkedIn and Twitter.

 
Bio: Bala Venkatesh is a Data Scientist. He has a passion for understanding technology at a fundamental level and sharing ideas and code.

Original. Reposted with permission.

Related:

Source: https://www.kdnuggets.com/2020/07/auto-rotate-images-deep-learning.html

spot_img

Latest Intelligence

spot_img

Chat with us

Hi there! How can I help you?