Skip to main content

Prepare Images

📝 Prepare Images

  • Reads all images and their masks.
  • Generate masks if they don't exist.
  • Populate our Images data structure.
@timeit
def prepare_images(create_mask = True, **kwargs) -> Images:
image_set_name = kwargs['image_set_name']
folder_path = f"../data/{image_set_name}"
images: Images = Images([], folder_path.split("/")[-1])
files: list[str] = list(
filter(
lambda file: ".jpg" in file, os.listdir(f"{folder_path}/images")
)
)
if create_mask:
for file in files:
image_path = f"{folder_path}/images/{file}"
rgb_image = OpenCV.cvtColor(OpenCV.imread(image_path), OpenCV.COLOR_BGR2RGB)
gray_image = OpenCV.cvtColor(rgb_image, OpenCV.COLOR_RGB2GRAY)
mask = remove(rgb_image)
mask = OpenCV.cvtColor(mask, OpenCV.COLOR_RGB2GRAY)
mask[mask > 0] = 255
OpenCV.imwrite(f"{folder_path}/masks/{file}", mask)
kernel = np.ones((5, 5), np.uint8)
dilated_mask = OpenCV.dilate(mask, kernel, iterations=20)
images.images.append(Image(file.split(".")[0], rgb_image, gray_image, dilated_mask, [], [], image_path))
else:
for file in files:
image_path = f"{folder_path}/images/{file}"
mask_path = f"{folder_path}/masks/{file}"
rgb_image = OpenCV.cvtColor(OpenCV.imread(image_path), OpenCV.COLOR_BGR2RGB)
gray_image = OpenCV.cvtColor(rgb_image, OpenCV.COLOR_RGB2GRAY)
mask = OpenCV.imread(mask_path, OpenCV.IMREAD_GRAYSCALE)
kernel = np.ones((5, 5), np.uint8)
dilated_mask = OpenCV.dilate(mask, kernel, iterations=20)
images.images.append(Image(file.split(".")[0], rgb_image, gray_image, dilated_mask, [], [], image_path))
return images