How to Build an Algorithm for Face and License Plate Blurring Using OpenCV and Python

This step-by-step guide covers everything from installing OpenCV to applying Gaussian blurring to the detected regions


13 April 2023, by Mario Sabatino RiontinoAsk a question


Figure 1: Photo by Brooke Cagle on Unsplash
Figure 1: Photo by Brooke Cagle on Unsplash

In today's world, privacy is of the utmost importance. As the cost of buying high-resolution cameras has decreased over the years, it became easier than ever to capture and share images and videos.

Whilst the increasing volume of available data is definitively good news for accelerating the development of even more advanced computer vision applications (e.g., autonomous vehicles, asset management, etc), ethical questions must be asked when personal data are involved.

As we extensively argued in previous articles, blurring is the most effective instrument to secure personal data from a legal and technical perspective. In this article, we'll show you how to build an algorithm for blurring faces and license plates using OpenCV and Python.

Installing OpenCV and its dependencies

OpenCV is an open-source computer vision library that allows developers to build image and video processing applications. It provides various features such as image and video capture, filtering, and object detection.

Before we start coding, we need to install OpenCV and its dependencies. We'll be using the pip package manager to install OpenCV, which can be done with the following command:

pip install opencv-python

We'll also need to install numpy, which is a package for scientific computing with Python. This can be done with the following command:

pip install numpy

Loading the image and detecting faces and license plates

Once we've installed OpenCV and its dependencies, we can start coding. The first step is to load the image and detect faces in the image. We'll be using OpenCV's CascadeClassifier to detect faces and license plates. First, download the classifiers from GitHub:

[pip install numpy](https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml
https://github.com/opencv/opencv/blob/3.4/data/haarcascades/haarcascade_russian_plate_number.xml
)

Here's the code to load the image and detect faces and license plates:

import cv2
import numpy as np

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

# Create a CascadeClassifier object for face detection
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# Create a CascadeClassifier object for license plate detection
plate_cascade = cv2.CascadeClassifier('haarcascade_russian_plate_number.xml')

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

# Detect faces and license plates in the image
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
plates = plate_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)

# Optional: Draw rectangles around the detected faces and license plates
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 0, 255), 10)

for (x, y, w, h) in plates:
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 10)

This code loads the image 'image.jpg' and creates CascadeClassifier objects for face and license plate detection. It then converts the image to grayscale and detects faces and license plates in the image using the detectMultiScale() method. Finally, it draws rectangles around the detected faces and license plates.

Applying blurring to the detected regions

Now that we've detected the faces and license plates in the image, we can apply blurring to these regions to anonymize them. There are several methods of blurring, including pixelation and Gaussian blurring. For this example, we'll be using Gaussian blurring, which produces a smoother and more natural-looking blur.

Here's the code to apply Gaussian blurring to the detected faces and license plates, display the results and save the resulting image:

# Apply Gaussian blurring to the detected faces and license plates
for (x, y, w, h) in faces:
    roi = image[y:y+h, x:x+w]
    blurred_roi = cv2.GaussianBlur(roi, (255, 255), 0)
    image[y:y+h, x:x+w] = blurred_roi

for (x, y, w, h) in plates:
    roi = image[y:y+h, x:x+w]
    blurred_roi = cv2.GaussianBlur(roi, (255, 255), 0)
    image[y:y+h, x:x+w] = blurred_roi

# Optional: Display the image with the detected faces and license plates
cv2.imshow('Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

# Save the anonymized image to disk
cv2.imwrite('anonymized_image.jpg', image)

This code applies Gaussian blurring to the detected faces and license plates using the GaussianBlur() method. We create a region of interest (ROI) for each detected face and license plate, apply the blur to the ROI, and then replace the original region with the blurred region in the image. Finally, we display the anonymized image and save it to disk using the imwrite() method.

Figure 2: Anonymized image using the above code
Figure 2: Anonymized image using the above code

Building Your Solution vs. Using a Ready-to-Go Solution

While building your solution using OpenCV and Python might seem the best option to have full control over the code and customize it to your specific needs, it presents some disadvantages:

  • It can be time-consuming to build and test a solution from scratch.
  • You need to have good ML segmentation or objection model. You’ll soon realize that the Cascade Classifiers are very limited in accuracy and efficiency (especially for specific use cases)
  • It defocuses you from the core aspects of your project
  • Maintenance and updates may require additional time and resources.

Therefore, using a ready-to-go solution like Celantur for face and license plate blurring can provide many advantages:

  • You can save time and resources by using a pre-built solution, allowing you to focus on other aspects of your project.
  • Celantur uses state-of-the-art algorithms and machine learning models for superior accuracy and efficiency.
  • The solution is constantly updated and maintained, ensuring that it remains up-to-date with the latest security measures.
  • Celantur offers a wide range of features and functionalities, including API access, on-premise deployment, and customization options.

Figure 3: Anonymized image using Celantur algorithm
Figure 3: Anonymized image using Celantur algorithm

In conclusion, face and license plate blurring can be accomplished using OpenCV and Python. while there are benefits to building your solution, using a ready-to-go solution can save time and provide a superior solution in terms of accuracy, maintenance, and deployment options. Regardless of the approach taken, the most important goal is protecting privacy in images and videos.

Ask us Anything. We'll get back to you shortly

linuxenglish
Start Demo Contact Us

Latest Blog Posts

Edge AI Learnings: CPU Architectures, GPU Capabilities, and Challenges with Nvidia Jetson

We delve into the intricacies of CPU choices (ARM vs. x86), unlock the mysteries of GPU capabilities, and tackle the unique challenges of Jetson platforms.


Celantur at Esri UC 2024: Solutions Now Listed on ArcGIS Marketplace

Celantur Solutions Available on ArcGIS Marketplace, and Growing Demand in the US.


Celantur at the Esri User Conference 2024 in San Diego

Visit Celantur at Esri UC 2024 and discover the future of data anonymization.