Posted inFace Detection

Face Detection with 7 Lines of Code in Python

Face Detection with 7 Lines of Code in Python

I will show you how to create your own face detection program using Python with 7 lines of code

Only a few years ago, if someone asked me to work on a project that required detecting a person’s face using the camera, to be honest, I would have shut down my laptop and run as far away from that person as possible because, first I knew that face detection was possible but not accessible to implement without domain knowledge and second, even if, somehow, I was able to make it work, it would probably take me a couple of months just to create a program that could detect faces.

It is a great time for us developers now, with tons of resources, libraries and how-to guides available, teaching and providing a much simpler way for us, without in-depth domain knowledge, to implement face detection.

In this post, I will show you how to create your own face detection program using Python with 7 lines of code.

You will need to install the following if not already installed:

Python 3.7.3: For more information on downloading and installing refer to this link.

OpenCV: Install OpenCV if it is not already installed. You can either use apt install or pip3 install OpenCV.

or

tflite for Python 3.7: Please refer to the guide to this download the appropriate Python wheel for your system, and then install it with the pip3 install command.

Xailient FaceSDK: Please follow the instruction on this link to install the Xailient FaceSDK.

Here is the code to import the required python libraries, read an image from storage, perform face detection on the image and save the image with a bounding box around the detected faces in the same directory.

Here is the code to import the required python libraries, read an image from storage, perform face detection on the image and save the image with a bounding box around the detected faces in the same directory.

Below are the results of face detection using the above code.

You have your face detection program ready.


Looking to implement real-time face detection on a Raspberry Pi? Check out this post.