【发布时间】:2021-07-18 07:00:25
【问题描述】:
我的 preprocessing_function 检测并模糊面部。如何从 ImageDataGenerator 绘制图像以确保其正常工作?代码如下:
haarcascades_loc = "libopencv-4.0.1-hbb9e17c_0/Library/etc/haarcascades/haarcascade_profileface.xml"
pface = cv2.CascadeClassifier(haarcascades_loc)
def BlurFaces(image):
gray_fr = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray_fr = np.array(gray_fr, dtype='uint8')
faces = pface.detectMultiScale(gray_fr, 1.3, 5)
for (x, y, w, h) in faces:
blur_face = image[y:y+h, x:x+w]
blur_face = cv2.GaussianBlur(blur_face,(23, 23), 30)
image[y:y+blur_face.shape[0], x:x+blur_face.shape[1]] = blur_face
return image
datagen = ImageDataGenerator(validation_split=0.20, preprocessing_function=BlurFaces)
train_generator = datagen.flow_from_directory(
directory=r"State Farm Distracted Driver Detection\imgs\train",
target_size=(224, 224),
color_mode="rgb",
batch_size=128, #32, 64, 128, 256 or 512
class_mode="categorical",
shuffle=True,
seed=42,
subset="training",
)
valid_generator = datagen.flow_from_directory(
directory=r"State Farm Distracted Driver Detection\imgs\train",
target_size=(224, 224),
color_mode="rgb",
batch_size=128,
class_mode="categorical",
shuffle=True,
seed=42,
subset="validation",
)
编辑:我使用这段代码检查图像
images, labels=next(train_generator)
print(batch[0].shape)
images=batch[0][0]
print (images.shape)
plt.imshow(image.astype(np.uint8))
【问题讨论】:
-
您尝试绘制一些图像以查看它是否有效?
标签: python image tensorflow opencv keras