def test_img_conversion(): img = load_image(data_path("example.jpeg")) annotation = AnnotatedImage.from_image(img) image_np = annotation.to_numpy() assert isinstance(image_np, np.ndarray) assert image_np.shape == (375, 500, 3) image_pil = annotation.to_pillow() assert image_pil.width == 500 assert image_pil.height == 375
def check_image_equality(image: np.ndarray, path: str, color_mode="rgb", delta=None): reference = load_image(data_path(path), color_mode=color_mode) if delta is None: assert (image == reference).all() else: assert np.allclose(image[:, :], reference[:, :], rtol=delta)
def test_image_loader(): images = [ data_path("dataset/1.jpeg"), data_path("dataset/2.jpeg"), data_path("dataset/3.jpeg"), ] loader = ImageLoader(images) assert len(loader) == len(images) for (image_path, (path, img)) in zip(images, loader): assert image_path == path assert np.all(load_image(path) == img)
def test_overlay_masks(): masks = [ load_image(data_path(f"segmentation/color_masks/mask-{i}.png")) for i in range(4) ] background = np.zeros((100, 100, 3), dtype=np.uint8) check_image_equality( overlay_masks(background, masks, alpha=1.0), data_path("segmentation/color_masks/overlay-alpha-1.0.png"), delta=0.001, ) check_image_equality( overlay_masks(background, masks, alpha=0.5), data_path("segmentation/color_masks/overlay-alpha-0.5.png"), delta=0.001, )
def test_annotated_image_wrong_image_shape(): img = load_image(data_path("example.jpeg")) with pytest.raises(Exception): AnnotatedImage(image=img, size=(100, 100))
def test_annotated_image_shape_from_image(): img = load_image(data_path("example.jpeg")) annotation = AnnotatedImage.from_image(img) assert annotation.width == 500 assert annotation.height == 375
def test_load_image_normalize(): img = load_image(data_path("example.jpeg"), normalize=True) assert np.max(img) <= 1.0
def test_load_image_resize_non_uniform(): img = load_image(data_path("example.jpeg"), target_size=(256, 512)) assert img.shape == (512, 256, 3)
def test_load_image_resize(): img = load_image(data_path("example.jpeg"), target_size=(224, 224)) assert img.shape == (224, 224, 3)
def test_load_image_grayscale(): img = load_image(data_path("example.jpeg"), color_mode="grayscale") assert img.shape == (375, 500)
def test_load_image_bgr(): img2 = load_image(data_path("example.jpeg"), color_mode="bgr") check_image_equality(cv2.cvtColor(img2, cv2.COLOR_BGR2RGB), data_path("example.jpeg"))
def test_load_image_rgb(): img = load_image(data_path("example.jpeg")) assert img.shape == (375, 500, 3) assert np.max(img) > 1.0