佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > 超詳細(xì)注釋之OpenCV實(shí)現(xiàn)視頻實(shí)時(shí)人臉模糊和人臉馬賽克

超詳細(xì)注釋之OpenCV實(shí)現(xiàn)視頻實(shí)時(shí)人臉模糊和人臉馬賽克

熱門標(biāo)簽:網(wǎng)站文章發(fā)布 檢查注冊(cè)表項(xiàng) 智能手機(jī) 美圖手機(jī) 呼叫中心市場(chǎng)需求 銀行業(yè)務(wù) 鐵路電話系統(tǒng) 服務(wù)器配置

這篇博客將介紹人臉檢測(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)行人臉模糊包括四部分:

  1. 進(jìn)行人臉檢測(cè);(如Haar級(jí)聯(lián)、HOG線性向量機(jī)、基于深度學(xué)習(xí)的檢測(cè));
  2. 提取ROI(Region Of Interests);
  3. 模糊/匿名化人臉;
  4. 將模糊的人臉存儲(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)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 超詳細(xì)注釋之OpenCV dlib實(shí)現(xiàn)人臉采集
  • 手把手教你利用opencv實(shí)現(xiàn)人臉識(shí)別功能(附源碼+文檔)
  • opencv基于Haar人臉檢測(cè)和眼睛檢測(cè)
  • OpenCV-Python實(shí)現(xiàn)人臉磨皮算法
  • 基于Opencv制作的美顏相機(jī)帶你領(lǐng)略美顏特效的效果

標(biāo)簽:沈陽 紅河 長治 上海 河南 新疆 樂山 滄州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《超詳細(xì)注釋之OpenCV實(shí)現(xiàn)視頻實(shí)時(shí)人臉模糊和人臉馬賽克》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    青岛市| 正阳县| 东乌珠穆沁旗| 容城县| 迁安市| 承德市| 穆棱市| 丽水市| 盐源县| 牙克石市| 明星| 汪清县| 马关县| 扬州市| 麻栗坡县| 庆元县| 淳化县| 嘉峪关市| 靖安县| 西华县| 大冶市| 寿光市| 玛多县| 吴旗县| 略阳县| 平罗县| 兴宁市| 徐汇区| 宿州市| 大城县| 施秉县| 安龙县| 威信县| 汉川市| 隆化县| 桂阳县| 土默特右旗| 三原县| 秦安县| 沙田区| 高阳县|