关键词:隐私保护;人脸匿名化;模糊处理;计算机视觉
一、项目简介
随着生活的智能化与信息化,视频监控、人脸识别、社交网络等为隐私侵犯提供了捷径。这些技术使得我们可能随时被拍照、被摄像、人脸信息被到处记录。同时,人脸图像因具有大量独特的个人特征,如果直接公开这些具有隐私信息的人脸图像,就会导致个人敏感信息的泄露,因此人脸图像的隐私保护至关重要。目前,对人脸进行隐私保护的常用方法是对其进行模糊处理及匿名化,本项目将使用深度学习及计算机视觉技术对人脸进行模糊处理及匿名化。
二、人脸模糊处理及匿名化算法流程
人脸模糊处理是一种常见的计算机视觉方法,主要对图像和视频中的人脸图像进行匿名处理,以此对人脸信息进行隐私保护。人脸面部模糊处理及匿名化的详细步骤如下图所示。
第一步:人脸检测。在这一步骤中,任意人脸检测算法都可以使用,前提是它可以在图像或视频流中生成人脸的边界框坐标。比较典型的人脸检测算法包括:Haar级联、HOG+SVM、基于深度学习的人脸检测算法。
第二步:提取人脸面部区域 (ROI)。在第一步中,可以得到边界框坐标(x, y),坐标具体表示:x的起始坐标和y的起始坐标,然后,可以使用此信息来提取人脸面部区域(ROI)。
第三步:实际模糊处理及匿名化人脸面部。通常,我们将应用高斯模糊来匿名化人脸面部区域。如果想要最终的结果更美观,可以应用一些方法对人脸面部进行像素化处理。
第四步:将模糊处理的人脸面部区域存储到原始图像中。使用第一步人脸检测得到的原始坐标(x, y),可以获取模糊处理及匿名化的人脸图像,然后将其存储到原始图像中,这一步将会使用NumPy数组。
三、软件平台
Mind+的Python模式。需要安装OpenCV、NumPy、imutils等库。
四、算法实践
(一)使用深度学习进行人脸检测
我们将使用OpenCV中自带的基于深度学习的 Caffe 模型进行人脸检测。
(二)使用高斯模糊算法对人脸进行模糊处理
首先,需要构建两个辅助函数来处理面部模糊和匿名化,分别是:anonymize_face_simple,对人脸面部区域 (ROI)执行简单的高斯模糊;anonymize_face_pixelate,创建像素级的模糊效果。
第一步新建face_blurring.py脚本文件,首先构建anonymize_face_simple辅助函数。
#导入NumPy和OpenCV
import numpy as np
import cv2
#定义anonymize_face_simple函数
def anonymize_face_simple(image, factor=3.0):
#在输入图像的空间维度上确定基于模糊核的大小
(h, w) = image.shape[:2]
kW = int(w / factor)
kH = int(h / factor)
#确保内核的宽度是奇数
if kW % 2 == 0:
kW -= 1
#确保内核宽度是奇数
if kH % 2 == 0:
kH -= 1
return cv2.GaussianBlur(image, (kW, kH), 0)
接着创建像素级的模糊效果,构建anonymize_face_pixelate辅助函数。
#定义anonymize_face_pixelate函数
def anonymize_face_pixelate(image, blocks=3):
#将输入图像分成N*N个块
(h, w) = image.shape[:2]
xSteps = np.linspace(0, w, blocks + 1, dtype="int")
ySteps = np.linspace(0, h, blocks + 1, dtype="int")
#在x和y方向循环遍历块
for i in range(1, len(ySteps)):
for j in range(1, len(xSteps)):
#对于当前块计算开始和结束的坐标(x, y)
startX = xSteps[j - 1]
startY = ySteps[i - 1]
endX = xSteps[j]
endY = ySteps[i]
#使用NumPy数组切片提取ROI,计算ROI的平均值,然后用原始图像中ROI上的平均RGB值绘制一个矩形
roi = image[startY:endY, startX:endX]
(B, G, R) = [int(x) for x in cv2.mean(roi)[:3]]
cv2.rectangle(image, (startX, startY), (endX, endY),
(B, G, R), -1)
#返回像素化的模糊图像
return image
(三)实现图像中人脸模糊处理及匿名化
#导入需要用到的软件包
from zy.face_blurring import anonymize_face_pixelate
from zy.face_blurring import anonymize_face_simple
import numpy as np
import argparse
import cv2
import os
#构造参数parse并解析参数
ap = argparse.ArgumentParser()
ap.add_argument("-m", "--method", type=str, default="simple",
choices=["simple", "pixelated"],
help="face blurring/anonymizing method")
ap.add_argument("-b", "--blocks", type=int, default=20,
help="# of blocks for the pixelated blurring method")
ap.add_argument("-c", "--confidence", type=float, default=0.5,
help="minimum probability to filter weak detections")
args = vars(ap.parse_args())
#从电脑磁盘中加载人脸检测器模型
print("[INFO] loading face detector model...")
prototxtPath = os.path.sep.join([r'D:\opencv-face-blurring\face_detector', "deploy.prototxt"])
weightsPath = os.path.sep.join([r'D:\opencv-face-blurring\face_detector',
"res10_300x300_ssd_iter_140000.caffemodel"])
net = cv2.dnn.readNet(prototxtPath, weightsPath)
#从电脑磁盘中读取并加载输入图像,进行复制,然后提取图像在空间维度的尺寸
image = cv2.imread(r'D:\opencv-face-blurring\examples\zy.png')
orig = image.copy()
(h, w) = image.shape[:2]
#构建blob
blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300),
(104.0, 177.0, 123.0))
#通过网络传递blob并获得人脸检测
print("[INFO] computing face detections...")
net.setInput(blob)
detections = net.forward()
#循环检测
for i in range(0, detections.shape[2]):
#提取与检测相关的置信度(即概率)
confidence = detections[0, 0, i, 2]
if confidence > args["confidence"]:
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
#提取人脸ROI
face = image[startY:endY, startX:endX]
#判断是否应用“simple”面部模糊处理方法
if args["method"] == "simple":
face = anonymize_face_simple(face, factor=3.0)
#否则,必须应用“blocks”面部模糊处理方法
else:
face = anonymize_face_pixelate(face,
blocks=args["blocks"])
#将模糊的人脸图像存储在输出图像中
image[startY:endY, startX:endX] = face
#并排显示原始图像和带有模糊处理的输出图像
output = np.hstack([orig, image])
cv2.imshow("Output", output)
cv2.waitKey(0)
1、高斯方法模糊处理及匿名化如下图所示。
2、创建像素级的模糊处理及匿名化效果,如下图所示。
(四)在实时视频流中实现人脸模糊处理及匿名化
#导入需要用到的软件包
from zy.face_blurring import anonymize_face_pixelate
from zy.face_blurring import anonymize_face_simple
from imutils.video import VideoStream
import numpy as np
import argparse
import imutils
import time
import cv2
import os
#构造参数parse并解析参数
ap = argparse.ArgumentParser()
ap.add_argument("-m", "--method", type=str, default="simple",
choices=["simple", "pixelated"],
help="face blurring/anonymizing method")
ap.add_argument("-b", "--blocks", type=int, default=20,
help="# of blocks for the pixelated blurring method")
ap.add_argument("-c", "--confidence", type=float, default=0.5,
help="minimum probability to filter weak detections")
args = vars(ap.parse_args())
#从电脑磁盘加载人脸检测器模型
print("[INFO] loading face detector model...")
prototxtPath = os.path.sep.join([r'D:\opencv-face-blurring\face_detector', "deploy.prototxt"])
weightsPath = os.path.sep.join([r'D:\opencv-face-blurring\face_detector',
"res10_300x300_ssd_iter_140000.caffemodel"])
net = cv2.dnn.readNet(prototxtPath, weightsPath)
#初始化视频流
print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
time.sleep(2.0)
#循环视频流中的帧
while True:
#从线程视频流中抓取帧并调整其大小,最大宽度为400像素
frame = vs.read()
frame = imutils.resize(frame, width=400)
#获取框架的尺寸,然后构造一个blob
(h, w) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300),
(104.0, 177.0, 123.0))
#通过网络传递blob并获得人脸检测
net.setInput(blob)
detections = net.forward()
#循环检测
for i in range(0, detections.shape[2]):
#提取与检测相关的置信度(即概率)
confidence = detections[0, 0, i, 2]
#通过确保置信度大于最小置信度来过滤掉弱检测
if confidence > args["confidence"]:
#计算对象边界框的坐标(x, y)
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
#提取人脸ROI
face = frame[startY:endY, startX:endX]
#检查是否应用了“simple”的面部模糊处理方法
if args["method"] == "simple":
face = anonymize_face_simple(face, factor=3.0)
#否则,必须应用“blocks”面部模糊处理方法
else:
face = anonymize_face_pixelate(face,
blocks=args["blocks"])
#将模糊的人脸图像存储在输出图像中
frame[startY:endY, startX:endX] = face
#显示输出帧
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
#如果按下‘q’键,则中断循环
if key == ord("q"):
break
#做一些清理工作
cv2.destroyAllWindows()
vs.stop()
在实时视频流中实现人脸模糊处理及匿名化效果视频如下所示。
五、项目总结
该项目有效实现了人脸模糊处理及匿名化,接下来,为了确保模糊处理及匿名化的有效性,将结合使用对象跟踪算法,该方法首先检测视频流中的人脸图像,然后为每个人脸创建一个对象跟踪器,接着使用对象跟踪器和人脸检测器关联人脸的位置,最后,如果人脸检测器未检测到,则回退到跟踪器以提供人脸的位置。
六、展望未来
本项目以人脸隐私保护为目的,将人脸进行模糊处理及匿名化,我们深知,随着技术的发展,隐私成为了最重要的社会问题之一。通信、多媒体、生物特征识别、数据挖掘、社交网络和音视频监控,每一种都为隐私侵犯提供了捷径。其中,大量的隐私信息蕴含在各种图像中,如果直接公开这些具有隐私信息的图像,就会导致个人敏感信息的泄露。因为人脸图像具有大量独特的个人特征,所以人脸图像的隐私保护也成为了研究的热点。如何在保护人脸隐私信息的同时,保护人脸图像可用性的研究,是具有重要价值和研究意义的。
现在就想起来以前不2021.10.09
这个太厉害了。人脸识别吗。?
李臻飏2021.10.08
zbc
李孟阳2021.09.24
厉害厉害
肖江锦2021.09.24
正在学计算机视觉方面的知识呢,正好可以作为一个项目来练。
ajue2021.09.24
保护隐私信息太重要了
Lincoln07052021.09.24
太棒啦!学习学习