Site icon TechVidvan

Real-Time Face Detection & Recognition using OpenCV

face detection recognition opencv python

Nowadays face detection is a very common problem. Face detection is also called facial detection. It is a computer vision technology used to find and identify human faces in digital images. Face detection technology can be applied to various fields such as security, surveillance, biometrics, law enforcement, entertainment, etc.

Today we’ll build a Face Detection and face recognition project using Python OpenCV and face_recognition library in python. Face_recognition library uses on dlib in the backend.

What is OpenCV?

OpenCV is a real-time Computer Vision framework. It is used in many image processing and computer vision tasks. OpenCV is written in C/C++, but we can use it in python also using opencv-python.

What is dlib?

Dlib is a Open Source C++ toolkit. It contains various machine learning algorithms and tools for creating complex software. Dlib used to solve real-world problems. It is useful in industry and academia including robotics, embedded devices, mobile phones, and large high-performance computing environments.

How does Dlib work in facial recognition?

At first, it detects faces from the input image and then generates 68 landmarks of faces – The outside of the eyes, nose, top chin, etc.

Source: dlib

And using those landmarks it rotates the face to the center position. In this way no matter how much the face is tilted, it is able to center position the detected face.

After correction, the corrected face is fed into a CNN (Convolution Neural Network) and generates 128 points. And using those 128 points later it compares with another face and recognizes that the detected face is same or not.

So let’s build this opencv project

Prerequisites for OpenCV Face Recognition Project:

1. Microsoft Visual Studio 2019
You’ll need Visual Studio C++ for compiling dlib during face-recognition python package installation.

Install the Desktop development with c++ package.

2. Python – 3.x ( We used python 3.7.10 for this project
3. OpenCV – 4.5

4. Face-recognition

5. Numpy – 1.20

Download Face Recognition OpenCV Python Code

Please download the source code of python face detection & recognition project: Face Detection & Recognition OpenCV Project Code

Steps to solve the project:

We’ll write two different programs for this OpenCV face recognition project. The first one will be for capturing training images, and the 2nd one will be for detecting and recognizing the face.

Steps for the First Program:

Step 1 – Import necessary packages and read video from webcam:

import cv2

# Take input of the person name
name = input("Enter name:  ")

# Create the videocapture object
cap = cv2.VideoCapture(0)

while True:
    # Read each frame
    success, frame = cap.read()
    # Show the output
    cv2.imshow("Frame", frame)

Note – If you don’t wanna use your webcam then you can directly drag and drop any image in the faces folder.

Step 2 – Capture a training image and save it in a local folder:

# If 'c' key is pressed then click picture
   	 if cv2.waitKey(1) == ord('c'):
        		filename = 'faces/'+name+'.jpg'
        		cv2.imwrite(filename, frame)
    print("Image Saved- ",filename)

Output

Steps for the 2nd Program:

Step 1 – Import necessary packages and reading the train images:

import cv2
import numpy as np
import face_recognition
import os

# Define the path for training images for OpenCV face recognition Project

path = 'faces'

images = []
classNames = []

Output:

[‘sourav’]

Step 2 – Encode faces from the train images:

# Function for Find the encoded data of the input image
def findEncodings(images):
    encodeList = []
    for img in images:
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        encode = face_recognition.face_encodings(img)[0]
        print(encode)
        encodeList.append(encode)
    return encodeList

# Find encodings of training images

knownEncodes = findEncodings(images)
print('Encoding Complete')

Step 3 – Detect and encode faces from the webcam:

scale = 0.25    
box_multiplier = 1/scale


# Define a videocapture object
cap = cv2.VideoCapture(0)
 
while True:
    success, img = cap.read()  # Reading Each frame
    
   # Resize the frame
    Current_image = cv2.resize(img,(0,0),None,scale,scale)
    Current_image = cv2.cvtColor(Current_image, cv2.COLOR_BGR2RGB)

    # Find the face location and encodings for the current frame
    
    face_locations = face_recognition.face_locations(Current_image,  model='cnn')
    face_encodes = face_recognition.face_encodings(Current_image,face_locations)

Note – Change this scale according to your need between 0 and 1. A lower number will give better performance but it will not be able to detect faces if the face is small in the image, and a greater number can detect small faces in the image but the performance will be slow

Step 4 – Find the matches between the detected faces and the Training images face:

 for encodeFace,faceLocation in zip(face_encodes,face_locations):
        matches = face_recognition.compare_faces(knownEncodes,encodeFace, 
tolerance=0.6)
        faceDis = face_recognition.face_distance(knownEncodes,encodeFace)
        matchIndex = np.argmin(faceDis)

 
        # If match found then get the class name for the corresponding match

        if matches[matchIndex]:
            name = classNames[matchIndex].upper()

        else:
            name = 'Unknown'

Step 5 – Draw the detection and show the identity of the person:

y1,x2,y2,x1=faceLocation
y1,x2,y2,x1=int(y1*box_multiplier),int(x2*box_multiplier),int(y2*box_multiplier),
int(x1*box_multiplier)

# Draw rectangle around detected face

cv2.rectangle(img,(x1,y1),(x2,y2),(0,255,0),2)
cv2.rectangle(img,(x1,y2-20),(x2,y2),(0,255,0),cv2.FILLED)
      cv2.putText(img,name,(x1+6,y2-6),cv2.FONT_HERSHEY_COMPLEX,0.5,(255,255,255)
,2)

OpenCV Face Detection & Recognition Output

Summary

In this project, we built a face detection and recognition system using python OpenCV. We used the face_recognition library to perform all the tasks. We’ve learned about how the face detection system works and how the face recognition system works through this project.

Exit mobile version