43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
import cv2
|
|
import numpy as np
|
|
from pdf2image import pdf2image
|
|
|
|
from vidocp.utils.detection import detect_large_coherent_structures
|
|
from vidocp.utils.display import show_mpl
|
|
from vidocp.utils.draw import draw_rectangles
|
|
from vidocp.utils.post_processing import remove_included
|
|
from vidocp.utils.filters import is_large_enough, has_acceptable_format
|
|
from vidocp.utils.text import remove_primary_text_regions
|
|
|
|
|
|
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 list(rects)
|
|
|
|
|
|
def detect_figures_in_pdf(pdf_path, page_index=1, show=True):
|
|
|
|
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)
|
|
|
|
if show:
|
|
show_mpl(page)
|
|
else:
|
|
return page
|