如何在Python中使用OpenCV模糊图像中的人脸?

2021年11月11日17:16:40 发表评论 636 次浏览

Python OpenCV模糊图像中的人脸简介 - 使用 Python 中的 OpenCV 库执行人脸检测后,对图像和视频中的人脸进行模糊和匿名处理。

在许多情况下,你希望在 Internet 上公开上传视频或图像,并且你可能希望匿名该视频或图像中显示的随机人员。在本教程中,你将学习如何使用 Python 中的 OpenCV 库模糊图像和视频中的人脸。

Python如何模糊图像中的人脸?为了模糊图像中显示的人脸,你需要首先检测这些人脸及其在图像中的位置。幸运的是,我已经写了一篇关于人脸检测的教程,我们将只使用它的源代码,请随时查看它以获取有关人脸检测代码如何工作的更多详细信息。

Python模糊图像中的人脸示例如下:

首先,安装所需的依赖项:

pip3 install opencv-python numpy

打开一个新文件并导入:

import cv2
import numpy as np

Python OpenCV模糊图像中的人脸:由于在面部检测说明教程,因为我们需要初始化,我们深切的学习模式侦测脸部,我们需要得到的模型架构连同其预训练的权重,下载它们,并把它们在权重文件夹:

# https://raw.githubusercontent.com/opencv/opencv/master/samples/dnn/face_detector/deploy.prototxt
prototxt_path = "weights/deploy.prototxt.txt"
# https://raw.githubusercontent.com/opencv/opencv_3rdparty/dnn_samples_face_detector_20180205_fp16/res10_300x300_ssd_iter_140000_fp16.caffemodel 
model_path = "weights/res10_300x300_ssd_iter_140000_fp16.caffemodel"
# load Caffe model
model = cv2.dnn.readNetFromCaffe(prototxt_path, model_path)

Python模糊图像中的人脸示例 - 下面的代码读取这个图像,准备它,并将它传递给神经网络:

# read the desired image
image = cv2.imread("father-and-daughter.jpg")
# get width and height of the image
h, w = image.shape[:2]
# gaussian blur kernel size depends on width and height of original image
kernel_width = (w // 7) | 1
kernel_height = (h // 7) | 1
# preprocess the image: resize and performs mean subtraction
blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0))
# set the image into the input of the neural network
model.setInput(blob)
# perform inference and get the result
output = np.squeeze(model.forward())

现在,output对象是一个 NumPy 数组,它检测到了所有的人脸,让我们迭代这个数组,只模糊我们确信它是人脸的部分:

for i in range(0, output.shape[0]):
    confidence = output[i, 2]
    # get the confidence
    # if confidence is above 40%, then blur the bounding box (face)
    if confidence > 0.4:
        # get the surrounding box cordinates and upscale them to original image
        box = output[i, 3:7] * np.array([w, h, w, h])
        # convert to integers
        start_x, start_y, end_x, end_y = box.astype(np.int)
        # get the face image
        face = image[start_y: end_y, start_x: end_x]
        # apply gaussian blur to this face
        face = cv2.GaussianBlur(face, (kernel_width, kernel_height), 0)
        # put the blurred face into the original image
        image[start_y: end_y, start_x: end_x] = face

Python如何模糊图像中的人脸?不像在人脸检测教程中我们为每个检测到的人脸绘制边界框。相反,我们在这里获取框坐标并对其应用高斯模糊。

cv2.GaussianBlur()方法使用高斯滤波器模糊图像,将中值应用于内核大小内的中心像素。它接受输入图像作为第一个参数,高斯核大小作为第二个参数中的元组,sigma 参数作为第三个参数。

我们从原始图像计算了高斯核大小,在文档中,它说它必须是一个奇数和正整数,我将原始图像除以7,因此它取决于图像形状并执行按位 OR以确保结果值是一个奇数,当然你可以设置你自己的内核大小,它越大,图像越模糊。

Python OpenCV模糊图像中的人脸示例:在我们对每个人脸进行模糊处理后,我们将其设置回原始图像,这样,我们将得到一张所有人脸都被模糊处理的图像,结果如下:

如何在Python中使用OpenCV模糊图像中的人脸?
Python模糊图像中的人脸示例

非常棒!这样做的好处是你可以使用相机实时模糊面部,以及阅读你选择的外部视频,查看完整代码页了解所有这些。

我抓了一个 YouTube 视频并模糊了所有面孔,请查看这个Python模糊图像中的人脸示例:

如何在Python中使用OpenCV模糊图像中的人脸?
Python OpenCV模糊图像中的人脸示例

如果你想了解人脸检测的工作原理,请查看本教程的说明。

你可以在此Github 页面上获取本教程的所有资源。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: