refactoring
This commit is contained in:
parent
0976971117
commit
1ffc9dcc68
@ -12,143 +12,6 @@ from image_prediction.transformer.transformers.coordinate.pdfnet import PDFNetCo
|
|||||||
from test.conftest import array_to_image, add_image
|
from test.conftest import array_to_image, add_image
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def coordinate_test_image():
|
|
||||||
return array_to_image(np.zeros(shape=(100, 100, 3)))
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def coordinate_test_fpdf():
|
|
||||||
pdf = fpdf.FPDF(unit="pt", format=(300, 300))
|
|
||||||
return pdf
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(params=["top_left", "bottom_left", "bottom_right", "top_right"])
|
|
||||||
def corner(request):
|
|
||||||
return request.param
|
|
||||||
|
|
||||||
|
|
||||||
def set_coords(metadata, x1, y1, x2, y2):
|
|
||||||
metadata.update({Info.X1: x1, Info.Y1: y1, Info.X2: x2, Info.Y2: y2})
|
|
||||||
|
|
||||||
|
|
||||||
def get_fpdf_coordinates(corner):
|
|
||||||
"""Origin top left, y1 <= y2; all coords on page are positive
|
|
||||||
(0,0)--+--(2,0)--+
|
|
||||||
|////| |////|
|
|
||||||
+--(1,1) +--(3,1)
|
|
||||||
|
|
||||||
(0,2)--+ (2,2)--+
|
|
||||||
|////| |////|
|
|
||||||
+--(1,3) +--(3,3)
|
|
||||||
"""
|
|
||||||
metadata = base_position_metadata()
|
|
||||||
|
|
||||||
if corner == "top_left":
|
|
||||||
set_coords(metadata, 0, 0, 100, 100)
|
|
||||||
|
|
||||||
elif corner == "bottom_left":
|
|
||||||
set_coords(metadata, 0, 200, 100, 300)
|
|
||||||
|
|
||||||
elif corner == "bottom_right":
|
|
||||||
set_coords(metadata, 200, 200, 300, 300)
|
|
||||||
|
|
||||||
elif corner == "top_right":
|
|
||||||
set_coords(metadata, 200, 0, 300, 100)
|
|
||||||
|
|
||||||
else:
|
|
||||||
raise ValueError(f"No coordinates specified for corner {corner}.")
|
|
||||||
|
|
||||||
return metadata
|
|
||||||
|
|
||||||
|
|
||||||
def get_fitz_coordinates(corner):
|
|
||||||
return get_fpdf_coordinates(corner)
|
|
||||||
|
|
||||||
|
|
||||||
def get_pdfnet_coordinates(corner):
|
|
||||||
"""Origin bottom left, y1 <= y2; all coords on page are positive
|
|
||||||
+---(1,3) +--(3,3)
|
|
||||||
|////| |////|
|
|
||||||
(0,2)--+ (2,2)--+
|
|
||||||
|
|
||||||
+--(1,1) +--(3,1)
|
|
||||||
|////| |////|
|
|
||||||
(0,0)--+ (2,0)--+
|
|
||||||
"""
|
|
||||||
metadata = base_position_metadata()
|
|
||||||
|
|
||||||
if corner == "top_left":
|
|
||||||
set_coords(metadata, 0, 200, 100, 300)
|
|
||||||
|
|
||||||
elif corner == "bottom_left":
|
|
||||||
set_coords(metadata, 0, 0, 100, 100)
|
|
||||||
|
|
||||||
elif corner == "bottom_right":
|
|
||||||
set_coords(metadata, 200, 0, 300, 100)
|
|
||||||
|
|
||||||
elif corner == "top_right":
|
|
||||||
set_coords(metadata, 200, 200, 300, 300)
|
|
||||||
|
|
||||||
else:
|
|
||||||
raise ValueError(f"No coordinates specified for corner {corner}.")
|
|
||||||
|
|
||||||
return metadata
|
|
||||||
|
|
||||||
|
|
||||||
def base_position_metadata(width=100, height=100):
|
|
||||||
return {
|
|
||||||
Info.X1: 0,
|
|
||||||
Info.Y1: 0,
|
|
||||||
Info.X2: width,
|
|
||||||
Info.Y2: height,
|
|
||||||
Info.WIDTH: width,
|
|
||||||
Info.HEIGHT: height,
|
|
||||||
Info.PAGE_IDX: 0,
|
|
||||||
Info.PAGE_WIDTH: 300,
|
|
||||||
Info.PAGE_HEIGHT: 300,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(params=["list", "dict"])
|
|
||||||
def coordinate_type(request):
|
|
||||||
return request.param
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def position_metadata_in_given_system(corner, coordinate_system, coordinate_type):
|
|
||||||
if coordinate_system == "fpdf":
|
|
||||||
coordinates = get_fpdf_coordinates(corner)
|
|
||||||
|
|
||||||
elif coordinate_system == "fitz":
|
|
||||||
coordinates = get_fitz_coordinates(corner)
|
|
||||||
|
|
||||||
elif coordinate_system == "pdfnet":
|
|
||||||
coordinates = get_pdfnet_coordinates(corner)
|
|
||||||
|
|
||||||
else:
|
|
||||||
raise ValueError(f"Unknown coordinate system: {coordinate_system}")
|
|
||||||
|
|
||||||
return [coordinates, coordinates] if coordinate_type == "list" else coordinates
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def position_metadata_in_reference_system(corner, coordinate_type):
|
|
||||||
coordinates = get_fpdf_coordinates(corner)
|
|
||||||
return [coordinates, coordinates] if coordinate_type == "list" else coordinates
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def coordinate_test_pdf(position_metadata_in_given_system, coordinate_test_image, coordinate_test_fpdf):
|
|
||||||
add_image(coordinate_test_fpdf, ImageMetadataPair(coordinate_test_image, position_metadata_in_given_system))
|
|
||||||
return coordinate_test_fpdf.output(dest="S").encode("latin1")
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def coordinate_test_page_image(coordinate_test_pdf):
|
|
||||||
return pdf2image.convert_from_bytes(coordinate_test_pdf)[0]
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("coordinate_system", ["fpdf"])
|
@pytest.mark.parametrize("coordinate_system", ["fpdf"])
|
||||||
def test_fpdf_coordinate_transformer(position_metadata_in_given_system, position_metadata_in_reference_system):
|
def test_fpdf_coordinate_transformer(position_metadata_in_given_system, position_metadata_in_reference_system):
|
||||||
"""We use FPDF's coordinate system as the reference system (arbitrarily). Hence, FPDFCoordinateTransformer
|
"""We use FPDF's coordinate system as the reference system (arbitrarily). Hence, FPDFCoordinateTransformer
|
||||||
@ -194,10 +57,134 @@ def test_pdfnet_coordinate_transformer(position_metadata_in_given_system, positi
|
|||||||
transformer = PDFNetCoordinateTransformer()
|
transformer = PDFNetCoordinateTransformer()
|
||||||
|
|
||||||
assert transformer.forward(position_metadata_in_reference_system) == position_metadata_in_given_system
|
assert transformer.forward(position_metadata_in_reference_system) == position_metadata_in_given_system
|
||||||
|
|
||||||
assert transformer.backward(position_metadata_in_given_system) == position_metadata_in_reference_system
|
assert transformer.backward(position_metadata_in_given_system) == position_metadata_in_reference_system
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
compose(transformer.backward, transformer.forward)(position_metadata_in_reference_system)
|
compose(transformer.backward, transformer.forward)(position_metadata_in_reference_system)
|
||||||
== position_metadata_in_reference_system
|
== position_metadata_in_reference_system
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def position_metadata_in_given_system(corner, corner2metadata_in_given_system):
|
||||||
|
return corner2metadata_in_given_system[corner]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def position_metadata_in_reference_system(corner, corner2metadata_in_reference_system):
|
||||||
|
return corner2metadata_in_reference_system[corner]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(params=["top_left", "bottom_left", "bottom_right", "top_right"])
|
||||||
|
def corner(request):
|
||||||
|
return request.param
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def corner2metadata_in_given_system(coordinate_system):
|
||||||
|
if coordinate_system == "fpdf":
|
||||||
|
return get_fpdf_corner_metadat()
|
||||||
|
|
||||||
|
elif coordinate_system == "fitz":
|
||||||
|
return get_fitz_corner_metadat()
|
||||||
|
|
||||||
|
elif coordinate_system == "pdfnet":
|
||||||
|
return get_pdfnet_corner_metadata()
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Unknown coordinate system: {coordinate_system}")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def corner2metadata_in_reference_system():
|
||||||
|
return get_fpdf_corner_metadat()
|
||||||
|
|
||||||
|
|
||||||
|
def get_fpdf_corner_metadat():
|
||||||
|
"""Origin top left, y1 <= y2; all coords on page are positive
|
||||||
|
(0,0)--+--(2,0)--+
|
||||||
|
|////| |////|
|
||||||
|
+--(1,1) +--(3,1)
|
||||||
|
|
||||||
|
(0,2)--+ (2,2)--+
|
||||||
|
|////| |////|
|
||||||
|
+--(1,3) +--(3,3)
|
||||||
|
"""
|
||||||
|
return {
|
||||||
|
"top_left": get_metadata_for_coords(0, 0, 100, 100),
|
||||||
|
"bottom_left": get_metadata_for_coords(0, 200, 100, 300),
|
||||||
|
"bottom_right": get_metadata_for_coords(200, 200, 300, 300),
|
||||||
|
"top_right": get_metadata_for_coords(200, 0, 300, 100)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_fitz_corner_metadat():
|
||||||
|
return get_fpdf_corner_metadat()
|
||||||
|
|
||||||
|
|
||||||
|
def get_pdfnet_corner_metadata():
|
||||||
|
"""Origin bottom left, y1 <= y2; all coords on page are positive
|
||||||
|
+---(1,3) +--(3,3)
|
||||||
|
|////| |////|
|
||||||
|
(0,2)--+ (2,2)--+
|
||||||
|
|
||||||
|
+--(1,1) +--(3,1)
|
||||||
|
|////| |////|
|
||||||
|
(0,0)--+ (2,0)--+
|
||||||
|
"""
|
||||||
|
return {
|
||||||
|
"top_left": get_metadata_for_coords(0, 200, 100, 300),
|
||||||
|
"bottom_left": get_metadata_for_coords(0, 0, 100, 100),
|
||||||
|
"bottom_right": get_metadata_for_coords(200, 0, 300, 100),
|
||||||
|
"top_right": get_metadata_for_coords(200, 200, 300, 300),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_metadata_for_coords(x1, y1, x2, y2):
|
||||||
|
metadata = base_position_metadata()
|
||||||
|
metadata.update({Info.X1: x1, Info.Y1: y1, Info.X2: x2, Info.Y2: y2})
|
||||||
|
return metadata
|
||||||
|
|
||||||
|
|
||||||
|
def base_position_metadata(width=100, height=100):
|
||||||
|
return {
|
||||||
|
Info.X1: 0,
|
||||||
|
Info.Y1: 0,
|
||||||
|
Info.X2: width,
|
||||||
|
Info.Y2: height,
|
||||||
|
Info.WIDTH: width,
|
||||||
|
Info.HEIGHT: height,
|
||||||
|
Info.PAGE_IDX: 0,
|
||||||
|
Info.PAGE_WIDTH: 300,
|
||||||
|
Info.PAGE_HEIGHT: 300,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# utils not needed for tests but for inferring new coordinates systems:
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def coordinate_test_pdf(position_metadata_in_given_system, coordinate_test_image, coordinate_test_fpdf):
|
||||||
|
add_image(coordinate_test_fpdf, ImageMetadataPair(coordinate_test_image, position_metadata_in_given_system))
|
||||||
|
return coordinate_test_fpdf.output(dest="S").encode("latin1")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def coordinate_test_image():
|
||||||
|
return array_to_image(np.zeros(shape=(100, 100, 3)))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def coordinate_test_fpdf():
|
||||||
|
pdf = fpdf.FPDF(unit="pt", format=(300, 300))
|
||||||
|
return pdf
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def coordinate_test_page_image(coordinate_test_pdf):
|
||||||
|
return pdf2image.convert_from_bytes(coordinate_test_pdf)[0]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(params=["list", "dict"])
|
||||||
|
def coordinate_type(request):
|
||||||
|
return request.param
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user