目錄
- 1. 效果圖
- 2. 原理
- 2.1 什么是人臉模糊,如何將其用于人臉匿名化?
- 2.2 執(zhí)行人臉模糊/匿名化的步驟
- 3. 源碼
- 3.1 圖像人臉模糊源碼
- 3.2 實時視頻流人臉模糊源碼
- 參考
這篇博客將介紹人臉檢測,然后使用Python,OpenCV模糊它們來“匿名化”每張圖像,以確保隱私得到保護,保證沒有人臉可以被識別如何使用。
并介紹倆種模糊的方法:簡單高斯模糊、像素模糊。
人臉模糊和匿名化的實際應用包括:
- 公共/私人區(qū)域的隱私和身份保護
- 在線保護兒童(即在上傳的照片中模糊未成年人的臉)
- 攝影新聞和新聞報道(如模糊未簽署棄權書的人的臉)
- 數(shù)據(jù)集管理和分發(fā)(如在數(shù)據(jù)集中匿名化個人)
1. 效果圖
原始圖 VS 簡單高斯模糊效果圖如下:
原始圖 VS 像素模糊效果圖如下:
在晚間新聞上看到的面部模糊正是像素模糊,主要是因為它比高斯模糊更“美觀”;
多人的也可以哦:原始圖 VS 簡單高斯模糊效果圖:
多人的也可以哦:原始圖 VS 像素模糊效果圖:
2. 原理
2.1 什么是人臉模糊,如何將其用于人臉匿名化?
人臉模糊是一種計算機視覺方法,用于對圖像和視頻中的人臉進行匿名化。
如上圖中人的身份是不可辨認的,通常使用面部模糊來幫助保護圖像中的人的身份。
2.2 執(zhí)行人臉模糊/匿名化的步驟
人臉檢測方法有很多,任選一種,進行圖像中的人臉檢測或者實時視頻流中人臉的檢測。人臉成功檢測后可使用以下倆種方式進行模糊。
- 使用高斯模糊對圖像和視頻流中的人臉進行匿名化
- 應用“像素模糊”效果來匿名化圖像和視頻中的人臉
應用OpenCV和計算機視覺進行人臉模糊包括四部分:
- 進行人臉檢測;(如Haar級聯(lián)、HOG線性向量機、基于深度學習的檢測);
- 提取ROI(Region Of Interests);
- 模糊/匿名化人臉;
- 將模糊的人臉存儲回原始圖像中(Numpy數(shù)組切片)。
3. 源碼
3.1 圖像人臉模糊源碼
# USAGE
# python blur_face.py --image examples/we.jpg --face face_detector
# python blur_face.py --image examples/we.jpg --face face_detector --method pixelated
# 使用OpenCV實現(xiàn)圖像中的人臉模糊
# 導入必要的包
import argparse
import os
import cv2
import imutils
import numpy as np
from pyimagesearch.face_blurring import anonymize_face_pixelate
from pyimagesearch.face_blurring import anonymize_face_simple
# 構(gòu)建命令行參數(shù)及解析
# --image 輸入人臉圖像
# --face 人臉檢測模型的目錄
# --method 使用簡單高斯模糊、像素模糊
# --blocks 面部分塊數(shù),默認20
# --confidence 面部檢測置信度,過濾弱檢測的值,默認50%
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to input image")
ap.add_argument("-f", "--face", required=True,
help="path to face detector model directory")
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())
# 加載基于Caffe的人臉檢測模型
# 從磁盤加載序列化的面部檢測模型及標簽文件
print("[INFO] loading face detector model...")
prototxtPath = os.path.sep.join([args["face"], "deploy.prototxt"])
weightsPath = os.path.sep.join([args["face"],
"res10_300x300_ssd_iter_140000.caffemodel"])
net = cv2.dnn.readNet(prototxtPath, weightsPath)
# 從此盤加載輸入圖像,獲取圖像維度
image = cv2.imread(args["image"])
image = imutils.resize(image, width=600)
orig = image.copy()
(h, w) = image.shape[:2]
# 預處理圖像,構(gòu)建圖像blob
blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300),
(104.0, 177.0, 123.0))
# 傳遞blob到網(wǎng)絡,并獲取面部檢測結(jié)果
print("[INFO] computing face detections...")
net.setInput(blob)
detections = net.forward()
# 遍歷人臉檢測結(jié)果
for i in range(0, detections.shape[2]):
# 提取檢測的置信度,即可能性
confidence = detections[0, 0, i, 2]
# 過濾弱檢測結(jié)果,確保均高于最小置信度
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 = image[startY:endY, startX:endX]
# 檢查是使用簡單高斯模糊 還是 像素模糊方法
if args["method"] == "simple":
face = anonymize_face_simple(face, factor=3.0)
# 否則應用像素匿名模糊方法
else:
face = anonymize_face_pixelate(face,
blocks=args["blocks"])
# 用模糊的匿名面部覆蓋圖像中的原始人臉ROI
image[startY:endY, startX:endX] = face
# 原始圖像和匿名圖像并排顯示
output = np.hstack([orig, image])
cv2.imshow("Origin VS " + str(args['method']), output)
cv2.waitKey(0)
3.2 實時視頻流人臉模糊源碼
# USAGE
# python blur_face_video.py --face face_detector
# python blur_face_video.py --face face_detector --method pixelated
# 導入必要的包
import argparse
import os
import time
import cv2
import imutils
import numpy as np
from imutils.video import VideoStream
from pyimagesearch.face_blurring import anonymize_face_pixelate
from pyimagesearch.face_blurring import anonymize_face_simple
# 構(gòu)建命令行參數(shù)及解析
# --face 人臉檢測模型的目錄
# --method 使用簡單高斯模糊、像素模糊
# --blocks 面部分塊數(shù),默認20
# --confidence 面部檢測置信度,過濾弱檢測的值,默認50%
ap = argparse.ArgumentParser()
ap.add_argument("-f", "--face", required=True,
help="path to face detector model directory")
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())
# 從磁盤加載訓練好的人臉檢測器Caffe模型
print("[INFO] loading face detector model...")
prototxtPath = os.path.sep.join([args["face"], "deploy.prototxt"])
weightsPath = os.path.sep.join([args["face"],
"res10_300x300_ssd_iter_140000.caffemodel"])
net = cv2.dnn.readNet(prototxtPath, weightsPath)
# 初始化視頻流,預熱傳感器2s
print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
time.sleep(2.0)
# 遍歷視頻流的每一幀
while True:
# 從線程化的視頻流獲取一幀,保持寬高比的縮放寬度為400px
frame = vs.read()
frame = imutils.resize(frame, width=400)
# 獲取幀的維度,預處理幀(構(gòu)建blob)
(h, w) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300),
(104.0, 177.0, 123.0))
# 傳遞blob到網(wǎng)絡并獲取面部檢測結(jié)果
net.setInput(blob)
detections = net.forward()
# 遍歷人臉檢測結(jié)果
for i in range(0, detections.shape[2]):
# 提取檢測的置信度,即可能性
confidence = detections[0, 0, i, 2]
# 過濾弱檢測結(jié)果,確保均高于最小置信度
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]
# 檢查是使用簡單高斯模糊 還是 像素模糊方法
if args["method"] == "simple":
face = anonymize_face_simple(face, factor=3.0)
# 否則應用像素匿名模糊方法
else:
face = anonymize_face_pixelate(face,
blocks=args["blocks"])
# 用模糊的匿名面部ROI覆蓋圖像中的原始人臉ROI
frame[startY:endY, startX:endX] = face
# 展示輸出幀
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) 0xFF
# 按下‘q'鍵,退出循環(huán)
if key == ord("q"):
break
# 做一些清理工作
# 關閉所有窗口,釋放視頻流指針
cv2.destroyAllWindows()
vs.stop()
參考
https://www.pyimagesearch.com/2020/04/06/blur-and-anonymize-faces-with-opencv-and-python/
到此這篇關于超詳細注釋之OpenCV實現(xiàn)視頻實時人臉模糊和人臉馬賽克的文章就介紹到這了,更多相關OpenCV人臉馬賽克內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 超詳細注釋之OpenCV dlib實現(xiàn)人臉采集
- 手把手教你利用opencv實現(xiàn)人臉識別功能(附源碼+文檔)
- opencv基于Haar人臉檢測和眼睛檢測
- OpenCV-Python實現(xiàn)人臉磨皮算法
- 基于Opencv制作的美顏相機帶你領略美顏特效的效果