How to Create a Face Detection App with OpenCV and Python

In this tutorial, you will learn how to create a face detection app using the OpenCV library and Python. Face detection is a common use case in computer vision applications, and OpenCV provides a pre-trained Haar cascade classifier that can be used for this purpose.

Prerequisites

To follow along with this tutorial, you will need:

  • Python 3 installed on your machine
  • Basic knowledge of Python programming
  • Familiarity with the command line

Step 1: Install OpenCV

The first step is to install the OpenCV library. You can install it using pip by running the following command:

pip install opencv-python

This will install the OpenCV library on your machine.

Step 2: Import the necessary libraries

Open a new Python script and import the necessary libraries:

import cv2

Step 3: Load the pre-trained classifier

Next, download the pre-trained classifier from the OpenCV GitHub repository. To do this, open your web browser and navigate to the following link:

https://github.com/opencv/opencv/tree/master/data/haarcascades

Download the file named haarcascade_frontalface_default.xml and save it in the same directory as your Python script.

Now, load the classifier in your Python script:

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

Step 4: Capture video from the webcam

To detect faces in real-time, you will need to capture video from your webcam. OpenCV provides a VideoCapture class that can be used to capture video from various sources. In this case, we will use the default webcam.

video_capture = cv2.VideoCapture(0)

Step 5: Detect faces in the video frames

Use a loop to continuously capture video frames from the webcam and detect faces in each frame. To do this, you need to perform the following steps:

  • Read a frame from the video capture
  • Convert the frame to grayscale
  • Detect faces in the grayscale frame using the pre-trained classifier
  • Draw rectangles around the detected faces

Here’s the complete code for this step:

while True:
    # Read a frame from the video capture
    ret, frame = video_capture.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), (0, 255, 0), 2)

    # Display the resulting frame
    cv2.imshow('Video', frame)

    # Break the loop if the 'q' key is pressed
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

Step 6: Release the video capture and close the windows

Finally, release the video capture and close the windows after you are done:

video_capture.release()
cv2.destroyAllWindows()

Step 7: Run the application

Save your Python script and run it from the command line:

python face_detection_app.py

A new window will open, showing the video stream from your webcam with rectangles drawn around the detected faces. Press the ‘q’ key to exit the application.

Conclusion

In this tutorial, you learned how to create a face detection app using the OpenCV library and Python. You loaded a pre-trained Haar cascade classifier, captured video from the webcam, detected faces in the video frames, and drew rectangles around the detected faces. This app can be further extended and customized to suit your specific needs in computer vision applications. Happy coding!

Related Post