How to auto rotate the image using Deep learning!!!

5 simple steps to auto rotate the image to get the right angle in the human photos using computer vision

Bala Venkatesh
Jun 29, 2020 · 3 min read
Image for post
Image for post
Demo

The above image showed the input (left) and output (right). 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 library 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

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:
continue
box = 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 for post
Image for post

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

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Sign up for Analytics Vidhya News Bytes

By Analytics Vidhya

Latest news from Analytics Vidhya on our Hackathons and some of our best articles! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Bala Venkatesh

Written by

I have a passion for understanding technology at a fundamental level and Sharing ideas and code. * Aspire to Inspire before I expire* https://balavenkatesh.com

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Bala Venkatesh

Written by

I have a passion for understanding technology at a fundamental level and Sharing ideas and code. * Aspire to Inspire before I expire* https://balavenkatesh.com

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store