36 lines
1023 B
Python
36 lines
1023 B
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)
|