[WIP] Refactoring meta-detection
This commit is contained in:
parent
b540cfd0f2
commit
557d091a54
@ -19,7 +19,6 @@ from cv_analysis.utils.rectangle import Rectangle
|
||||
def parse_layout(image: np.array):
|
||||
|
||||
rectangles = find_segments(image)
|
||||
|
||||
rectangles = remove_included(rectangles)
|
||||
rectangles = lmap(rectangle_to_box, rectangles)
|
||||
rectangles = connect_related_rects2(rectangles)
|
||||
@ -31,25 +30,27 @@ def parse_layout(image: np.array):
|
||||
|
||||
def find_segments(image, meta=1):
|
||||
|
||||
if meta:
|
||||
original = image.copy()
|
||||
image = normalize_to_gray_scale(image)
|
||||
image = dilate_page_components(image)
|
||||
preprocess = compose(dilate_page_components, normalize_to_gray_scale)
|
||||
|
||||
contours, hierarchies = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
||||
mask1 = map(is_likely_segment, contours)
|
||||
mask2 = map(has_no_parent, hierarchies[0])
|
||||
mask = map(__and__, mask1, mask2)
|
||||
contours = compress(contours, mask)
|
||||
contours, hierarchies = find_contours(preprocess(image) if meta else image)
|
||||
likely_segments = map(is_likely_segment, contours)
|
||||
without_parents = map(has_no_parent, hierarchies[0])
|
||||
valid_contours = map(__and__, likely_segments, without_parents)
|
||||
contours = compress(contours, valid_contours)
|
||||
rectangles = lmap(compose(box_to_rectangle, cv2.boundingRect), contours)
|
||||
|
||||
if meta:
|
||||
image = meta_detection(original, rectangles)
|
||||
rectangles = find_segments(image, 0)
|
||||
preprocessed = meta_detection(image.copy(), rectangles)
|
||||
rectangles = find_segments(preprocessed, 0)
|
||||
|
||||
return rectangles
|
||||
|
||||
|
||||
def find_contours(image):
|
||||
contours, hierarchies = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
||||
return contours, hierarchies
|
||||
|
||||
|
||||
def is_likely_segment(rect, min_area=100):
|
||||
return cv2.contourArea(rect, False) > min_area
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user