Refactoring: Move

Move page partitioners into partitioner module
This commit is contained in:
Matthias Bisping 2023-02-01 17:20:30 +01:00
parent a54ccb2fdf
commit cef97b33f9
3 changed files with 48 additions and 42 deletions

View File

@ -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

View File

@ -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)

View File

@ -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,