超詳細(xì)注釋之OpenCV實現(xiàn)視頻實時人臉模糊和人臉馬賽克
這篇博客將介紹人臉檢測,然后使用Python,OpenCV模糊它們來“匿名化”每張圖像,以確保隱私得到保護(hù),保證沒有人臉可以被識別如何使用。
并介紹倆種模糊的方法:簡單高斯模糊、像素模糊。
人臉模糊和匿名化的實際應(yīng)用包括:
- 公共/私人區(qū)域的隱私和身份保護(hù)
- 在線保護(hù)兒童(即在上傳的照片中模糊未成年人的臉)
- 攝影新聞和新聞報道(如模糊未簽署棄權(quán)書的人的臉)
- 數(shù)據(jù)集管理和分發(fā)(如在數(shù)據(jù)集中匿名化個人)
1. 效果圖
原始圖 VS 簡單高斯模糊效果圖如下:
原始圖 VS 像素模糊效果圖如下:
在晚間新聞上看到的面部模糊正是像素模糊,主要是因為它比高斯模糊更“美觀”;
多人的也可以哦:原始圖 VS 簡單高斯模糊效果圖:
多人的也可以哦:原始圖 VS 像素模糊效果圖:
2. 原理
2.1 什么是人臉模糊,如何將其用于人臉匿名化?
人臉模糊是一種計算機(jī)視覺方法,用于對圖像和視頻中的人臉進(jìn)行匿名化。
如上圖中人的身份是不可辨認(rèn)的,通常使用面部模糊來幫助保護(hù)圖像中的人的身份。
2.2 執(zhí)行人臉模糊/匿名化的步驟
人臉檢測方法有很多,任選一種,進(jìn)行圖像中的人臉檢測或者實時視頻流中人臉的檢測。人臉成功檢測后可使用以下倆種方式進(jìn)行模糊。
- 使用高斯模糊對圖像和視頻流中的人臉進(jìn)行匿名化
- 應(yīng)用“像素模糊”效果來匿名化圖像和視頻中的人臉
應(yīng)用OpenCV和計算機(jī)視覺進(jìn)行人臉模糊包括四部分:
- 進(jìn)行人臉檢測;(如Haar級聯(lián)、HOG線性向量機(jī)、基于深度學(xué)習(xí)的檢測);
- 提取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)圖像中的人臉模糊 # 導(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 人臉檢測模型的目錄 # --method 使用簡單高斯模糊、像素模糊 # --blocks 面部分塊數(shù),默認(rèn)20 # --confidence 面部檢測置信度,過濾弱檢測的值,默認(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的人臉檢測模型 # 從磁盤加載序列化的面部檢測模型及標(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ò),并獲取面部檢測結(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) # 否則應(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 實時視頻流人臉模糊源碼
# 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 人臉檢測模型的目錄 # --method 使用簡單高斯模糊、像素模糊 # --blocks 面部分塊數(shù),默認(rèn)20 # --confidence 面部檢測置信度,過濾弱檢測的值,默認(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)練好的人臉檢測器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ò)并獲取面部檢測結(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) # 否則應(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實現(xiàn)視頻實時人臉模糊和人臉馬賽克的文章就介紹到這了,更多相關(guān)OpenCV人臉馬賽克內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Series的index的方法和屬性使用說明
這篇文章主要介紹了關(guān)于Series的index的方法和屬性使用說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06Starship定制shell提示符實現(xiàn)信息自由
這篇文章主要介紹了Starship定制shell提示符的實現(xiàn),讓你需要的所有信息觸手可及,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03python實現(xiàn)輸入三角形邊長自動作圖求面積案例
這篇文章主要介紹了python實現(xiàn)輸入三角形邊長自動作圖求面積案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04python腳本生成caffe train_list.txt的方法
下面小編就為大家分享一篇python腳本生成caffe train_list.txt的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04