Real Time Face Blurring with OpenCV Python

Real Time Face Blurring with OpenCV Python

Table of Contents

  1. Introduction
  2. Installing Python and OpenCV
  3. Importing the necessary libraries
  4. Loading the face cascade
  5. Capturing input from the webcam
  6. Converting the colored image to grayscale
  7. Detecting faces in the grayscale image
  8. Drawing rectangles around the detected faces
  9. Applying median blur to blur the faces
  10. Displaying the blurred image
  11. Conclusion

Introduction

In this article, we will learn how to Create a Python program to auto-blur human faces using OpenCV. We will start by installing Python and OpenCV if they are not already installed. Then, we will import the necessary libraries and load the face cascade file. Next, we will capture input from the webcam, convert the colored image to grayscale, and detect faces in the grayscale image. We will draw rectangles around the detected faces and Apply median blur to blur the faces. Finally, we will display the blurred image.

Let's get started!


1. Installing Python and OpenCV

Before we begin, make sure You have Python installed on your system. If you don't, simply visit python.org and follow the instructions to install it. Once Python is installed, you can install OpenCV by opening the command prompt and running the command pip install opencv-python.

2. Importing the necessary libraries

To get started, we need to import the OpenCV library in Python. We will also import the time library, which we will later use to close the GUI window.

import cv2
import time

3. Loading the face cascade

The next step is to load the face cascade file. The XML file contains the feature data required for detecting faces. We will be using the frontal face cascade file, which is specifically trained to detect frontal faces.

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

4. Capturing input from the webcam

Now let's create a variable to capture input from the webcam. We will use the OpenCV VideoCapture function and set the input parameter to 0, which is the default value for the webcam.

video = cv2.VideoCapture(0)

5. Converting the colored image to grayscale

To make the face detection process easier and more accurate, we will convert the colored image captured from the webcam to grayscale. OpenCV provides the cv2.cvtColor() function to perform this conversion.

while True:
    check, frame = video.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

6. Detecting faces in the grayscale image

Now, let's use the loaded face cascade and detect faces in the grayscale image. We will use the cv2.detectMultiScale() function, which returns the coordinates of the detected faces.

faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))

7. Drawing rectangles around the detected faces

Once we have detected the faces, we will draw rectangles around them using the coordinates returned by the detectMultiScale() function.

for (x, y, w, h) in faces:
    cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 1)

8. Applying median blur to blur the faces

To blur the detected faces, we will apply the median blur function from OpenCV. The cv2.medianBlur() function takes the image and the blur percentage as parameters.

for (x, y, w, h) in faces:
    face = frame[y:y+h, x:x+w]
    face = cv2.medianBlur(face, 35)
    frame[y:y+h, x:x+w] = face

9. Displaying the blurred image

To see the effect of the blur, we will display the blurred image in a GUI window using the cv2.imshow() function.

cv2.imshow("Auto Blur", frame)

10. Conclusion

In this article, we have learned how to create a Python program to auto-blur human faces using OpenCV. We started by installing Python and OpenCV, then imported the necessary libraries. We loaded the face cascade file and captured input from the webcam. We converted the colored image to grayscale and detected faces in the grayscale image. We drew rectangles around the detected faces and applied median blur to blur the faces. Finally, we displayed the blurred image using a GUI window.

Now you can use this program to automatically blur faces in your images or videos for privacy or security purposes. Experiment with different parameters and settings to achieve the desired level of blur.

Most people like

Find AI tools in Toolify

Join TOOLIFY to find the ai tools

Get started

Sign Up
App rating
4.9
AI Tools
20k+
Trusted Users
5000+
No complicated
No difficulty
Free forever
Browse More Content