Skip to content

fix errors in validate polygons (temp solution) #286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions mindocr/data/transforms/general_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,30 +314,33 @@ class ValidatePolygons:
"""
def __init__(self, min_area: float = 1.0):
self._min_area = min_area
#self.fix_when_invalid = fix_when_invalid

def __call__(self, data: dict):
size = data.get('actual_size', np.array(data['image'].shape[:2]))[::-1] # convert to x, y coord
border = box(0, 0, *size)

new_polys, new_texts, new_tags = [], [], []
for np_poly, text, ignore in zip(data['polys'], data['texts'], data['ignore_tags']):
if ((0 <= np_poly) & (np_poly < size)).all(): # if the polygon is fully within the image
poly = Polygon(np_poly)
if (not poly.is_valid) or (poly.is_empty):
#poly = poly.buffer(0)
continue

elif ((0 <= np_poly) & (np_poly < size)).all(): # if the polygon is fully within the image
new_polys.append(np_poly)

else:
poly = Polygon(np_poly)
if poly.intersects(border): # if the polygon is partially within the image
poly = poly.intersection(border)
if poly.area < self._min_area:
ignore = True

poly = poly.exterior
poly = poly.coords[::-1] if poly.is_ccw else poly.coords # sort in clockwise order
new_polys.append(np.array(poly[:-1]))

else: # the polygon is fully outside the image
continue

new_tags.append(ignore)
new_texts.append(text)

Expand Down