Face Detection with 7 Lines of Code in Python

This blog post was originally published at Xailient’s website. It is reprinted here with the permission of Xailient.

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.

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.

Sabina Pokhrel
Customer Success AI Engineer, Xailient

Here you’ll find a wealth of practical technical insights and expert advice to help you bring AI and visual intelligence into your products without flying blind.

Contact

Address

Berkeley Design Technology, Inc.
PO Box #4446
Walnut Creek, CA 94596

Phone
Phone: +1 (925) 954-1411
Scroll to Top