OpenCV之人脸模糊

在这里插入图片描述

1.人脸模糊

人脸模糊是一种计算机视觉方法,用于匿名化图像和视频中的人脸,保护身份隐私。
思路:

  1. 输入视频或图像
  2. 检测出图像中的人脸
  3. 提取面部感兴趣区域ROI
  4. 使面部模糊化
  5. 返回结果,覆盖原图中的区域并显示

2.代码

环境:

  • win10
  • pycharm
  • anaconda3
  • python3.7
  • opencv4.2.0

对于OpenCV尽量用最新版本,可参考这篇仅一个命令行简单快速安装:https://blog.csdn.net/y459541195/article/details/104851892

文件结构:
在这里插入图片描述

2.1 单图像人脸模糊
from blur_face import blurred_face_gauss
from blur_face import  blurred_face_pixel
import numpy as np
import cv2
import argparse

"""
#方法一
执行:
python test_blur_face.py 

#方法二
执行:
python test_blur_face.py --method pixel
"""

ap = argparse.ArgumentParser()
ap.add_argument("-m", "--method", type=str, default="gauss",
	choices=["gauss", "pixel"],
	help="gauss/pixel method")
args = vars(ap.parse_args())
# 模型路径
prototxtPath ="./model/deploy.prototxt"
weightsPath = "./model/res10_300x300_ssd_iter_140000.caffemodel"
#加载模型
net  = cv2.dnn.readNet(prototxtPath,weightsPath)

#获取图像
image = cv2.imread("./input/test01.png")
src = image.copy()
(h,w)= image.shape[:2]

# 构造blob
blob = cv2.dnn.blobFromImage(image,1.0,(300,300),
                             (104,177,123))
# 送入网络计算
net.setInput(blob)
detect = net.forward()
# 检测
for i in range(0,detect.shape[2]):
    confidence = detect[0,0,i,2]
    # 过滤掉小的置信度,计算坐标,提取面部roi
    if confidence > 0.5:
        box = detect[0,0,i,3:7]*np.array([w,h,w,h])
        (startX,startY,endX,endY) = box.astype("int")
        face = image[startY:endY,startX:endX]

        #高斯方法
        if args["method"] == "gauss":
            face  = blurred_face_gauss(face,kernel_scale=3.0)

        # 像素模糊法
        else:
            face = blurred_face_pixel(face,pixel_blocks=20)

        image[startY:endY,startX:endX] = face

# 水平方向上平铺显示图
result = np.hstack([src,image])
cv2.imshow("Result",result)
cv2.waitKey(0)

2.2 视频流人脸模糊
from blur_face import blurred_face_gauss
from blur_face import  blurred_face_pixel
import numpy as np
import cv2
import argparse
import imutils


"""
#方法一
执行:
python test_video_blur_face.py 

#方法二
执行:
python test_video_blur_face.py --method pixel
"""

ap = argparse.ArgumentParser()
ap.add_argument("-m", "--method", type=str, default="gauss",
	choices=["gauss", "pixel"],
	help="gauss/pixel method")
args = vars(ap.parse_args())
# 模型路径
prototxtPath ="./model/deploy.prototxt"
weightsPath = "./model/res10_300x300_ssd_iter_140000.caffemodel"
#加载模型
net  = cv2.dnn.readNet(prototxtPath,weightsPath)

#获取视频图像
videoPath = "./input/test1.mp4"
vs = cv2.VideoCapture(videoPath)

#处理视频流
while True:
    (grabbed,frame) = vs.read()

    # 判断是否结束
    if not grabbed:
        print("无视频读取...")
        break
    frame = imutils.resize(frame,width=720)

    (h,w) = frame.shape[:2]
    # 构造blob
    blob = cv2.dnn.blobFromImage(frame,1.0,(300,300),
                                 (104,177,123))
    # 送入网络计算
    net.setInput(blob)
    detect = net.forward()
    # 检测
    for i in range(0,detect.shape[2]):
        confidence = detect[0,0,i,2]
        # 过滤掉小的置信度,计算坐标,提取面部roi
        if confidence > 0.5:
            box = detect[0,0,i,3:7]*np.array([w,h,w,h])
            (startX,startY,endX,endY) = box.astype("int")
            face = frame[startY:endY,startX:endX]

            #高斯方法
            if args["method"] == "gauss":
                face  = blurred_face_gauss(face,kernel_scale=3.0)

            # 像素模糊法
            else:
                face = blurred_face_pixel(face,pixel_blocks=20)

            frame[startY:endY,startX:endX] = face

    cv2.imshow("Result", frame)
    key = cv2.waitKey(1) & 0xFF

    if key == ord("q"):
        break
cv2.destroyAllWindows()
vs.release()

3.测试

3.1 单图测试

高斯模糊法:

在这里插入图片描述
图1

在这里插入图片描述
图2

像素模糊法:
在这里插入图片描述
图1

在这里插入图片描述
图2

3.2 视频流测试

方法一:

OpenCV之人脸模糊高斯法

视频地址:https://www.bilibili.com/video/BV1ti4y1t7Pp/

方法二:

OpenCV人脸模糊之像素模糊法

视频地址:https://www.bilibili.com/video/BV1x5411x74Y/

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

圆滚熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值