From cef97b33f920488857c308e6ebcbc5a309de4b20 Mon Sep 17 00:00:00 2001 From: Matthias Bisping Date: Wed, 1 Feb 2023 17:20:30 +0100 Subject: [PATCH] Refactoring: Move Move page partitioners into partitioner module --- synthesis/partitioner/random.py | 22 ++++++++++++++ synthesis/partitioner/two_column.py | 25 ++++++++++++++++ test/fixtures/page_generation/page.py | 43 +-------------------------- 3 files changed, 48 insertions(+), 42 deletions(-) create mode 100644 synthesis/partitioner/random.py create mode 100644 synthesis/partitioner/two_column.py diff --git a/synthesis/partitioner/random.py b/synthesis/partitioner/random.py new file mode 100644 index 0000000..fe2ec9d --- /dev/null +++ b/synthesis/partitioner/random.py @@ -0,0 +1,22 @@ +from cv_analysis.utils.rectangle import Rectangle +from synthesis.partitioner.page_partitioner import PagePartitioner +from synthesis.random import rnd + + +class RandomPagePartitioner(PagePartitioner): + def __init__(self): + super().__init__() + + def generate_content_boxes(self, box: Rectangle, depth=0): + if depth >= self.max_recursion_depth: + yield box + else: + child_boxes = self.generate_child_boxes( + box, + axis=rnd.choice(["x", "y"]), + split_percentage=rnd.uniform(0.3, 0.7), + ) + if self.recurse(depth): + yield from (self.generate_content_boxes(b, depth + 1) for b in child_boxes) + else: + yield child_boxes diff --git a/synthesis/partitioner/two_column.py b/synthesis/partitioner/two_column.py new file mode 100644 index 0000000..02cde9c --- /dev/null +++ b/synthesis/partitioner/two_column.py @@ -0,0 +1,25 @@ +from cv_analysis.utils.rectangle import Rectangle +from synthesis.partitioner.page_partitioner import PagePartitioner +from synthesis.random import rnd + + +class TwoColumnPagePartitioner(PagePartitioner): + def __init__(self): + super().__init__() + self.max_recursion_depth = 3 + + def generate_content_boxes(self, box: Rectangle, depth=0): + if depth >= self.max_recursion_depth: + yield box + + else: + if depth == 0: + axis = "x" + split_percentage = 0.5 + else: + axis = "y" + split_percentage = rnd.choice([0.3, 0.7]) + + child_boxes = self.generate_child_boxes(box, axis=axis, split_percentage=split_percentage) + + yield from (self.generate_content_boxes(b, depth + 1) for b in child_boxes) diff --git a/test/fixtures/page_generation/page.py b/test/fixtures/page_generation/page.py index c50baf5..6bd7049 100644 --- a/test/fixtures/page_generation/page.py +++ b/test/fixtures/page_generation/page.py @@ -26,7 +26,7 @@ from cv_analysis.utils.image_operations import blur, sharpen, overlay, superimpo from cv_analysis.utils.merging import merge_related_rectangles from cv_analysis.utils.postprocessing import remove_overlapping, remove_included from cv_analysis.utils.spacial import area -from synthesis.partitioner.page_partitioner import PagePartitioner +from synthesis.partitioner.two_column import TwoColumnPagePartitioner from synthesis.random import rnd from synthesis.segment.content_rectangle import ContentRectangle from synthesis.segment.random_content_rectangle import RandomContentRectangle @@ -1008,47 +1008,6 @@ def paste_contents(page, contents: Iterable[ContentRectangle]): return page -class RandomPagePartitioner(PagePartitioner): - def __init__(self): - super().__init__() - - def generate_content_boxes(self, box: Rectangle, depth=0): - if depth >= self.max_recursion_depth: - yield box - else: - child_boxes = self.generate_child_boxes( - box, - axis=rnd.choice(["x", "y"]), - split_percentage=rnd.uniform(0.3, 0.7), - ) - if self.recurse(depth): - yield from (self.generate_content_boxes(b, depth + 1) for b in child_boxes) - else: - yield child_boxes - - -class TwoColumnPagePartitioner(PagePartitioner): - def __init__(self): - super().__init__() - self.max_recursion_depth = 3 - - def generate_content_boxes(self, box: Rectangle, depth=0): - if depth >= self.max_recursion_depth: - yield box - - else: - if depth == 0: - axis = "x" - split_percentage = 0.5 - else: - axis = "y" - split_percentage = rnd.choice([0.3, 0.7]) - - child_boxes = self.generate_child_boxes(box, axis=axis, split_percentage=split_percentage) - - yield from (self.generate_content_boxes(b, depth + 1) for b in child_boxes) - - @pytest.fixture( params=[ TwoColumnPagePartitioner,