Faces Blur in Videos using OpenCV in Python
Last Updated :
28 Apr, 2025
OpenCV is an open-source cross-platform library for various operating systems, including Windows, Linux, and macOS, for computer vision, machine learning, and image processing. With the help of OpenCV, we can easily process images and videos to recognize objects, faces, or even someone's handwriting.
In this article, we will see how to blur faces in images and videos using OpenCV in Python.
Requirements
In addition to the OpenCV module and in order to recognize faces, we also need Haar Cascade Frontal Face Classifier, which needs to be downloaded. It is provided as an XML file and is used to detect faces in images and videos.
Make sure to download the Haar Cascade Frontal Face Classifier from this link: haarcascade_frontalface_default.xml.
Blur the faces in Images using OpenCV in Python
First, we will load an image that contains some faces so, that we can test our code. After that, we will convert it into RGB format and then detect faces using the haar cascade classifier. After this, we will get the bounding box coordinates by using which we will blur that particular region, and then we can show that image along with the original image.
Python3
# Importing libraries
import numpy as np
import cv2
import matplotlib.pyplot as plt
# Reading an image using OpenCV and
# Converting it into a
# RGB image because OpenCV reads
# images by default in BGR format
image = cv2.cvtColor(cv2.imread('image.png'),
cv2.COLOR_BGR2RGB)
# Display the Original Image
print('Original Image')
plt.imshow(image, cmap="gray")
plt.axis('off')
plt.show()
cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
face_data = cascade.detectMultiScale(image,
scaleFactor=2.0,
minNeighbors=4)
for x, y, w, h in face_data:
# Draw a border around the detected face in Red
# colour with a thickness 5.
image = cv2.rectangle(image, (x, y), (x+w, y+h),
(255, 0, 0), 5)
image[y:y+h, x:x+w] = cv2.medianBlur(image[y:y+h,
x:x+w],
35)
# Display the Blured Image
print('Blured Image')
plt.imshow(image, cmap="gray")
plt.axis('off')
plt.show()
Output:
Original image and the blurred face imageBlur the faces in Videos using OpenCV in Python
First, we will load a video that contains some faces so, that we can test our code. After that, we will convert it into grayscale and then detect faces using the haar cascade classifier. After this, we will get the bounding box coordinates by using which we will blur that particular region, and then we can show that video.
Python3
# Importing libraries
import cv2
# to detect the face of the human
cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
# Create a VideoCapture object and read from input file
video_capture = cv2.VideoCapture('video.mp4')
# Read until video is completed
while(video_capture.isOpened()):
# Capture frame-by-frame
ret, frame = video_capture.read()
# convert the frame into grayscale(shades of black & white)
gray_image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face_data = cascade.detectMultiScale(gray_image,scaleFactor=1.3,minNeighbors=5)
for x, y, w, h in face_data:
# Draw a border around the detected face in Red colour with a thickness 5.
image = cv2.rectangle(frame, (x, y),(x+w, y+h), (0,0,255), 5)
image[y:y+h, x:x+w] = cv2.medianBlur(image[y:y+h, x:x+w], 35)
# show the blurred face in the video
cv2.imshow('face blurred', frame)
key = cv2.waitKey(1)
# Press Q on keyboard to exit
if key == ord('q'):
break
# When everything done, release
# the video capture object
video_capture.release()
# Closes all the frames
cv2.destroyAllWindows()
Output:
Similar Reads
How to Blur Faces in Images using OpenCV in Python? Prerequisite: OpenCV OpenCV is a huge open-source library for computer vision, machine learning, and image processing. Â It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as Numpy which is a highly optim
2 min read
Creating a Slow Motion Video Using OpenCV - Python In this article, we will try to create a slow-motion video using OpenCV( Open Source Computer Vision) library in Python. OpenCV ( is an open-source computer vision and machine learning software library. Now a video is basically a set of moving pictures moving at a rate of about 24 frames per second
4 min read
Display date and time in videos using OpenCV - Python OpenCV-Python is a library of Python bindings designed to solve computer vision problems. It can process images and videos to identify objects, faces, or even the handwriting of a humanNote: For more information, refer to Introduction to OpenCVÂ Display date and time in videos It sometimes becomes n
2 min read
Display date and time in videos using OpenCV - Python OpenCV-Python is a library of Python bindings designed to solve computer vision problems. It can process images and videos to identify objects, faces, or even the handwriting of a humanNote: For more information, refer to Introduction to OpenCVÂ Display date and time in videos It sometimes becomes n
2 min read
Python | Play a video using OpenCV OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on Images or videos. OpenCV library can be used to perform multiple operations on videos. Letâs see how to play a video using the OpenCV Python. To capture a video, we need to crea
2 min read
Blending of two videos using Python Prerequisite: Addition and Blending of images using OpenCV in Python In this article, we will be showing you how to effectively blend two videos together. But for that, we will first see what is Alpha Blending. Alpha Blending Alpha blending is a curved mix of two tones taking into consideration tra
5 min read