Issue
My preprocessing_function detects and blurs faces. How to plot images from ImageDataGenerator to make sure that it works? The code is below:
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",
)
Edit: I used this block of code to check the images
images, labels=next(train_generator)
print(batch[0].shape)
images=batch[0][0]
print (images.shape)
plt.imshow(image.astype(np.uint8))
Solution
well you need to provide some images to the generator using .flow or.flow_from_directory or .flow_from_dataframe. For example train_gen=datagen.flow_from_directory( etc) Then try
images, labels=next(train_gen)
images will be of the shape (batch_size, height, width, channels) plot the images to see if you get what you expect. Be aware the preprocessing_function must return an image of the SAME dimensions as you specified in target_size and must have the same number of channels as specified by color_mode.
Answered By - Gerry P
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.