专栏名称: 小白学视觉
本公众号主要介绍机器视觉基础知识和新闻,以及在学习机器视觉时遇到的各种纠结和坑的心路历程。
目录
相关文章推荐
清廉蓉城  ·  元宵丨一盏花灯一场喜 一碗元宵一团圆 ·  17 小时前  
植物星球  ·  蜡梅树下有很多小蜡梅苗,你要认识一下不 ·  2 天前  
成都本地宝  ·  成都哪里的火锅好吃? ·  3 天前  
成都本地宝  ·  免试入学,直升本科!2025想要提升学历的看 ... ·  2 天前  
51好读  ›  专栏  ›  小白学视觉

使用 OpenCV 和 Python 在直播中模糊人脸

小白学视觉  · 公众号  ·  · 2024-10-10 10:05

正文

点击上方 小白学视觉 ”,选择加" 星标 "或“ 置顶
重磅干货,第一时间送达
本文将学习如何使用 OpenCV 和 Python 在直播中模糊人脸。这将是一个非常有趣的博客,让我们开始吧!

我们最终结果的快照:

第 1 步:导入所需的库

  • 为图像操作导入 cv2
  • 为数组操作导入 Numpy
import cv2
import numpy as np

第 2 步:定义模糊函数

  • 这里我们定义了 Blur 函数。
  • 它需要 2 个参数,图像 img 和模糊因子 k 。
  • 然后我们通过将高度和宽度除以模糊因子来简单地计算内核高度和内核宽度。kw 和 kh 越小,模糊度越高。
  • 然后我们检查 kw 和 kh 是否为奇数,如果它们是偶数,则减 1 以使它们为奇数。
  • 然后简单地我们将高斯模糊应用于我们的图像并返回它。我们还可以应用任何其他模糊操作,请在此处阅读有关它们的更多信息: https://machinelearningprojects.net/blurrings-in-cv2/
def blur(img,k):
    h,w = img.shape[:2]
    kh,kw = h//k,w//k
    if kh%2==0:
        kh-=1
    if kw%2==0:
        kw-=1
    img = cv2.GaussianBlur(img,ksize=(kh,kw),sigmaX=0)
    return img

第 3 步:定义 pixelate_face 函数

  • 这是一个简单地为模糊图像添加像素化效果的函数。
def pixelate_face(image, blocks=10):
    # divide the input image into NxN blocks
    (h, w) = image.shape[:2]
    xSteps = np.linspace(0, w, blocks + 1, dtype="int")
    ySteps = np.linspace(0, h, blocks + 1, dtype="int")
    # loop over the blocks in both the x and y direction
    for i in range(1, len(ySteps)):
        for j in range(1, len(xSteps)):
            # compute the starting and ending (x, y)-coordinates
            # for the current block
            startX = xSteps[j - 1]
            startY = ySteps[i - 1]
            endX = xSteps[j]
            endY = ySteps[i]
            # extract the ROI using NumPy array slicing, compute the
            # mean of the ROI, and then draw a rectangle with the
            # mean RGB values over the ROI in the original image
            roi = image[startY:endY, startX:endX]
            (B, G, R) = [int(x) for x in cv2.mean(roi)[:3]]
            cv2.rectangle(image, (startX, startY), (endX, endY),
                (B, G, R), -1)
    # return the pixelated blurred image
    return image

第 4 步:让我们在实时提要中模糊面孔

  • 下面的代码是代码的主要部分。
  • 这里的 factor  定义了模糊量。
  • 定义一个级联分类器对象 face_cascade 来检测人脸。
  • 下载 haarcascade_frontalface_default.xml 文件
    • https://raw.githubusercontent.com/sharmaji27/face-blur-using-opencv/main/haarcascade_frontalface_default.xml
  • 然后简单地运行一个无限循环,从网络摄像头读取图像,检测其中的人脸,然后用像素化的人脸替换该人脸部分。
  • 阅读更多关于使用 HAARCASCADES 进行面部和眼睛检测的信息
    • https://machinelearningprojects.net/face-and-eye-detection-in-cv2/)
factor = 3    
cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

while 1:
    ret,frame = cap.read()
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.55)
    for (x,y,w,h) in faces:
        frame[y:y+h,x:x+w] = pixelate_face(blur(frame[y:y+h,x:x+w],factor))
    cv2.imshow('Live',frame)
    if cv2.waitKey(1)==27:
        break

cap.release()
cv2.destroyAllWindows()

让我们看看完整代码

import cv2
import numpy as np

def blur(img,k):
    h,w = img.shape[:2]
    kh,kw = h//k,w//k
    if kh%2==0:
        kh-=1
    if kw%2==0:
        kw-=1
    img = cv2.GaussianBlur(img,ksize=(kh,kw),sigmaX=0)
    return img
    
def pixelate_face(image, blocks=10):
    # divide the input image into NxN blocks
    (h, w) = image.shape[:2]
    xSteps = np.linspace(0, w, blocks + 1, dtype="int")
    ySteps = np.linspace(0, h, blocks + 1, dtype="int")
    # loop over the blocks in both the x and y direction
    for i in range(1, len(ySteps)):
        for j in range(1, len(xSteps)):
            # compute the starting and ending (x, y)-coordinates
            # for the current block
            startX = xSteps[j - 1]
            startY = ySteps[i - 1]
            endX = xSteps[j]
            endY = ySteps[i]
            # extract the ROI using NumPy array slicing, compute the
            # mean of the ROI, and then draw a rectangle with the
            # mean RGB values over the ROI in the original image
            roi = image[startY:endY, startX:endX]
            (B, G, R) = [int(x) for x in cv2.mean(roi)[:3]]
            cv2.rectangle(image, (startX, startY), (endX, endY),
                (B, G, R), -1)
    # return the pixelated blurred image
    return image
factor = 3    
cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

while 1:
    ret,frame = cap.read()
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    
    faces = face_cascade.detectMultiScale(gray, 1.55)
    for (x,y,w,h) in faces:
        frame[y:y+h,x:x+w] = pixelate_face(blur(frame[y:y+h,x:x+w],factor))
    cv2.imshow('Live',frame)
    if cv2.waitKey(1)==27:
        break

cap.release()
cv2.destroyAllWindows()
这就是你在直播中模糊面孔的方式!
下载1:OpenCV-Contrib扩展模块中文版教程






请到「今天看啥」查看全文