Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconArtificial Intelligencebreadcumb forward arrow iconFace Detection Project in Python [In 5 Easy Steps]

Face Detection Project in Python [In 5 Easy Steps]

Last updated:
1st Sep, 2020
Views
Read Time
8 Mins
share image icon
In this article
Chevron in toc
View All
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.

Ads of upGrad blog

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 = cap.read()

   img = cv2.flip(img, -1)

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

   faces = faceCascade.detectMultiScale(

       gray,    

       scaleFactor=1.2,

       minNeighbors=5,    

       minSize=(20, 20)

   )

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

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

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

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

   cv2.imshow(‘video’,img)

   k = cv2.waitKey(30) & 0xff

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

       break

cap.release()

cv2.destroyAllWindows()

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)]    

   faceSamples=[]

   ids = []

   for imagePath in imagePaths:

       PIL_img = Image.open(imagePath).convert(‘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:

           faceSamples.append(img_numpy[y:y+h,x:x+w])

           ids.append(id)

   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

recognizer.write(‘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()

recognizer.read(‘trainer/trainer.yml’)

cascadePath = “haarcascade_frontalface_default.xml”

faceCascade = cv2.CascadeClassifier(cascadePath);

font = cv2.FONT_HERSHEY_SIMPLEX

#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 =cam.read()

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

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

   faces = faceCascade.detectMultiScale(

       gray,

       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))

       else:

           id = “unknown”

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

       cv2.putText(

                   img,

                   str(id),

                   (x+5,y-5),

                   font,

                   1,

                   (255,255,255),

                   2

                  )

       cv2.putText(

                   img,

                   str(confidence),

                   (x+5,y+h-5),

                   font,

                   1,

                   (255,255,0),

                   1

                  )   

   cv2.imshow(‘camera’,img)

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

   if k == 27:

       break

# Do a cleanup

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

cam.release()

cv2.destroyAllWindows()

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!

Ads of upGrad blog

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.

Profile

Pavan Vadapalli

Blog Author
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and long term technology strategy.
Get Free Consultation

Select Coursecaret down icon
Selectcaret down icon
By clicking 'Submit' you Agree to  
UpGrad's Terms & Conditions

Our Popular Machine Learning Course

Frequently Asked Questions (FAQs)

1Which 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.

2How 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.

3What 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.

Explore Free Courses

Suggested Blogs

Artificial Intelligence course fees
5436
Artificial intelligence (AI) was one of the most used words in 2023, which emphasizes how important and widespread this technology has become. If you
Read More

by venkatesh Rajanala

29 Feb 2024

Artificial Intelligence in Banking 2024: Examples &#038; Challenges
6173
Introduction Millennials and their changing preferences have led to a wide-scale disruption of daily processes in many industries and a simultaneous g
Read More

by Pavan Vadapalli

27 Feb 2024

Top 9 Python Libraries for Machine Learning in 2024
75629
Machine learning is the most algorithm-intense field in computer science. Gone are those days when people had to code all algorithms for machine learn
Read More

by upGrad

19 Feb 2024

Top 15 IoT Interview Questions &#038; Answers 2024 – For Beginners &#038; Experienced
64466
These days, the minute you indulge in any technology-oriented discussion, interview questions on cloud computing come up in some form or the other. Th
Read More

by Kechit Goyal

19 Feb 2024

Data Preprocessing in Machine Learning: 7 Easy Steps To Follow
152952
Summary: In this article, you will learn about data preprocessing in Machine Learning: 7 easy steps to follow. Acquire the dataset Import all the cr
Read More

by Kechit Goyal

18 Feb 2024

Artificial Intelligence Salary in India [For Beginners &#038; Experienced] in 2024
908753
Artificial Intelligence (AI) has been one of the hottest buzzwords in the tech sphere for quite some time now. As Data Science is advancing, both AI a
Read More

by upGrad

18 Feb 2024

24 Exciting IoT Project Ideas &#038; Topics For Beginners 2024 [Latest]
760303
Summary: In this article, you will learn the 24 Exciting IoT Project Ideas & Topics. Take a glimpse at the project ideas listed below. Smart Agr
Read More

by Kechit Goyal

18 Feb 2024

Natural Language Processing (NLP) Projects &amp; Topics For Beginners [2023]
107732
What are Natural Language Processing Projects? NLP project ideas advanced encompass various applications and research areas that leverage computation
Read More

by Pavan Vadapalli

17 Feb 2024

45+ Interesting Machine Learning Project Ideas For Beginners [2024]
328345
Summary: In this Article, you will learn Stock Prices Predictor Sports Predictor Develop A Sentiment Analyzer Enhance Healthcare Prepare ML Algorith
Read More

by Jaideep Khare

16 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon