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. 

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

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. 

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: TensorFlow Object Detection Tutorial For Beginners

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 PG Diploma 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.

Prepare for a Career of the Future

PG DIPLOMA FROM IIIT-B, 100+ HRS OF CLASSROOM LEARNING, 400+ HRS OF ONLINE LEARNING & 360 DEGREES CAREER SUPPORT
Enroll Today

Leave a comment

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

Our Popular Machine Learning Course

Accelerate Your Career with upGrad

×