Computer Vision: Face Detection Using OpenCV Haar Cascade Classifier

Search for a command to run...

No comments yet. Be the first to comment.
In this article, we explored how to deploy a graph database on AWS using Amazon Neptune, how to load some data into the database, and how to query those data. The graph data model is rapidly becoming the ideal way for modeling datasets that are highl...

Using SQL to predict telecoms customer churn based on customer activity data (95+% accuracy).

Analyzing data in real-time gives us the opportunity to derive important insights from data and respond to events as they occur. More than any other topic, COVID-19 was the most discussed topic globally in 2020. Since the beginning of the outbreak,...

FastAPI is a modern and fast Python web framework for building backend applications. FastAPI comes with support for API documentation powered by Swagger, security modules, and type checking to ensure correctness in code. Why choose FastAPI over othe...

Apache Airflow is an open-source workflow automation tool that can be used to programmatically author, schedule, and monitor data processing pipelines. Airflow originated at Airbnb in 2014 and became a top-level open-source project under Apache Soft...

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in commercial products.
The library has more than 2500 optimized algorithms, which includes a comprehensive set of both classic and state-of-the-art computer vision and machine learning algorithms. These algorithms can be used to detect and recognize faces, identify objects, classify human actions in videos, track camera movements, track moving objects, and much more.
In this OpenCV tutorial, you will learn how to use OpenCV & Python for object detection and face recognition.
Anaconda is a free and open-source distribution of the Python & R programming languages that aims to simplify package management and deployment for scientific purposes. Anaconda is the easiest and fastest way to do data science and machine learning on Linux, Windows, and Mac OS X.
To get started setting-up Anaconda on your machine, head over to the Anaconda download page to download the latest version of Anaconda. Anaconda installs a few programs on your computer when you run the installer. These include the Anaconda Navigator, Anaconda Cloud, Jupyter Notebook, Spyder, and the Anaconda Prompt.
Open up Anaconda Navigator, go to the “Environments Tab” as shown in the below image. Search for OpenCV library and click apply to install. OpenCV and all required dependencies will be installed.

Face detection is a computer technology that determines the locations and sizes of human faces in images. It detects facial features and ignores anything else, such as buildings, trees, and bodies.
Face Detection using Haar feature-based cascade classifiers is an effective object detection method proposed by Paul Viola and Michael Jones. It is a machine learning-based approach where a cascade function is trained from a lot of positive and negative images. It is then used to detect objects in other images.
Our goal for this exercise is to be able to detect the faces in an image. OpenCV comes with many pre-trained classifiers for face, eyes, smiles, etc. Those classifiers are stored in an XML file format and can be found in the “opencv/data/haarcascades/” folder or click here to download.
First, we need to load the required Python modules into our work environment. To ensure that opencv module is loaded correctly, its version was printed below.
## opencv python modules
import cv2
from matplotlib import pyplot as plt
print ("Version of OpenCV: {}".format(cv2.__version__))
For this exercise, we will be using a sample image of different faces. We will load this image into our work environment using the opencv “imread” function. Once the image has been loaded, we will use Matplotlib (a 2D plotting library for the Python programming language.) to visualize the image.

## load image using opencv
image = cv2.imread("images/faces2.jpg", 1)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
## display image using matplotlib
plt.imshow(image),plt.title('Faces Detection.')
At this stage, we will use the default classifier library that came with opencv to create a face cascade that will be applied on our sample image.
### cascade classifier
lib_path = "xml/haarcascade_frontalface_default.xml"
face_cascade = cv2.CascadeClassifier(lib_path)
### match the classifier with the faces image
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.07, minNeighbors=5, minSize=(40,40))
### output number of faces found
print ("Found {0} faces!".format(len(faces)))
## draw boundary boxes unto detected faces.
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
## display image using matplotlib
plt.imshow(image),plt.title('Faces Detection.')
