Face Detection Project in Python [In 5 Easy Steps]

Object identification and face detection are probably the most popular applications of computer vision. This technology finds applications in various industries, such as security and social media. So we’re building a face detection project through Python. 

Note that you should be familiar with programming in Python, OpenCV, and NumPy. It will ensure that you don’t get confused while working on this project. Let’s get started. 

We’ve shared two methods to perform face recognition. The first uses Python’s face recognition library, while the other one uses OpenCV and NumPy. Check out our data science programs to learn more.

Learn Machine Learning Courses from the World’s top Universities. Earn Masters, Executive PGP, or Advanced Certificate Programs to fast-track your career.

Face Recognition with Python’s ‘Face Recognition’

Probably the easiest method to detect faces is to use the face recognition library in Python. It had 99.38% accuracy in the LFW database. Using it is quite simple and doesn’t require much effort. Moreover, the library has a dedicated ‘face_recognition’ command for identifying faces in images. 

How to Use Face Recognition

You can distinguish faces in images by using the ‘face_locations’ command:

import face_recognition

image = face_recognition.load_image_file(“your_file.jpg”)

face_locations = face_recognition.face_locations(image)

It can also recognize faces and associate them with their names:

import face_recognition

known_image = face_recognition.load_image_file(“modi.jpg”)

unknown_image = face_recognition.load_image_file(“unknown.jpg”)

modi_encoding = face_recognition.face_encodings(known_image)[0]

unknown_encoding = face_recognition.face_encodings(unknown_image)[0]

results = face_recognition.compare_faces([modi_encoding], unknown_encoding)

Picture Contains “Narendra Modi”

There are many other things you can perform with this library by combining it with others. We’ll now discuss performing face recognition with other prominent libraries in Python, particularly OpenCV and NumPy.

Read more: Python NumPy Tutorial: Learn Python Numpy With Examples

Face Detection Project in Python

In this project, we’ve performed face detection and recognition by using OpenCV and NumPy. We’ve used Raspberry Pi, but you can also use it with other systems. You’ll only have to modify the code slightly to use it on some other device (such as a Mac or a Windows PC). 

Some credit for this project goes to Marcelo Rovai.

Step #1: Install Libraries

First, you should install the required libraries, OpenCV, and NumPy. You can install it easily through:

pip install opencv-python

pip install opencv-contrib-python 

For installing NumPy in your system, use the same command as above and replace ‘opencv-python’ with ‘numpy’:

pip install numpy

Step #2: Detect Faces

Now, you must configure your camera and connect it to your system. The camera should work properly to avoid any issues in face detection.

Before our camera recognizes us, it first has to detect faces. We’ll use the Haar Cascade classifier for face detection. It is primarily an object detection method where you train a cascade function through negative and positive images, after which it becomes able to detect objects in other photos. 

In our case, we want our model to detect faces. OpenCV comes with a trainer and a detector, so using the Haar Cascade classifier is relatively more comfortable with this library. You can create your classifier to detect other images as well. 

Here’s the code:

import numpy as np

import cv2

faceCascade = cv2.CascadeClassifier(‘Cascades/haarcascade_frontalface_default.xml’)

cap = cv2.VideoCapture(0)

cap.set(3,640) # set Width

cap.set(4,480) # set Height

while True:

   ret, img =

   img = cv2.flip(img, -1)

   gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

   faces = faceCascade.detectMultiScale(




       minSize=(20, 20)


   for (x,y,w,h) in faces:


       roi_gray = gray[y:y+h, x:x+w]

       roi_color = img[y:y+h, x:x+w] 


   k = cv2.waitKey(30) & 0xff

   if k == 27: # press ‘ESC’ to quit




FYI: Free nlp course!

Also, Check out all Trending Python Tutorial Concepts in 2024.

Step #3: Gather Data

Now that your model can identify faces, you can train it so it would start recognizing whose face is in the picture. To do that, you must provide it with multiple photos of the faces you want it to remember. 

That’s why we’ll start with creating our dataset by gathering photos. After collecting the necessary images, add IDs for every person, so the model knows what face to associate with what ID. Start with the images of one person and add at least 10-20. Use different expressions to get the most effective results. 

Create a script for adding user IDs to images, so you don’t have to do it manually every time. The script is vital in case you want to use your model for multiple faces. 

Learn: TensorFlow Object Detection Tutorial For Beginners

In-demand Machine Learning Skills

Step #4: Train

After creating the dataset of the person’s images, you’d have to train the model. You’d feed the pictures to your OpenCV recognizer, and it will create a file named ‘trainer.yml’ in the end. 

In this stage, you only have to provide the model with images and their IDs so the model can get familiar with the ID of every image. After we finish training the model, we can test it. 

Here’s the code:

import cv2

import numpy as np

from PIL import Image

import os

# Path for face image database

path = ‘dataset’

recognizer = cv2.face.LBPHFaceRecognizer_create()

detector = cv2.CascadeClassifier(“haarcascade_frontalface_default.xml”);

# function to get the images and label data

def getImagesAndLabels(path):

   imagePaths = [os.path.join(path,f) for f in os.listdir(path)]    


   ids = []

   for imagePath in imagePaths:

       PIL_img =‘L’) # grayscale

       img_numpy = np.array(PIL_img,’uint8′)

       id = int(os.path.split(imagePath)[-1].split(“.”)[1])

       faces = detector.detectMultiScale(img_numpy)

       for (x,y,w,h) in faces:



   return faceSamples,ids

print (“\n [INFO] Training faces. It will take a few seconds. Wait …”)

faces,ids = getImagesAndLabels(path)

recognizer.train(faces, np.array(ids))

# Save the model into trainer/trainer.yml


# Print the number of faces trained and end program

print(“\n [INFO] {0} faces trained. Exiting Program”.format(len(np.unique(ids))))

Learn: MATLAB Application in Face Recognition: Code, Description & Syntax

Step#5: Start Recognition

Now that you have trained the model, we can start testing the model. In this section, we have added names to the IDs so the model can display the names of the respective users it recognizes. 

The model doesn’t recognize a person. It predicts whether the face it detects matches to the face present in its database. Our model displays a percentage of how much the face matches the face present in its database. Its accuracy will depend heavily on the image you’re testing and the pictures you’ve added to your database (the images you trained the model with). 

Here’s the code:

import cv2

import numpy as np

import os

recognizer = cv2.face.LBPHFaceRecognizer_create()‘trainer/trainer.yml’)

cascadePath = “haarcascade_frontalface_default.xml”

faceCascade = cv2.CascadeClassifier(cascadePath);


#initiate id counter

id = 0

# names related to ids: example ==> upGrad: id=1, etc

names = [‘None’, upGrad’, Me’, ‘Friend’, ‘Y’, ‘X’]

# Initialize and start realtime video capture

cam = cv2.VideoCapture(0)

cam.set(3, 640) # set video width

cam.set(4, 480) # set video height

# Define min window size to be recognized as a face

minW = 0.1*cam.get(3)

minH = 0.1*cam.get(4)

while True:

   ret, img

   img = cv2.flip(img, -1) # Flip vertically

   gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

   faces = faceCascade.detectMultiScale(


       scaleFactor = 1.2,

       minNeighbors = 5,

       minSize = (int(minW), int(minH)),


   for(x,y,w,h) in faces:

       cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)

       id, confidence = recognizer.predict(gray[y:y+h,x:x+w])      

       # If confidence is less than 100 ==> “0” : perfect match

       if (confidence < 100):

           id = names[id]

           confidence = ” {0}%”.format(round(100 – confidence))


           id = “unknown”

           confidence = ” {0}%”.format(round(100 – confidence))      




















   k = cv2.waitKey(10) & 0xff # Press ‘ESC’ for exiting video

   if k == 27:


# Do a cleanup

print(“\n [INFO] Exiting Program and doing cleanup”)



We have reached the end of our face detection project in Python. You now know how to create a machine learning model that detects and recognizes faces. Make sure to share your results with us!

Popular AI and ML Blogs & Free Courses

Learn More About Machine Learning 

We hope you liked this face detection project. If you want to make it more challenging, you can add multiple faces in your dataset and train your model accordingly. You can also combine it with other libraries and extend the project into something else, such as a face detection security system for a program! 

If you’re interested to learn more about machine learning, check out IIIT-B & upGrad’s Executive PG Programme in Machine Learning & AI which is designed for working professionals and offers 450+ hours of rigorous training, 30+ case studies & assignments, IIIT-B Alumni status, 5+ practical hands-on capstone projects & job assistance with top firms.

Which mathematical approach is used for face recognition?

Face recognition is computationally expensive and it is often used as accuracy test of machine learning algorithms and object detection methods. Generally, in most of the cases, the classical mathematical approach is followed - Euclidean distance. A geometric transformation is applied in order to find the closest Euclidean distance between the two sets. Euclidean distance requires adding up of a square of the difference between the two vectors of the points that represent the two images. More details about the Euclidean distance algorithm can be found from this research paper.

How does face detection work?

Face detection is the process of detecting a human face or multiple human faces in a digital image or video. Face detection is a sub-process of facial recognition, but the term typically refers to image-based face recognition where only the locations of faces in an image are used to identify or verify a person, while facial recognition also creates a model of their unique face, which is then matched to a target face. Face detection is different than face recognition in that face recognition is the automated process of identifying or verifying a person from a digital image or a video source.

What are the challenges of facial recognition?

Some challenges of facial recognition are discussed here. The algorithms involved in facial recognition systems are quite complex, which makes them highly inconsistent. The facial recognition systems are easily fooled by environmental and lighting changes, different poses, and even similar-looking people. Facial recognition systems require very high computational power, which is why facial recognition systems are mostly used with high-end smartphones and laptops. A facial recognition system might detect several false matches in a single frame. The software has to determine what the user intended to do, which is not an easy task for the software.

Want to share this article?

Leave a comment

Your email address will not be published. Required fields are marked *

Leave a comment

Your email address will not be published. Required fields are marked *

Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks