cv-analysis-service/vidocp/figure_detection.py
Matthias Bisping 9d30009dce refactoring
2022-02-06 14:45:53 +01:00

43 lines
1.0 KiB
Python

import cv2
import numpy as np
from pdf2image import pdf2image
from vidocp.utils import (
show_mpl,
draw_rectangles,
remove_included,
remove_primary_text_regions,
detect_large_coherent_structures,
is_large_enough,
has_acceptable_format,
)
def is_likely_figure(cont, min_area=5000, max_width_to_hight_ratio=6):
return is_large_enough(cont, min_area) and has_acceptable_format(cont, max_width_to_hight_ratio)
def detect_figures(image: np.array):
image = image.copy()
image = remove_primary_text_regions(image)
cnts = detect_large_coherent_structures(image)
cnts = filter(is_likely_figure, cnts)
rects = map(cv2.boundingRect, cnts)
rects = remove_included(rects)
return rects
def detect_figures_in_pdf(pdf_path, page_index=1):
page = pdf2image.convert_from_path(pdf_path, first_page=page_index + 1, last_page=page_index + 1)[0]
page = np.array(page)
redaction_contours = detect_figures(page)
page = draw_rectangles(page, redaction_contours)
show_mpl(page)