gpt4 book ai didi

python - 尝试仅模糊脸部(python)

转载 作者:太空宇宙 更新时间:2023-11-03 20:20:22 25 4
gpt4 key购买 nike

我尝试使用以下脚本仅模糊脸部,但最终模糊了整个图像。关于如何更改脚本以仅模糊脸部的任何建议?

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)

# Blur the faces, i.e. only the ROIs defined by faces
k = np.array(([1/9, 1/9, 1/9], [1/9, 1/9, 1/9], [1/9, 1/9, 1/9]), np.float32)
for (x, y, w, h) in faces:
img[y:y+h,x:x+w] = cv2.filter2D(img[y:y+h,x:x+w], -1, k)

# Display the output
cv2.imshow('img', img)
cv2.waitKey()

关于python - 尝试仅模糊脸部(python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58201365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com