This tutorial will introduce you to the concept of object detection in Python using OpenCV library and how you can utilize it to perform tasks like Facial detection.
TOPICS
What is Computer Vision?
Introduction to OpenCV
OpenCV allows developers and non-mathematicians to build computer vision applications easily without having to code them from scratch. The library has over 2,500 algorithms that allow users to perform tasks like face recognition and object detection.
Developers and data practitioners at well-established organizations like Google, Microsoft, IBM, and Intel make extensive use of the OpenCV library, which is currently free for commercial use.
In this article, we will use OpenCV to perform face detection in Python.
By the end of this tutorial, you will know how to:
- Detect human faces in images with OpenCV in Python
- Perform real-time face detection in a live stream from a webcam
- Recognize and label celebrity faces in images
What is Face Detection?
OpenCV for Face Detection Tutorial
In this section, we will learn to apply a popular face detection approach called Haar Cascade for face detection using OpenCV and Python.
Intro to Haar Cascade Classifiers
This method was first introduced in the paper Rapid Object Detection Using a Boosted Cascade of Simple Features, written by Paul Viola and Michael Jones.
The idea behind this technique involves using a cascade of classifiers to detect different features in an image. These classifiers are then combined into one strong classifier that can accurately distinguish between samples that contain a human face from those that don’t.
The Haar Cascade classifier that is built into OpenCV has already been trained on a large dataset of human faces, so no further training is required. We just need to load the classifier from the library and use it to perform face detection on an input image.
Installing OpenCV for Python
To install the OpenCV library, simply open your command prompt or terminal window and run the following command:
OpenCV for Face Detection in Images
We will build a detector to identify the human face in a photo from Unsplash. Make sure to save the picture to your working directory and rename it to input_image
before coding along.
Step 1: Import the OpenCV Package
Now, let’s import OpenCV and enter the input image path with the following lines of code:
This will load the image from the specified file path and return it in the form of a Numpy array.
Let’s print the dimensions of this array:
Notice that this is a 3-dimensional array. The array’s values represent the picture’s height, width, and channels respectively. Since this is a color image, there are three channels used to depict it - blue, green, and red (BGR).
Note that while the conventional sequence used to represent images is RGB (Red, Blue, Green), the OpenCV library uses the opposite layout (Blue, Green, Red)
Step 3: Convert the Image to Grayscale
To improve computational efficiency, we first need to convert this image to grayscale before performing face detection on it:
Step 4: Load the Classifier
Let’s load the pre-trained Haar Cascade classifier that is built into OpenCV:
Notice that we are using a file called haarcascade_frontalface_default.xml
. This classifier is designed specifically for detecting frontal faces in visual input.
OpenCV also provides other pre-trained models to detect different objects within an image - such as a person’s eyes, smile, upper body, and even a vehicle’s license plate. You can learn more about the different classifiers built into OpenCV by examining the library’s GitHub repository.
Step 5: Perform the Face Detection
We can now perform face detection on the grayscale image using the classifier we just loaded:
Step 6: Drawing a Bounding Box
Now that the model has detected the faces within the image, let’s run the following lines of code to create a bounding box around these faces:
The face
variable is an array with four values: the x and y axis in which the faces were detected, and their width and height. The above code iterates over the identified faces and creates a bounding box that spans across these measurements.
The parameter 0,255,0
represents the color of the bounding box, which is green, and 4
indicates its thickness.
Step 7: Displaying the Image
To display the image with the detected faces, we first need to convert the image from the BGR format to RGB:
Great!
The model has successfully detected the human face in this image and created a bounding box around it.
Related Reading
- OpenCV – Face Detection using Haar Cascades
- Face Recognition with Python
- DataScienceGo – OpenCV Face Detection
In today’s blog post you discovered a little known secret about the OpenCV library — OpenCV ships out-of-the-box with a more accurate face detector (as compared to OpenCV’s Haar cascades).
The more accurate OpenCV face detector is deep learning based, and in particular, utilizes the Single Shot Detector (SSD) framework with ResNet as the base network.
Thanks to the hard work of Aleksandr Rybnikov and the other contributors to OpenCV’s
No comments:
Post a Comment