---
# System prepended metadata

title: Face Detection Using OpenCV And Python

---

# Face Detection Using OpenCV And Python

:::section{.abstract}
## Overview
**Face detection** is an essential task in Computer Vision that involves detecting human faces in images or videos. **OpenCV python face detection** has become the easiest way to detect faces from images or videos. It has many practical applications, including **face recognition**, **emotion analysis**, and **video surveillance**. OpenCV and Python are two popular tools for performing this task. 

:::
:::section{.scope}
## What are we building?

In this blog, we will learn how to perform **face detection using OpenCV** to detect faces in images and videos using Haar cascades. Face detection using OpenCV is a popular topic in computer vision and machine learning. **Haar cascades are classifiers** that are trained to detect objects in images using machine learning techniques. They work by **analyzing patterns** in the image data and **classifying the patterns** as objects of interest or background noise.

### Pre-requisites
A basic understanding of computer vision and a fundamental view of what is OpenCV are needed to do this task. To know more about Computer Vision, read this [article](https://www.scaler.com/topics/deep-learning/what-is-computer-vision/). 

OpenCV is an **open-source computer vision library** that provides tools for image and video processing. It has a wide range of features and functions that make it popular among computer vision researchers and developers.

**Face detection**
* Face detection using OpenCV technique that involves **identifying the location** and **size of faces** in an image or video stream.
* The most common method for face detection is to use Haar cascades, which are pre-trained classifiers that use machine learning algorithms to detect facial features such as **eyes, nose, and mouth**.
* Once a face is detected, it can be extracted and processed further, for example, to **recognize the person** or **analyze their emotional state**.

**Face recognition**
* Face recognition is a more advanced computer vision technique that involves identifying and verifying the identity of a person based on their **facial features**.
* Face recognition typically involves training a machine learning model on a **dataset of labeled faces**, so that it can learn to **distinguish between different individuals**.
* The most common method for face recognition is to use **deep learning** models such as Convolutional Neural Networks (CNNs), which can learn highly discriminative features from face images.
* Face recognition can be used for various tasks, such as **access control, surveillance, personalization, and entertainment**.

**Haar Cascades - Complete Overview**

* The basic idea behind Haar cascades is to use a set of Haar-like features to identify regions of an image that are likely to contain the object of interest. **Haar-like features** are simple rectangular features that can be calculated for each region of an image by subtracting the sum of pixel values in one part of the rectangle from the sum of pixel values in another part of the rectangle. These features can capture variations in **brightness, contrast, and edges in the image**.

* The Haar cascades classifier is built by training a machine learning algorithm, such as AdaBoost or **Support Vector Machines (SVM), on a large set of positive and negative images**. Positive images contain the object of interest (such as faces), while negative images do not. During training, the algorithm learns to distinguish between positive and negative images based on the Haar-like features.

[IMAGE {1} {Mechanism of Haarcascades} START SAMPLE]
![Mechanism of Haarcascades](https://hackmd.io/_uploads/B1VQs47Zn.png)
[IMAGE {1} FINISH SAMPLE]



* Once trained, the Haar cascades classifier can be applied to new images or video frames to detect the object of interest. The classifier works by applying a sliding window over the image and evaluating the Haar-like features within each window. The resulting features are fed into the machine learning algorithm, which outputs a **confidence score** indicating whether the window contains the object of interest.

* One advantage of the Haar cascades approach is that it is relatively fast and can be implemented in real-time applications. However, it may not be as accurate as other methods such as deep learning approaches, particularly for complex objects with varied appearances. Additionally, training the **Haar cascades classifier can be time-consuming** and requires a large dataset of **positive and negative images.**

### How are we going to build this?
To perform face detection using OpenCV and Haar cascades, you can follow these general steps:

* **Import the required libraries**: First, you'll need to import the OpenCV library. OpenCV (Open Source Computer Vision Library) is a popular open-source computer vision and machine learning library that provides a wide range of tools and functions for **image and video processing, object detection, face recognition**, and more.
* **Load the Haar cascades**: Load the Haar cascades XML file for face detection using the cv2.CascadeClassifier() method.
* **Load the image or video**: Load the image or video on which you want to perform face detection using the cv2.imread() or cv2.VideoCapture() methods.
* **Convert the image to grayscale**: Convert the image to grayscale using the cv2.cvtColor() method.
* **Detect faces using Haar cascades**: Apply the Haar cascades to the grayscale image using the detectMultiScale() method of the cv2.CascadeClassifier() object. This method will return a list of rectangles representing the location and size of the detected faces.
* **Draw rectangles around the detected faces**: Loop through the list of detected faces and draw a rectangle around each face using the cv2.rectangle() method.
* **Display the image or video with detected faces**: Display the original image or video with the detected faces using the cv2.imshow() and cv2.waitKey() methods.
* **Clean up**: Once the face detection is complete, release any resources used by the OpenCV functions and close any windows that were opened for display.

### Final Output
When performing face detection using Haar cascades in OpenCV, the output typically consists of an image with rectangles drawn around the detected faces. These **rectangles represent the bounding boxes** of the detected faces.

It's important to note that the accuracy and quality of the face detection output can depend on a variety of factors, such as the **quality of the input image**, the **size and orientation** of the faces in the image, and the specific Haar cascade being used for detection.

[IMAGE {2} {Face-detection-Final-Output} START SAMPLE]
![](https://hackmd.io/_uploads/BkUPQ8_eh.png)
[IMAGE {2} FINISH SAMPLE]

:::
:::section{.main}
## Requirements

To perform face detection using OpenCV in Python using Haar cascades, you will need the following prerequisites:

* **Python:** First, you'll need to have Python installed on your computer. You can download the latest version of Python from the official website.

* **OpenCV:** You'll also need to have OpenCV installed on your computer. To perform face detection using OpenCV, we first need to install OpenCV and its dependencies. We can install it using pip by typing the following command in the terminal:

    ```py
    pip install OpenCV-python
    ```
    After installing you will see an output like the image below:
    
    [IMAGE {2} {OpenCV-install-success} START SAMPLE]
![OpenCV-install-success](https://hackmd.io/_uploads/rkmXmFLz2.png)
[IMAGE {2} FINISH SAMPLE]
    

    


* **Haar cascades:** Haar cascades are XML files that contain the data needed to detect specific objects in an image or video stream. For face detection, you'll need the "haarcascade_frontalface_default.xml" file. You can download this file from the OpenCV GitHub repository.

* **Image or video:** You'll need an image or video to perform face detection using OpenCV.

Once you have these prerequisites, you can start writing Python code to perform face detection using OpenCV (Haar cascades).



:::
:::section{.main}
## Implementation of Face Detection Using OpenCV And Python

The implementation of Face detection using OpenCV and Python is very easy and requires only a little amount of time to understand the process. 

Here's a step-by-step explanation for face detection using OpenCV Haar cascades in Python:


OpenCV provides pre-trained models and APIs for detecting and recognizing objects in images and videos.  This will give you access to all the functions and APIs provided by OpenCV for performing various computer vision tasks.

**Import the OpenCV library for computer vision tasks.**
The first step is to import the OpenCV module. This makes its functions and classes available for use in your code. Once you have imported OpenCV, you can start using its functions and classes to perform various computer vision and image processing tasks.
```python
import cv2

```

**Load the Haar cascades XML file for face detection using OpenCV.** 

The pre-trained classifiers are XML files that contain the trained models and can be loaded into your Python code using the **cv2.CascadeClassifier()** function.

```python
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

```

The above code loads the XML file containing the pre-trained Haar cascade for face detection. The file should be saved in the same directory as the Python script.

The file can be viewed/downloaded from this [repository](https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml).

**Load the Image for OpenCV Python face detection**

To load an image for face detection in OpenCV Python, we will use the **cv2.imread()** function to read the image file and store it as a NumPy array.
```python
img = cv2.imread('image.jpg')

```
This loads the image that you want to perform face detection on. Replace 'image.jpg' with the file name and path of your image.

**Convert the image to grayscale**

To convert an image to grayscale in OpenCV Python, we will use the **cv2.cvtColor()** function with the **cv2.COLOR_BGR2GRAY flag** to convert the BGR color image to grayscale.
```python
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

```
This converts the loaded image to grayscale. Haar cascades are typically applied to grayscale images for faster processing.


[IMAGE {3} {Grayscale Conversion} START SAMPLE]
![Grayscale Conversion](https://hackmd.io/_uploads/HkrKrFIGn.png)

[IMAGE {3} FINISH SAMPLE]

**Detect faces using OpenCV python Haar cascades**
To detect faces in an image using OpenCV Python and Haar cascades, we will use the **detectMultiScale()** function to perform face detection.
```python
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)

```
The **scaleFactor and minNeighbors** parameters control the sensitivity and accuracy of the face detection.

**Draw rectangles around the detected faces**

To draw rectangles around the detected faces in OpenCV Python, loop through the list of detected faces and use the **cv2.rectangle() function** to draw a rectangle around each face using its bounding box coordinates.
```python 
for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)

```
* This loops through the list of detected faces and draws a green rectangle around each face on the original color image. 
* The (x, y) coordinates and width and height (w, h) of each detected face are obtained from the faces variable returned by **detectMultiScale()** method.

**Display the image with detected faces**

The final step is to display the original image with the detected faces. The first argument of the **imshow()** method is the name of the window that will display the image. 
```python
cv2.imshow('Detected Faces', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

```

The output is received similarly to the image given below: 

[IMAGE {4} {Final Output} START SAMPLE]
![Final Output](https://hackmd.io/_uploads/rySmBYLfh.png)
[IMAGE {4} FINISH SAMPLE]



The waitKey method waits for a key press event, and the **destroyAllWindows()** method closes all open windows once a key is pressed.

Here's the complete code for face detection using OpenCV Haar cascades in Python:

```python
import cv2

# Load the Haar cascades XML file for face detection
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# Load the image
img = cv2.imread('image.jpg')

# Convert the image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Detect faces using Haar cascades
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)

# Draw rectangles around the detected faces
for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)

# Display the image with detected faces
cv2.imshow('Detected Faces', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
Make sure you replace 'image.jpg' with the file name and path of your image in the above code for OpenCV python face detection.
:::
:::section{.main}

## Testing

Let’s test against another photo!
The output for the above-mentioned code:

[IMAGE {4} {Face-detection-final-output} START SAMPLE]
![](https://hackmd.io/_uploads/ByLLQU_gn.png)
[IMAGE {4} FINISH SAMPLE]


:::
:::section{.summary}
## Conclusion

 
* In conclusion, face detection using OpenCV Haar cascades in Python involves, Loading the Haar cascades XML file and image, Converting the image to grayscale, Applying the Haar cascades to detect faces, Drawing rectangles around the detected faces, and Displaying the final image with detected faces.
* Face detection using OpenCV is a powerful technique for image processing, computer vision, and machine learning. 
* It provides various algorithms for face detection and can be used for various applications such as facial recognition, emotion detection, and gender classification. 
* With the increasing demand for computer vision and machine learning, OpenCV Python face detection is becoming more important and relevant than ever.

:::
:::section{.main}
## MCQs 
**1. What is the purpose of converting the image to grayscale in face detection using OpenCV Haar cascades in Python?**
a) To display the image with detected faces in grayscale
b) To increase the accuracy of face detection
c) To speed up the face detection process
d) To reduce the sensitivity of face detection
**Answer: c) To speed up the face detection process**

**2. Which method of the CascadeClassifier object applies the Haar cascades to the grayscale image for face detection uisng OpenCV?**
a) detectFaces()
b) detectMultiScale()
c) applyCascades()
d) applyHaar()
**Answer: b) detectMultiScale()**

**3. What is the purpose of the scaleFactor parameter in face detection using OpenCV Haar cascades in Python?**
a) To increase the accuracy of OpenCV Python face detection
b) To reduce the sensitivity of face detection
c) To control the size of the image patches during face detection
d) To control the number of image patches during face detection
**Answer: c) To control the size of the image patches during face detection**

:::