Python Face Recognition System – Full Tutorial

Facial recognition is a technology that uses the unique features of a person’s face, like the distance between their eyes or the shape of their jaw, to identify them in pictures or videos. It’s used in security systems and smartphones, among other things. The technology compares these features with a database of faces to find a match. We see facial recognition in security checks, cameras that follow faces, surveillance, and personalized suggestions.

Face Detection VS Face Recognition
When we talk about analyzing facial features, there are two main things that come in picture.

  1. Detecting the presence of face
  2. Recognizing who that face belongs to

Face detection is when a computer looks at an image or videos and draws the bounding box over it if there is a face in it. It does not care about who the face belongs to, just that it’s there.

Face recognition is when a computer looks at a face and tries to identify who that person is, based on features like the distance between their eyes and the shape of their face.

Let us take an example:

Required things:

  1. Python 3.7 (64-bit) and above
  2. Any python editor (VS code, Pycharm)
  3. Cmake and dlib setup

Set up for Cmake and dlib

1. Download cmake software from the link below and install it on your system.

2. Download visual studio from the link below and install it on your system. After installing the setup file select Desktop environment with C++ and click on install.

To install face_recognition run the command from cmd.

pip install face_recognition
import cv2

# Load the pre-trained Haar cascade face detector
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

# Start capturing video from the default camera
cap = cv2.VideoCapture(0)

while True:
    # Read a frame from the video stream
    ret, frame = cap.read()
    
    # Convert the frame to grayscale
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
    # Detect faces in the grayscale frame
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
    
    # Draw rectangles around the detected faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
    
    # Display the frame with detected faces
    cv2.imshow('Face Recognition', frame)
    
    # Check for the 'q' key to quit the program
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release the video capture object and close all windows
cap.release()
cv2.destroyAllWindows()

Output:

Explanation:

  1. Importing Libraries: The code starts by importing the OpenCV library (cv2).
  2. Loading the Face Detector: The cv2.CascadeClassifier is used to load a pre-trained Haar cascade classifier for face detection. This classifier is a machine learning model trained to detect faces in images.
  3. Capturing Video: The cv2.VideoCapture(0) command initializes the webcam (0 indicates the default camera). This creates a video capture object (cap) that can read frames from the webcam.
  4. Main Loop (Video Processing): The code enters a while loop that continuously reads frames from the webcam using cap.read(). Each frame is stored in the frame variable.
  5. Converting to Grayscale: The frame is converted to grayscale using cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY). This simplifies face detection as it only requires one channel of information (brightness).
  6. Detecting Faces: The face_cascade.detectMultiScale() method is used to detect faces in the grayscale frame. It takes the grayscale frame as input and returns a list of rectangles, each representing a detected face.
  7. Drawing Rectangles: For each detected face, a rectangle is drawn around it using cv2.rectangle(). The rectangle is drawn on the original color frame (frame) to show the detected face.
  8. Displaying the Frame: The cv2.imshow('Face Recognition', frame) command displays the frame with the detected faces in a window named “Face Recognition”.
  9. Exiting the Program: The code checks for the ‘q’ key (ord('q')) using cv2.waitKey(1) & 0xFF. If the ‘q’ key is pressed, the break statement exits the while loop, releasing the video capture object and closing all windows.

To add the functionality to recognize and label known faces, you can use the face_recognition library along with some modifications to to our above code. Here’s an example of how you can modify your code to achieve this:

import cv2
import face_recognition

# Load the pre-trained Haar cascade face detector
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

# Load known face images and their corresponding names
known_faces.append(face_recognition.load_image_file("C:/Users/Swarna Khushbu/Desktop/Coding/msd.jpg"))
known_names.append("Mahendra Singh Dhoni")

# Example: Add known faces (replace with your own images and names)
# known_faces.append(face_recognition.load_image_file("path/to/known_face1.jpg"))
# known_names.append("Person 1")

# Start capturing video from the default camera
cap = cv2.VideoCapture(0)

while True:
    # Read a frame from the video stream
    ret, frame = cap.read()

    # Convert the frame to grayscale
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Detect faces in the grayscale frame
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))

    for (x, y, w, h) in faces:
        # Draw rectangles around the detected faces
        cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)

        # Recognize faces using face_recognition
        face_encodings = face_recognition.face_encodings(frame, [(y, x + w, y + h, x)])
        for face_encoding in face_encodings:
            # Compare with known faces
            matches = face_recognition.compare_faces(known_faces, face_encoding)
            if True in matches:
                # Get the name of the recognized person
                name = known_names[matches.index(True)]
                # Display the name underneath the face
                cv2.putText(frame, name, (x, y + h + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)

    # Display the frame with detected faces
    cv2.imshow('Face Recognition', frame)

    # Check for the 'q' key to quit the program
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release the video capture object and close all windows
cap.release()
cv2.destroyAllWindows()

Explanation:

  1. import cv2: This imports the OpenCV library, which is used for computer vision tasks, including image and video processing.
  2. import face_recognition: This imports the face_recognition library, which provides face recognition capabilities built on top of OpenCV and dlib.
  3. face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'): This line loads a pre-trained Haar cascade classifier for face detection. Haar cascades are machine learning models used for object detection.
  4. known_faces.append(face_recognition.load_image_file("C:/Users/Swarna Khushbu/Desktop/Coding/msd.jpg")): This line loads an image file containing a known face (in this case, an image of Mahendra Singh Dhoni) and appends it to a list of known faces.
  5. known_names.append("Mahendra Singh Dhoni"): This line appends the name corresponding to the known face (Mahendra Singh Dhoni) to a list of known names.
  6. cap = cv2.VideoCapture(0): This line creates a video capture object (cap) to capture video from the default camera (index 0).
  7. The while True: loop is an infinite loop that continuously captures frames from the video stream and processes them for face detection and recognition.
  8. ret, frame = cap.read(): This line reads a frame from the video stream. ret is a boolean indicating if a frame was successfully read, and frame is the captured frame.
  9. gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY): This line converts the captured frame from color (BGR) to grayscale. Grayscale images are easier to process for face detection.
  10. faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)): This line detects faces in the grayscale frame using the Haar cascade classifier. It returns a list of rectangles representing the bounding boxes of the detected faces.
  11. The for (x, y, w, h) in faces: loop iterates over each detected face and draws a rectangle around it using cv2.rectangle().
  12. face_encodings = face_recognition.face_encodings(frame, [(y, x + w, y + h, x)]): This line computes the face encodings (a numerical representation of a face) for the detected face using the face_recognition library.
  13. matches = face_recognition.compare_faces(known_faces, face_encoding): This line compares the computed face encoding with the known face encodings to see if there is a match. It returns a list of boolean values indicating the matches.
  14. name = known_names[matches.index(True)]: This line retrieves the name of the recognized person from the list of known names based on the index of the match.
  15. cv2.putText(frame, name, (x, y + h + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2): This line adds the name of the recognized person as text underneath the detected face rectangle using cv2.putText().
  16. cv2.imshow('Face Recognition', frame): This line displays the frame with detected faces and recognized names in a window titled “Face Recognition”.
  17. if cv2.waitKey(1) & 0xFF == ord('q'):: This line checks if the ‘q’ key is pressed. If it is, the loop breaks, and the program exits.
  18. cap.release(): This releases the video capture object, freeing up the camera resources.
  19. cv2.destroyAllWindows(): This closes all OpenCV windows.

Conclusion,

For creating face recognition systems OpenCV is a useful tool. With high accuracy and reliability. This technology has many practical applications in security, surveillance, and biometric authentication. As computer vision and artificial intelligence continue to advance, face recognition technology is expected to become even more advanced. Developers can use OpenCV to create advanced face recognition applications that can identify people and detect their emotions and facial expressions.

Author

Sona Avatar

Written by

Leave a Reply

Trending

CodeMagnet

Your Magnetic Resource, For Coding Brilliance

Programming Languages

Web Development

Data Science and Visualization

Career Section

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4205364944170772"
     crossorigin="anonymous"></script>