python - 试图模糊面部(python)
问题描述
我试图通过使用以下脚本来模糊面部,但最终会模糊整个图像。有关如何更改脚本以使其仅模糊面孔的任何建议?
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
img = cv2.imread('beatles.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detecting faces
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
# To show the detected faces
cv2.imshow('img',img)
cv2.waitKey(5000)
cv2.destroyAllWindows()
k = np.array(([1/9, 1/9, 1/9], [1/9, 1/9, 1/9], [1/9, 1/9, 1/9]), np.float32)
# Blurring of just the faces in a picture
skaldetlykkes = cv2.filter2D(img, -1, k)
cv2.imshow("Ladetskje", skaldetlykkes)
cv2.waitKey (5000)
cv2.destroyAllWindows()
解决方案
此代码示例
import cv2
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Read the input image
img = cv2.imread('test.jpg')
# Convert into grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Draw rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
# Display the output
cv2.imshow('img', img)
cv2.waitKey()
从人脸检测应该计算你想要模糊的矩形区域。
然后你只需要在该区域上应用一个平均函数:)(模糊 = 窗口平均值)
推荐阅读
- xquery - XQuery 将土耳其语字符的“ü”替换为“u”
- azure-devops - 在 Azure Devops 中的 nuget 源中使用 azure synapse c# notebook 中的 nuget 包
- android - 如何像我们在 androidleaback 组件的 VideoSupportFragment 中那样使用行转换自定义片段?
- python-3.x - python系统路径中“../../../”的含义
- reactjs - 如何在 apollo 客户端中检测订阅(websocket)的断开和重新连接
- c - 验证用户输入
- okhttp - 处理 OkHttp HTTP/2 REFUSED_STREAM 错误
- reactjs - 将后端文件托管到 IIS
- python - 如何在单元测试中从另一个模块捕获自定义异常?
- solr - 使用 SOLRNET 更新 solr 中的字段 - 字段数据更改