Face Detection is currently a trending technology. You look out the offline world and internet world everywhere you see faces. Faces in pictures as well as in Videos. Of course, Our brain easily identifies the person in the pictures and videos. But we want that computer or mobiles itself identifies these things. Therefore In this intuition, I want you to build a simple but effective face detection using Computer Vision Algorithms.
But before you go further in the coding part, first of all, you should know how the Face Detections works inside the box.
How does the Face Detection work?
Face Detection is the ability to locate the faces in a photograph. You create a two steps pipeline for face detection.
Step 1: Build a Face Detection Model
You create a machine learning model that detects faces in a photograph and tell that it has a face or not.
Step 2: Use the Sliding Window Classifier.
After building the model in the step 1, Sliding Window Classifier will slides in the photograph until it finds the face. If it finds then, locations of the face are noted.
There are various face detection algorithms like HOG( Histogram of Oriented Gradients), Convolutional Neural Network. Both detect the face. The only difference is their accuracy. Deep Learning ( Convolutional Neural Network) method is more accurate than the HOG. But it requires more computational power like High GPU, CPU e.t.c as compared to HOG. That’ why HOG is widely used on Mobile Platforms.
I am not going in deep to tell you how the HOG method detects the image. Here you will know how to apply it. If you want to learn in details then you can take this course.
How does the Histogram of Oriented Gradients (HOG) works?
Let’s know How the HOG algorithm works step by step.
Step 1: Converts the input image to black and white.
HOG only considers the changes between the light and dark areas in the image. It ignores the color information. That’s why it converts colored image into the black and white image.
Step 2: Looks for the gradient
Now after the step 1, it looks for the gradient in each pixel. A gradient is a direction from the lighter area to the darker area. It repeats the process for the entire pixels of a black and white image and draws the gradient image of it.
How to train the model to detect the face?
The trained datasets are available like dlib, face recognition that is free to use. These libraries contain all the HOG represented images and built a machine learning model. If you want to build your own face dataset then go for the following steps.
Step 1: Collect the Training dataset.
The first stage is to collect the HOG represented images. You can create them or use the existing dataset openly available online.
Step 2: Train the model
You will build a classifier model to classify there is a face or not in the image. You will use all the HOG represented images for training the model.
Step 3: Detect the Face
Sliding Window Classifier works on it. It slides on the entire image until it returns true and detects the position of the image.
Lets code a simple and effective face detection in python. It takes a picture as an input and draws a rectangle around the faces.
Coding Face Detection
Step 1: Import the necessary library
import PIL.Image import PIL.ImageDraw import face_recognition
PIL is an open source Python image libraries that allow you to open, manipulate and save the different image file formats. It used to easily display the image and draw a line on the top of the image.
Face recognition library will give you access to use the face detection model. Thus it relieves you from building your own face detection model for finding the faces in the photograph.
Step 2: Load the Image into the Numpy array
In this step for manipulating the image, you have to first convert into the Numpy array. As you have to get the locations of the image.
The following is the input image we have to detect the faces.
Step 3: Find all the faces in the photograph
face_locations = face_recognition.face_locations(image) no_of_faces = len(face_locations) print(no_of_faces)
Step 4: Draw the rectangular shape.
You have to draw the rectangular shape around the faces. First, you will convert the image to the PIL Image using the fromarray() method. Then You will use the PIL Draw() method of for this.
pil_image = PIL.Image.fromarray(image) for face_location in face_locations: top,right,bottom,left =face_location draw_shape = PIL.ImageDraw.Draw(pil_image) draw_shape.rectangle([left, top, right, bottom],outline="red")
Step 5: Save and Show the Image
You will use the save() and show() method for showing the final image. You can see it contains all the faces with the rectangular image.
#display and save the image pil_image.save("images/output_image.jpg") pil_image.show()
Hurray, you have build your own face detection and Recognition mode. Now you can use all these codes in your projects like in face detection in camera e.t.c. The full code is available on the GitHub. If you have any query about this then please contact us or message us Data Science Learner Page.