Detect faces with Python

Have you ever wondered how Snapchat accurately applies different filters to your face? Does your smartphone perform magic when unlocked with your facial features? No, you’re just watching Face Detection in action.


Face detection is an artificial intelligence technology that can identify human faces in a digital image or video. Find out how the legendary Haar Cascade algorithm allows you to build a real-time face detection program in less than 25 lines of code.


What is the hair cascade?

Hair Cascade is an object detection algorithm introduced by Paul Viola and Michael Jones to detect faces in images or videos. A cascade function is trained using many positive and negative images that can later be used to identify any object or face in other media. These trained files are available in the OpenCV GitHub repository.

Using a sliding window approach, a fixed size window iterates over an image from left to right, top to bottom. At each stage, the window stops and classifies whether the area contains a face or not.

OpenCV, a computer vision tool, works with a pre-trained Haar Cascade model to classify its features. Each stage controls five attributes: two edge attributes, two line elements, and one four-rectangle element.

When observed, the eye area appears darker than the cheek area, while the nose area appears brighter than the eye area. You can visualize these functions in the following way.

Using these functions and the calculation of pixels, the algorithm identifies more than 100,000 data points. You can then use an Adaboost algorithm to improve accuracy and ignore irrelevant features. Over many iterations, this approach minimizes the error rate and increases the feature weight until it reaches an acceptable accuracy.

However, the sliding window technique stops if a particular test case fails and it is computationally expensive. To solve this you can apply the concept of the Cascade of Classifiers. Rather than applying all the features in a single window, this approach groups and applies them in stages.

See also  8 Ways to Fix iPhone Voicemail Not Working

If the window does not pass the first stage, the process will delete it, otherwise it will continue. This drastically reduces the number of operations to be performed and makes it viable to use for real-time applications.

Workflow of face detection

Follow this algorithm to build your face detection program:

  1. Load the Hair Cascade Frontal Face algorithm.
  2. Initialize the camera.
  3. Read frames from the camera.
  4. Convert color images to grayscale.
  5. Get the face coordinates.
  6. Draw a rectangle and place the correct message.
  7. Display the output.

What is OpenCV?

OpenCV is an open-source computer vision and machine learning library. It has more than 2500 optimized algorithms for different applications. These include face/object detection, recognition, classification and much more.

Privacy of your facial data is a separate concern. Hundreds of leading companies such as Google, IBM and Yahoo use OpenCV in their applications. Some people who want to keep their data private have shown that there are ways to avoid facial recognition.

To install OpenCV in Python, use the command:

pip install opencv-python

How to build a face detection program with Python

Follow these steps to build the face detector:

This sample code, along with the Haar Cascade algorithm file, is available in a GitHub repository and is free for you to use under the MIT license.

  1. Download the Hair Cascade Frontal Face Standard XML file and place it in the same location as your Python program.
  2. Import the OpenCV library.

    import cv2
  3. Save the Hair Cascade Frontal Face algorithm file for easy reference.

    alg = "haarcascade_frontalface_default.xml"
  4. Use the CascadeClassifier class to load an XML file into OpenCV.

    haar_cascade = cv2.CascadeClassifier(alg)
  5. Capture the video from the camera. Pass 0 to the Video recording() function to use your primary camera. If you have an external camera connected, you can use sequential numbers 1, 2, etc. to use it instead.

    cam = cv2.VideoCapture(0)
  6. Set an infinite loop to read the camera input frame by frame. The read() function returns two parameters. The first value is of type boolean to indicate whether the operation was successful or not. The second parameter contains the actual frame you will be working with. Save this frame in the img variable.
    while True:
    _, img = cam.read()
  7. Set the default text to appear as Face not detected. Update the value of this variable when it is detected.
        text = "Face not detected"
  8. The real world input is colorful, in the BGR format. BGR stands for blue, green and red. This provides a lot of processing for computer vision applications. Use a grayscale format to reduce this.
        
    grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    Pass the frame and conversion code format, COLOR_BGR2GRAYuntil cvtColor() to change each frame of the video from color to grayscale.

  9. To use detectMultiScale() detect faces. This method takes three parameters as input. The first is the source image, grayImg. The second parameter is the scaleFactor. This tells you how much you should reduce the image size at each image scale. Use the default value of 1.3 as the scale factor. The higher the scale factor, the fewer steps and the faster the execution. However, there is also a higher chance of missing faces. The third parameter is minNeighbours. This specifies how many neighbors each candidate rectangle should have in order to keep it. The higher the value, the smaller the chance of a false positive, but it also means you’ll miss obscure facial trails.
        
    face = haar_cascade.detectMultiScale(grayImg, 1.3, 4)
  10. When you detect a face, you get four coordinates. x represents the x coordinate, y represents the y coordinate, w represents the width, and h represents the height. Update the text to Face Detected and draw a rectangle with these coordinates. The color of the rectangle is green (BGR) format with a thickness of two pixels.
        
    for (x, y, w, h) in face:
    text = "Face Detected"
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
  11. If desired, print the text on the output console. Display the text on the screen using the captured frame as the source, text as obtained in the above text, font style of FONT_HERSHEY_SIMPLEX, font scale factor of 1, blue color, thickness of two pixels, and line type of AA.
        
    print(text)
    image = cv2.putText(img, text, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv2.LINE_AA)
  12. Display the window with the title Face recognition and the image. Use the waitkey() method to display the window for 10 milliseconds and check for a keystroke. When a user presses the esc key (ASCII value 27), exit the loop.
        
    cv2.imshow("Face Detection", image)
    key = cv2.waitKey(10)

    if key == 27:
    break

  13. Finally, release the camera object from the python program and close all windows.
    cam.release()
    cv2.destroyAllWindows()

Face Detection with Python in Action

When a face is in view, you should see the output like this:

If no face is present, you will see a message like this:

Limitations of Face Detection Using the Hair Cascade Algorithm

While this algorithm is lightweight, has a small model, and works fast, there are some limitations:

  • In a real-time video, the face must be in the camera’s line of sight. If the face is too far, too close, or too tilted, the algorithm will not be able to pick up the features.
  • It’s a frontal face algorithm, so you can’t detect side views.
  • High false positive results. It often detects areas as faces even though no face is present.
  • Must have optimal lighting conditions. Excessive or weak lighting hinders the accuracy of the algorithm.

The many uses of face detection

Face detection has a wide variety of uses in today’s world. You can use it for facial recognition in smartphones, homes, vehicles and immigration checkpoints. Face detection is already common in CCTV, social media filters and automatic facial recognition in cinematography.

It is just the beginning of this wonderful technology. With progress, we may be able to identify missing children, catch criminals and prevent crimes such as identity theft.

See also  8 Conditions You Should Know When Buying Computer RAM

Leave a Reply

Your email address will not be published.