超詳細(xì)注釋之OpenCV實(shí)現(xiàn)視頻實(shí)時(shí)人臉模糊和人臉馬賽克
這篇博客將介紹人臉檢測(cè),然后使用Python,OpenCV模糊它們來“匿名化”每張圖像,以確保隱私得到保護(hù),保證沒有人臉可以被識(shí)別如何使用。
并介紹倆種模糊的方法:簡(jiǎn)單高斯模糊、像素模糊。
人臉模糊和匿名化的實(shí)際應(yīng)用包括:
- 公共/私人區(qū)域的隱私和身份保護(hù)
- 在線保護(hù)兒童(即在上傳的照片中模糊未成年人的臉)
- 攝影新聞和新聞報(bào)道(如模糊未簽署棄權(quán)書的人的臉)
- 數(shù)據(jù)集管理和分發(fā)(如在數(shù)據(jù)集中匿名化個(gè)人)
1. 效果圖
原始圖 VS 簡(jiǎn)單高斯模糊效果圖如下:
原始圖 VS 像素模糊效果圖如下:
在晚間新聞上看到的面部模糊正是像素模糊,主要是因?yàn)樗雀咚鼓:懊烙^”;
多人的也可以哦:原始圖 VS 簡(jiǎn)單高斯模糊效果圖:
多人的也可以哦:原始圖 VS 像素模糊效果圖:
2. 原理
2.1 什么是人臉模糊,如何將其用于人臉匿名化?
人臉模糊是一種計(jì)算機(jī)視覺方法,用于對(duì)圖像和視頻中的人臉進(jìn)行匿名化。
如上圖中人的身份是不可辨認(rèn)的,通常使用面部模糊來幫助保護(hù)圖像中的人的身份。
2.2 執(zhí)行人臉模糊/匿名化的步驟
人臉檢測(cè)方法有很多,任選一種,進(jìn)行圖像中的人臉檢測(cè)或者實(shí)時(shí)視頻流中人臉的檢測(cè)。人臉成功檢測(cè)后可使用以下倆種方式進(jìn)行模糊。
- 使用高斯模糊對(duì)圖像和視頻流中的人臉進(jìn)行匿名化
- 應(yīng)用“像素模糊”效果來匿名化圖像和視頻中的人臉
應(yīng)用OpenCV和計(jì)算機(jī)視覺進(jìn)行人臉模糊包括四部分:
- 進(jìn)行人臉檢測(cè);(如Haar級(jí)聯(lián)、HOG線性向量機(jī)、基于深度學(xué)習(xí)的檢測(cè));
- 提取ROI(Region Of Interests);
- 模糊/匿名化人臉;
- 將模糊的人臉存儲(chǔ)回原始圖像中(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實(shí)現(xiàn)圖像中的人臉模糊 # 導(dǎo)入必要的包 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 人臉檢測(cè)模型的目錄 # --method 使用簡(jiǎn)單高斯模糊、像素模糊 # --blocks 面部分塊數(shù),默認(rèn)20 # --confidence 面部檢測(cè)置信度,過濾弱檢測(cè)的值,默認(rèn)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的人臉檢測(cè)模型 # 從磁盤加載序列化的面部檢測(cè)模型及標(biāo)簽文件 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] # 預(yù)處理圖像,構(gòu)建圖像blob blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0)) # 傳遞blob到網(wǎng)絡(luò),并獲取面部檢測(cè)結(jié)果 print("[INFO] computing face detections...") net.setInput(blob) detections = net.forward() # 遍歷人臉檢測(cè)結(jié)果 for i in range(0, detections.shape[2]): # 提取檢測(cè)的置信度,即可能性 confidence = detections[0, 0, i, 2] # 過濾弱檢測(cè)結(jié)果,確保均高于最小置信度 if confidence > args["confidence"]: # 計(jì)算人臉的邊界框(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] # 檢查是使用簡(jiǎn)單高斯模糊 還是 像素模糊方法 if args["method"] == "simple": face = anonymize_face_simple(face, factor=3.0) # 否則應(yīng)用像素匿名模糊方法 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 實(shí)時(shí)視頻流人臉模糊源碼
# USAGE # python blur_face_video.py --face face_detector # python blur_face_video.py --face face_detector --method pixelated # 導(dǎo)入必要的包 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 人臉檢測(cè)模型的目錄 # --method 使用簡(jiǎn)單高斯模糊、像素模糊 # --blocks 面部分塊數(shù),默認(rèn)20 # --confidence 面部檢測(cè)置信度,過濾弱檢測(cè)的值,默認(rèn)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()) # 從磁盤加載訓(xùn)練好的人臉檢測(cè)器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) # 初始化視頻流,預(yù)熱傳感器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) # 獲取幀的維度,預(yù)處理幀(構(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)絡(luò)并獲取面部檢測(cè)結(jié)果 net.setInput(blob) detections = net.forward() # 遍歷人臉檢測(cè)結(jié)果 for i in range(0, detections.shape[2]): # 提取檢測(cè)的置信度,即可能性 confidence = detections[0, 0, i, 2] # 過濾弱檢測(cè)結(jié)果,確保均高于最小置信度 if confidence > args["confidence"]: # 計(jì)算人臉的邊界框(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] # 檢查是使用簡(jiǎn)單高斯模糊 還是 像素模糊方法 if args["method"] == "simple": face = anonymize_face_simple(face, factor=3.0) # 否則應(yīng)用像素匿名模糊方法 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 # 做一些清理工作 # 關(guān)閉所有窗口,釋放視頻流指針 cv2.destroyAllWindows() vs.stop()
參考
https://www.pyimagesearch.com/2020/04/06/blur-and-anonymize-faces-with-opencv-and-python/
到此這篇關(guān)于超詳細(xì)注釋之OpenCV實(shí)現(xiàn)視頻實(shí)時(shí)人臉模糊和人臉馬賽克的文章就介紹到這了,更多相關(guān)OpenCV人臉馬賽克內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。