Skip to content

Commit

Permalink
fix image tests - we don't explode on bad bboxes these days
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Lindeman <[email protected]>
  • Loading branch information
HenryL27 committed Feb 21, 2025
1 parent faafd11 commit 6eb2cdb
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions lib/sycamore/sycamore/tests/integration/test_image_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pdf2image
from PIL import Image
from copy import deepcopy


import sycamore
Expand All @@ -27,18 +28,23 @@ def image_boxes() -> list[BoundingBox]:


@pytest.fixture(scope="module")
def source_image() -> Image.Image:
def source_image_module_scope() -> Image.Image:
images = pdf2image.convert_from_path(path)
return images[0].convert(mode="RGBA")


@pytest.fixture(scope="function")
def source_image(source_image_module_scope) -> Image.Image:
return deepcopy(source_image_module_scope)


# Checks that the image contains blue pixels. This is of course an imperfect check, but
# it at least will tell us if we drew some bounding boxes. on the image. Won't work
# if the image contains blue pixels to begin with. Image must have mode RGBA.
def check_image(image: Image.Image, expected_color=(0, 0, 255, 255)) -> None:
raw_colors = image.getcolors(64_000)
assert raw_colors is not None, "Image has too many colors to count"
assert expected_color in set((color_tup[1] for color_tup in raw_colors))
assert expected_color in set((color_tup[1] for color_tup in raw_colors)), "Did not draw boxes"


def test_draw_boxes_bbox(source_image, image_boxes) -> None:
Expand Down Expand Up @@ -86,11 +92,13 @@ def test_draw_boxes_dict(source_image, image_boxes) -> None:

def test_invalid_list(source_image, image_boxes):
boxes = [[b.coordinates] for b in image_boxes]
with pytest.raises(ValueError):
try_draw_boxes(source_image, boxes)
output: Image.Image = try_draw_boxes(source_image, boxes)
with pytest.raises(AssertionError, match=r".*Did not draw boxes.*"):
check_image(output)


def test_invalid_dict(source_image, image_boxes):
boxes = [{"bboxes": b.coordinates} for b in image_boxes]
with pytest.raises(ValueError):
try_draw_boxes(source_image, boxes)
output: Image.Image = try_draw_boxes(source_image, boxes)
with pytest.raises(AssertionError, match=r".*Did not draw boxes.*"):
check_image(output)

0 comments on commit 6eb2cdb

Please sign in to comment.