Face Detection with Python

In this article, I will show you how to create a simple face detector using Python. Building a program that detects faces is a great way to get started with machine learning computer vision tasks. So I will introduce you here a very easy way of face detection with Python.

As the title suggests, I will write a program for the task of face detection with Python. When I say a program you can understand it like teaching a machine what to do. I like to use teaching rather than programming because that’s actually what I’m doing here.

Also, Read – Machine Learning Full Course for free.

The best way to learn about everything is to teach, so while teaching a machine to detect faces, we are also learning. So, before I start this face detection task with Python, I want to share the difference between face detection and face recognition.

Face Detection Vs Face Recognition

face detection

Face detection and face recognition may look very similar, but in reality, they are not the same. Let’s understand the difference so as not to miss the point.

Face detection is the process of detecting faces, from an image or a video does not matter. The program does nothing more than finding the faces. But on the other hand in the task of face recognition, the program finds faces and can also tell which face belongs to which. So it’s more informative than just detecting them. There is more programming, in other words, more training in the process.

Let’s say you are looking at the street and the cars are passing. Face detection is like saying the passing object is a car. And facial recognition is like being able to tell the model of the passing car. Here is a nice picture showing the difference in practice.

In this article, I will continue with the task of detecting faces. If you want to learn facial recognition, you can mention me in the comments section below.

Face Detection with Python

I will use the OpenCV library in Python which is used as the primary tool for the tasks of computer vision in Python. If you are new to OpenCV then this task is the best to start with it.

If you want to learn more about the OpenCV you can watch a complete tutorial on OpenCV from here. Now let’s get started with the task of Face Detection with Python.

First you need to install the OpenCV library in Python which can be easily installed by using the pip command; pip install opencv-python. After installing this library you need to simply import this by using the command below:

import cv2
Code language: Python (python)

OpenCV library in python is blessed with many pre-trained classifiers for face, eyes, smile, etc. These XML files are stored in a folder. We will use the face detection model. You can download the pre-trained face detection model from here.

After downloading and saving the file in your directory, let’s load it into the face detection program:

face_cascade = cv2.CascadeClassifier('face_detector.xml')Code language: Python (python)

The next step is to choose an image on which you want to test your code. Make sure there is at least one face in the image so that the face detection program can find at least one face.

After choosing an image, let’s define it in our program. Make sure the image is in the same directory you are working in:

img = cv2.imread('image.jpg')Code language: Python (python)

Detect Faces

You will be amazed at how short the face detection code is. Thanks to the people who contribute to OpenCV. Here is the code that detects faces in an image:

faces = face_cascade.detectMultiScale(img, 1.1, 4)
Code language: Python (python)

Now the last step is to draw rectangles around the detected faces, which can be easily done with the following code:

for (x, y, w, h) in faces: 
  cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2.imwrite("face_detected.png", img) 
print('Successfully saved')
Code language: Python (python)
Image for post detect faces

So this is how we can easily detect a face or as many as faces in the image. I hope you liked this article on face detection with Python. Feel free to ask your valuable questions in the comments section below.

Follow Us:

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1534


  1. Similarly like face detection, can you show how it is done for motorbike helmet detection using python.

Leave a Reply