Skip to content

Commit

Permalink
Merge pull request #7 from stefanklut/actions/black
Browse files Browse the repository at this point in the history
Format Python code with psf/black push
  • Loading branch information
MMaas3 committed Dec 14, 2023
2 parents 8878c88 + e1c390b commit b27a13a
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 171 deletions.
2 changes: 1 addition & 1 deletion page_xml/output_pageXML.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(
cfg: Optional[CfgNode] = None,
whitelist: Optional[Iterable[str]] = None,
rectangle_regions: Optional[list[str]] = [],
min_region_size: int = 10
min_region_size: int = 10,
) -> None:
"""
Class for the generation of the pageXML from class predictions on images
Expand Down
287 changes: 117 additions & 170 deletions test/test_output_pageXML.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,9 @@


class TestOutputPageXML(unittest.TestCase):

def test_one_region_type(self):
output = tempfile.mktemp("_laypa_test")
xml = OutputPageXML(
"region",
output,
5,
["Photo"],
[],
["ImageRegion:Photo"],
None,
[]

)
xml = OutputPageXML("region", output, 5, ["Photo"], [], ["ImageRegion:Photo"], None, [])
background = (np.full((10, 10), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) <= 5) * 1
image = (np.full((10, 10), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) > 5) * 1
array = np.array([background, image])
Expand All @@ -46,17 +35,7 @@ def test_one_region_type(self):

def test_multiple_region_types(self):
output = tempfile.mktemp("_laypa_test")
xml = OutputPageXML(
"region",
output,
5,
["Photo", "Text"],
[],
["ImageRegion:Photo", "TextRegion:Text"],
None,
[]

)
xml = OutputPageXML("region", output, 5, ["Photo", "Text"], [], ["ImageRegion:Photo", "TextRegion:Text"], None, [])
background = (np.full((10, 10), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) <= 2) * 1
text = (np.full((10, 10), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) > 5) * 1
image = ((text | background) == 0) * 1
Expand All @@ -80,27 +59,21 @@ def test_multiple_region_types(self):

def test_region_not_square(self):
output = tempfile.mktemp("_laypa_test")
xml = OutputPageXML(
"region",
output,
5,
["Photo"],
[],
["ImageRegion:Photo"],
None,
[]

xml = OutputPageXML("region", output, 5, ["Photo"], [], ["ImageRegion:Photo"], None, [])
background = np.array(
[
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
]
)
background = np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
image = np.invert(background == 1) * 1
array = np.array([background, image])
tensor = torch.from_numpy(array)
Expand All @@ -117,28 +90,21 @@ def test_region_not_square(self):

def test_rectangle_region_does_cotains_4_points(self):
output = tempfile.mktemp("_laypa_test")
xml = OutputPageXML(
"region",
output,
5,
["Photo"],
[],
["ImageRegion:Photo"],
None,
[],
["Photo"]

xml = OutputPageXML("region", output, 5, ["Photo"], [], ["ImageRegion:Photo"], None, [], ["Photo"])
background = np.array(
[
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
]
)
background = np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
image = np.invert(background == 1) * 1
array = np.array([background, image])
tensor = torch.from_numpy(array)
Expand All @@ -156,28 +122,21 @@ def test_rectangle_region_does_cotains_4_points(self):

def test_rectangle_region_does_create_floating_point_coords(self):
output = tempfile.mktemp("_laypa_test")
xml = OutputPageXML(
"region",
output,
5,
["Photo"],
[],
["ImageRegion:Photo"],
None,
[],
["Photo"]

xml = OutputPageXML("region", output, 5, ["Photo"], [], ["ImageRegion:Photo"], None, [], ["Photo"])
background = np.array(
[
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
]
)
background = np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 0, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
image = np.invert(background == 1) * 1
array = np.array([background, image])
tensor = torch.from_numpy(array)
Expand All @@ -196,49 +155,52 @@ def test_rectangle_region_does_create_floating_point_coords(self):
def test_only_rectangle_region_one_type(self):
output = tempfile.mktemp("_laypa_test")
xml = OutputPageXML(
"region",
output,
5,
["Photo", "Text"],
[],
["ImageRegion:Photo", "TextRegion:Text"],
None,
[],
["Photo"]
"region", output, 5, ["Photo", "Text"], [], ["ImageRegion:Photo", "TextRegion:Text"], None, [], ["Photo"]
)
background = np.array(
[
[1, 1, 0, 0, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 1, 1, 1, 1, 1],
[1, 1, 0, 0, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 0, 0, 1, 1],
[1, 1, 1, 1, 1, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 0, 0, 0, 0, 1],
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, 0, 0, 0],
[0, 1, 1, 1, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

text = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 0, 0]])
array = np.array([background, image, text])
tensor = torch.from_numpy(array)

Expand All @@ -251,8 +213,7 @@ def test_only_rectangle_region_one_type(self):
image_coords_elements = page.findall("./page:Page/page:ImageRegion/page:Coords", namespaces=namespaces)
self.assertEqual(1, len(image_coords_elements))
image_coord_points = image_coords_elements[0].attrib.get("points")
self.assertEqual(4, image_coord_points.count(","),
f"ImageRegion Contains more then 4 points: '{image_coord_points}'")
self.assertEqual(4, image_coord_points.count(","), f"ImageRegion Contains more then 4 points: '{image_coord_points}'")
text_coords_elements = page.findall("./page:Page/page:TextRegion/page:Coords", namespaces=namespaces)
self.assertEqual(1, len(text_coords_elements))
text_coord_points = text_coords_elements[0].attrib.get("points")
Expand All @@ -261,28 +222,21 @@ def test_only_rectangle_region_one_type(self):
@unittest.skip("Not enough time/priority for implementation")
def test_merge_overlapping_squares(self):
output = tempfile.mktemp("_laypa_test")
xml = OutputPageXML(
"region",
output,
5,
["Photo"],
[],
["ImageRegion:Photo"],
None,
[],
["Photo"]

xml = OutputPageXML("region", output, 5, ["Photo"], [], ["ImageRegion:Photo"], None, [], ["Photo"])
background = np.array(
[
[1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 0, 0, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 1, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 1, 1, 1, 1],
[1, 0, 0, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
]
)
background = np.array([[1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 0, 0, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 1, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 1, 1, 1, 1],
[1, 0, 0, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
image = np.invert(background == 1) * 1
array = np.array([background, image])
tensor = torch.from_numpy(array)
Expand All @@ -298,28 +252,21 @@ def test_merge_overlapping_squares(self):

def test_ignores_too_small_regions(self):
output = tempfile.mktemp("_laypa_test")
xml = OutputPageXML(
"region",
output,
5,
["Photo"],
[],
["ImageRegion:Photo"],
None,
[],
["Photo"],
10
xml = OutputPageXML("region", output, 5, ["Photo"], [], ["ImageRegion:Photo"], None, [], ["Photo"], 10)
background = np.array(
[
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
]
)
background = np.array([[0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])

image = np.invert(background == 1) * 1
array = np.array([background, image])
Expand Down

0 comments on commit b27a13a

Please sign in to comment.