使用 OpenCV 和 Python 在直播中模糊人脸
本文将学习如何使用 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/
defblur(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 函数
这是一个简单地为模糊图像添加像素化效果的函数。
defpixelate_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')
while1:
ret,frame = cap.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.5, 5)
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
defblur(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
defpixelate_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')
while1:
ret,frame = cap.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.5, 5)
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()
这就是你在直播中模糊面孔的方式!
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。