• Sabina Pokhrel

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 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.



gif


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 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.


sudo apt install -y python3-opencv


or


pip3 install opencv-python


tflite for Python 3.7: Please refer to 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 a few results of face detection using the above code.






You have your face detection program ready.


Found this post helpful? Leave your thoughts as comments below.


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


More stories:

Integrate Face Detection on your App

Challenges of Running Deep Learning Computer Vision on Computationally Limited Devices

Struggles of Running Object Detection on a Raspberry Pi

You Need to Move from Cloud Computing to Edge Computing Now!

Australia:

11 York Street, Level 8, Sydney, NSW 2000

Tel: +61 434 965 010

USA:
440 N Wolfe Rd, Sunnyvale, CA 94085

8 The Green Suite 6970, Dover, DE 19901

Tel: +1(310)359-8357