45 lines
1.2 KiB
Python
45 lines
1.2 KiB
Python
from typing import List
|
|
|
|
from surya.common.polygon import PolygonBox
|
|
|
|
|
|
def clean_boxes(boxes: List[PolygonBox]) -> List[PolygonBox]:
|
|
new_boxes = []
|
|
for box_obj in boxes:
|
|
xs = [point[0] for point in box_obj.polygon]
|
|
ys = [point[1] for point in box_obj.polygon]
|
|
if max(xs) == min(xs) or max(ys) == min(ys):
|
|
continue
|
|
|
|
box = box_obj.bbox
|
|
contained = False
|
|
for other_box_obj in boxes:
|
|
if other_box_obj.polygon == box_obj.polygon:
|
|
continue
|
|
|
|
other_box = other_box_obj.bbox
|
|
if box == other_box:
|
|
continue
|
|
if (
|
|
box[0] >= other_box[0]
|
|
and box[1] >= other_box[1]
|
|
and box[2] <= other_box[2]
|
|
and box[3] <= other_box[3]
|
|
):
|
|
contained = True
|
|
break
|
|
if not contained:
|
|
new_boxes.append(box_obj)
|
|
return new_boxes
|
|
|
|
|
|
def expand_bbox(bbox, expansion_factor=0.01):
|
|
expansion_low = 1 - expansion_factor
|
|
expansion_high = 1 + expansion_factor
|
|
return [
|
|
bbox[0] * expansion_low,
|
|
bbox[1] * expansion_low,
|
|
bbox[2] * expansion_high,
|
|
bbox[3] * expansion_high,
|
|
]
|