39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
from functools import partial
|
|
|
|
import numpy as np
|
|
from funcy import lmap
|
|
|
|
from cv_analysis.figure_detection.figures import detect_large_coherent_structures
|
|
from cv_analysis.figure_detection.text import remove_primary_text_regions
|
|
from cv_analysis.utils.conversion import contour_to_rectangle
|
|
from cv_analysis.utils.filters import (
|
|
is_large_enough,
|
|
has_acceptable_format,
|
|
is_small_enough,
|
|
)
|
|
from cv_analysis.utils.postprocessing import remove_included
|
|
|
|
|
|
def detect_figures(image: np.array):
|
|
max_area = image.shape[0] * image.shape[1] * 0.99
|
|
min_area = 5000
|
|
max_width_to_height_ratio = 6
|
|
figure_filter = partial(is_likely_figure, min_area, max_area, max_width_to_height_ratio)
|
|
|
|
image = remove_primary_text_regions(image)
|
|
contours = detect_large_coherent_structures(image)
|
|
contours = filter(figure_filter, contours)
|
|
|
|
rectangles = lmap(contour_to_rectangle, contours)
|
|
rectangles = remove_included(rectangles)
|
|
|
|
return rectangles
|
|
|
|
|
|
def is_likely_figure(min_area, max_area, max_width_to_height_ratio, contours):
|
|
return (
|
|
is_small_enough(contours, max_area)
|
|
and is_large_enough(contours, min_area)
|
|
and has_acceptable_format(contours, max_width_to_height_ratio)
|
|
)
|