Face Detection Python in 5 Minutes

by Augmented Startups in Circuits > Software

855 Views, 3 Favorites, 0 Comments

Face Detection Python in 5 Minutes

Face Detection Python in 5 minutes
image 1.png
image 2.png

A few weeks back, I collaborated on a Digital Invisibility Shield project for Jake. Who's from the popular YouTube Channel JLASERVIDEO. So this project required us to match the perspective of the viewer to the projected scene so that it gives the perception of invisibility. One method to solve this perspective mapping problem was by using Computer Vision to perform face detection.

It was a pretty complex project, but what's not so complex is Face Detection which is the topic of this video. We’re gonna be talking about:

  • What it is?
  • How it works? and
  • How to implement 70+ frames per second FaceDetection on a CPU?

All in under 5 minutes.

Face Detection

image 3.png

First up, what is Face Detection? Well, it's quite intuitive just detecting faces, right? Well, the more formal definition is that Face detection is a technology that enables computers to identify human faces in digital images. It is a type of object-class detection in which the task is to find the locations and sizes of all objects in an image that belongs to a given class, in this case, it is faces.

So the reason why you'd be interested in using face detection is as we mentioned earlier, is to:

  1. Find out how many faces you have in an image.
  2. The size of the face, which can also help us to determine how far the face is from the camera, and
  3. The location of the face within the image.

Manipulating objects both physical and/or virtual, like in the case of the invisibility shield, is just one of many applications of face detection.

Face Detection Vs Facial Recognition

image 5.png

Face Detection is not to be confused with Facial Recognition, which is the process of identifying each individual rather than all faces just being the same. We'll cover recognition in a separate article.

Deep Learning

image 6.png

Moving on, there’s a massive amount of ways in which you can implement face detection. Before Deep Learning, face detection used to be done using Haar Cascades. Eventually, it evolved using Machine Learning to do feature extraction with the Histogram of Oriented Gradients, also known as HOG and this was used in the Dlib Frontal Face Detector. Currently, face detection can be implemented with a Deep Learning Neural Network or DNN using a ResNet-10 Architecture.

BlazeFace

image 7.png

In our implementation, we’ll be using BlazeFace, which is an ultra-fast face detection model that is both lightweight and accurate.

MediaPipe

image 8.png

In this tutorial, we will be using the MediaPipe implementation of BlazeFace to achieve over 70+ frames per second on a 720P video and on a CPU! That’s right! No need for expensive CUDA-enabled GPUs.

Vision Store

image 10.png
image 11.png

To enable this go over to the Vision Store and click on Face Detection, ensure that you are logged in otherwise you can sign up.

Requirements

image 12.png

Ensure that you have all of the requirements, in this case: any PC, a webcam, as well as Python Installed along with Pycharm Community Development Environment.

Install PyCharm Community

image 13.png

Step one, once PyCharm Community has been installed - create a new project, and let's call it FaceDetection.py.

You will be required to install only 2 dependencies:

  1. OpenCV
  2. Media Pipe.

It's highly recommended to use the exact same versions mentioned here for compatibility with the code.

The Code

image 14.png

And then lastly you download the code, or you can copy and paste it here from the Vision Model Page.

Run the Code

image 15.png

To run the code on a video, you just need to click the play button.

To run this on a webcam, you can simply change the name of the file here to zero and you'll be able to detect faces using your webcam.

How It Works?

image 16.png

Let's delve into the code to understand how it works on a high level. In the first part, we import our libraries especially OpenCV and MediaPipe. Next, we initiate our webcam or video and then establish our face detection threshold which adjusts the sensitivity of our detector. Then, we change our color space BGR to RGB. Which also stands for Red, Green, and Blue. Lastly, we search through all our detections and draw a bounding box over the faces before displaying all of the information to a window. Simple, right? I’ll show you how to modify this code in the future for your own applications.

Conclusion

image 17.png

For now, if you enjoyed this 5 minute tutorial in Computer Vision then check out more models on our Vision Store - only on https://www.augmentedstartups.com/ .

The vision store is where you can download and implement projects without wasting too much time and it’s meant for rapid prototyping and quicker time to market. Otherwise, check out our comprehensive courses in Computer Vision, AI, and Robotics.