Adding padding in address zone elements
Ref: https://redmine.teklia.com/issues/9130
I trained a yolo model to make orientation classification on address zone elements. However the training has been made with padded updated images taken from the base images of the elements with the following code :
Padding function
def add_padding(image, min_size=640):
# Get current dimensions
width, height = image.size
# Ensure the image is in RGB mode
if image.mode != 'RGB':
image = image.convert('RGB')
# Determine the scaling factor to make the longest side equal to min_size
scale_factor = min_size / max(width, height)
# Resize the image with the new scale factor
new_width = int(width * scale_factor)
new_height = int(height * scale_factor)
resized_image = image.resize((new_width, new_height), Image.Resampling.LANCZOS)
# Calculate padding to make the other side equal to min_size
pad_width = max(min_size - new_width, 0)
pad_height = max(min_size - new_height, 0)
# Apply padding with the correct fill color (white in this case)
padded_image = ImageOps.expand(resized_image, border=(
pad_width // 2, pad_height // 2,
pad_width - (pad_width // 2), pad_height - (pad_height // 2)
), fill=0)
return padded_image
which delivers such images (multiple images are plotted here):
Add an optional user_configuration argument (padding_size: bool = False
) that triggers that behaviour, updating the image before passing it to the classifier. Image_size will be used to know how much we need to pad.
Supporting it on the segmentation model is a much harder work so only do it for the classifier.
Edited by Yoann Schneider