42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
from functools import partial
|
|
|
|
import cv2
|
|
import numpy as np
|
|
|
|
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.filters import (
|
|
is_large_enough,
|
|
has_acceptable_format,
|
|
is_not_too_large,
|
|
)
|
|
from cv_analysis.utils.post_processing import remove_included
|
|
from cv_analysis.utils.structures import Rectangle
|
|
|
|
|
|
def make_figure_detection_pipeline(min_area=5000, max_width_to_height_ratio=6):
|
|
def pipeline(image: np.array):
|
|
max_area = image.shape[0] * image.shape[1] * 0.99
|
|
filter_cnts = make_filter_likely_figures(min_area, max_area, max_width_to_height_ratio)
|
|
|
|
image = remove_primary_text_regions(image)
|
|
cnts = detect_large_coherent_structures(image)
|
|
cnts = filter_cnts(cnts)
|
|
|
|
rects = remove_included(map(cv2.boundingRect, cnts))
|
|
rectangles = map(Rectangle.from_xywh, rects)
|
|
return rectangles
|
|
|
|
return pipeline
|
|
|
|
|
|
def make_filter_likely_figures(min_area, max_area, max_width_to_height_ratio):
|
|
def is_likely_figure(cnts):
|
|
return (
|
|
is_not_too_large(cnts, max_area)
|
|
and is_large_enough(cnts, min_area)
|
|
and has_acceptable_format(cnts, max_width_to_height_ratio)
|
|
)
|
|
|
|
return partial(filter, is_likely_figure)
|