Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Face Detection Project in Python [In 5 Easy Steps]

Updated on 14 February, 2024

25.13K+ views
8 min read

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

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!

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.

Frequently Asked Questions (FAQs)

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

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

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