使用 OpenCV 和Python模糊和匿名人脸
在本文中,我们将了解如何使用 OpenCV 和Python对人脸进行模糊处理和匿名化处理。
为此,我们将使用级联分类器来检测人脸。确保从以下链接下载相同的内容:haarcascade_frontalface_default.xml
方法
- 首先,我们使用内置的人脸检测算法,从实时视频或图像中检测人脸。在这里,我们将使用级联分类器方法从实时视频中检测人脸(使用网络摄像头)。
- 然后,读取来自实时视频的帧。存储最新帧并转换为灰度,以更好地了解特征。
- 现在,为了使输出美观,我们将在检测到的人脸周围制作一个带颜色边框的矩形。但是,我们希望检测到的人脸是模糊的,所以我们使用 medianBlur函数来做同样的事情,并提到人脸应该模糊的区域。
- 而且,现在我们要显示模糊的脸,使用 imshow函数读取的帧,我们希望它显示出来,直到我们按下一个键。
逐步实施:
第 1 步:导入人脸检测算法,称为级联分类器。
Python3
import cv2
# to detect the face of the human
cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
Python3
video_capture = cv2.VideoCapture(0)
while True:
# capture the latest frame from the video
check, frame = video_capture.read()
Python3
# convert the frame into grayscale(shades of black & white)
gray_image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face = cascade.detectMultiScale(gray_image,
scaleFactor=2.0,
minNeighbors=4)
Python3
for x, y, w, h in face:
# draw a border around the detected face.
# (here border color = green, and thickness = 3)
image = cv2.rectangle(frame, (x, y),
(x+w, y+h),
(0, 255, 0), 3)
Python3
# blur the face which is in the rectangle
image[y:y+h, x:x+w] = cv2.medianBlur(image[y:y+h, x:x+w], 35)
Python3
# show the blurred face in the video
cv2.imshow('face blurred', frame)
key = cv2.waitKey(1)
Python3
import cv2
# to detect the face of the human
cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
# VideoCapture is a function, to capture
# video from the camera attached to system
# You can pass either 0 or 1
# 0 for laptop webcam
# 1 for external webcam
video_capture = cv2.VideoCapture(0)
# a while loop to run infinite times,
# to capture infinite number of frames for video
# because a video is a combination of frames
while True:
# capture the latest frame from the video
check, frame = video_capture.read()
# convert the frame into grayscale(shades of black & white)
gray_image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# detect multiple faces in a captured frame
# scaleFactor: Parameter specify how much the
# image sizeis reduced at each image scale.
# minNeighbors: Parameter specify how many
# neighbours each rectangle should have to retain it.
# rectangle consists the detect object.
# Here the object is the face.
face = cascade.detectMultiScale(
gray_image, scaleFactor=2.0, minNeighbors=4)
for x, y, w, h in face:
# draw a border around the detected face.
# (here border color = green, and thickness = 3)
image = cv2.rectangle(frame, (x, y), (x+w, y+h),
(0, 255, 0), 3)
# blur the face which is in the rectangle
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)
# This statement just runs once per frame.
# Basically, if we get a key, and that key is a q,
if key == ord('q'):
break
# we will exit the while loop with a break,
# which then runs:
video_capture.release()
cv2.destroyAllWindows()
第 2 步:从视频中捕获帧,以便从帧中检测人脸
Python3
video_capture = cv2.VideoCapture(0)
while True:
# capture the latest frame from the video
check, frame = video_capture.read()
第 3 步:将捕获的帧更改为灰度。
Python3
# convert the frame into grayscale(shades of black & white)
gray_image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face = cascade.detectMultiScale(gray_image,
scaleFactor=2.0,
minNeighbors=4)
第 4 步:在检测到的人脸周围绘制一个彩色矩形。
Python3
for x, y, w, h in face:
# draw a border around the detected face.
# (here border color = green, and thickness = 3)
image = cv2.rectangle(frame, (x, y),
(x+w, y+h),
(0, 255, 0), 3)
第5步:模糊矩形内的部分(包含检测到的人脸)。
Python3
# blur the face which is in the rectangle
image[y:y+h, x:x+w] = cv2.medianBlur(image[y:y+h, x:x+w], 35)
步骤6:显示最终输出,即检测到的人脸(在矩形内)是模糊的。
Python3
# show the blurred face in the video
cv2.imshow('face blurred', frame)
key = cv2.waitKey(1)
下面是完整的实现:
Python3
import cv2
# to detect the face of the human
cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
# VideoCapture is a function, to capture
# video from the camera attached to system
# You can pass either 0 or 1
# 0 for laptop webcam
# 1 for external webcam
video_capture = cv2.VideoCapture(0)
# a while loop to run infinite times,
# to capture infinite number of frames for video
# because a video is a combination of frames
while True:
# capture the latest frame from the video
check, frame = video_capture.read()
# convert the frame into grayscale(shades of black & white)
gray_image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# detect multiple faces in a captured frame
# scaleFactor: Parameter specify how much the
# image sizeis reduced at each image scale.
# minNeighbors: Parameter specify how many
# neighbours each rectangle should have to retain it.
# rectangle consists the detect object.
# Here the object is the face.
face = cascade.detectMultiScale(
gray_image, scaleFactor=2.0, minNeighbors=4)
for x, y, w, h in face:
# draw a border around the detected face.
# (here border color = green, and thickness = 3)
image = cv2.rectangle(frame, (x, y), (x+w, y+h),
(0, 255, 0), 3)
# blur the face which is in the rectangle
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)
# This statement just runs once per frame.
# Basically, if we get a key, and that key is a q,
if key == ord('q'):
break
# we will exit the while loop with a break,
# which then runs:
video_capture.release()
cv2.destroyAllWindows()
输出: