mirror of https://github.com/open-mmlab/mmocr.git
[Fix] Fix RandomCrop
parent
fe43259a05
commit
58c59e80dd
|
@ -13,7 +13,7 @@ from shapely.geometry import Polygon as plg
|
||||||
|
|
||||||
import mmocr.core.evaluation.utils as eval_utils
|
import mmocr.core.evaluation.utils as eval_utils
|
||||||
from mmocr.registry import TRANSFORMS
|
from mmocr.registry import TRANSFORMS
|
||||||
from mmocr.utils import (bbox2poly, crop_polygon, is_poly_outside_rect,
|
from mmocr.utils import (bbox2poly, crop_polygon, is_poly_inside_rect,
|
||||||
poly2bbox, rescale_polygon)
|
poly2bbox, rescale_polygon)
|
||||||
from .wrappers import ImgAug
|
from .wrappers import ImgAug
|
||||||
|
|
||||||
|
@ -888,7 +888,7 @@ class RandomCrop(BaseTransform):
|
||||||
for idx, poly in enumerate(polys):
|
for idx, poly in enumerate(polys):
|
||||||
poly = poly.reshape(-1, 2)
|
poly = poly.reshape(-1, 2)
|
||||||
poly = poly - (crop_x, crop_y)
|
poly = poly - (crop_x, crop_y)
|
||||||
if not is_poly_outside_rect(poly, [0, 0, crop_w, crop_h]):
|
if is_poly_inside_rect(poly.flatten(), [0, 0, crop_w, crop_h]):
|
||||||
valid_polys.append(poly.flatten())
|
valid_polys.append(poly.flatten())
|
||||||
valid_labels.append(labels[idx])
|
valid_labels.append(labels[idx])
|
||||||
valid_ignored.append(ignored[idx])
|
valid_ignored.append(ignored[idx])
|
||||||
|
@ -905,7 +905,8 @@ class RandomCrop(BaseTransform):
|
||||||
for bbox in bboxes:
|
for bbox in bboxes:
|
||||||
bbox = bbox.reshape(-1, 2)
|
bbox = bbox.reshape(-1, 2)
|
||||||
bbox = bbox - (crop_x, crop_y)
|
bbox = bbox - (crop_x, crop_y)
|
||||||
if not is_poly_outside_rect(bbox, [0, 0, crop_w, crop_h]):
|
if is_poly_inside_rect(
|
||||||
|
bbox2poly(bbox.flatten()), [0, 0, crop_w, crop_h]):
|
||||||
valid_bboxes.append(bbox.flatten())
|
valid_bboxes.append(bbox.flatten())
|
||||||
assert (len(valid_bboxes) == len(valid_polys))
|
assert (len(valid_bboxes) == len(valid_polys))
|
||||||
results['gt_bboxes'] = np.array(valid_bboxes).astype(np.float32)
|
results['gt_bboxes'] = np.array(valid_bboxes).astype(np.float32)
|
||||||
|
|
|
@ -14,7 +14,7 @@ from .img_util import drop_orientation, is_not_png
|
||||||
from .lmdb_util import recog2lmdb
|
from .lmdb_util import recog2lmdb
|
||||||
from .logger import get_root_logger
|
from .logger import get_root_logger
|
||||||
from .model import revert_sync_batchnorm
|
from .model import revert_sync_batchnorm
|
||||||
from .polygon_utils import (crop_polygon, is_poly_outside_rect, poly2bbox,
|
from .polygon_utils import (crop_polygon, is_poly_inside_rect, poly2bbox,
|
||||||
poly2shapely, poly_intersection, poly_iou,
|
poly2shapely, poly_intersection, poly_iou,
|
||||||
poly_make_valid, poly_union, polys2shapely,
|
poly_make_valid, poly_union, polys2shapely,
|
||||||
rescale_polygon, rescale_polygons)
|
rescale_polygon, rescale_polygons)
|
||||||
|
@ -29,7 +29,7 @@ __all__ = [
|
||||||
'stitch_boxes_into_lines', 'StringStrip', 'revert_sync_batchnorm',
|
'stitch_boxes_into_lines', 'StringStrip', 'revert_sync_batchnorm',
|
||||||
'bezier_to_polygon', 'sort_points', 'recog2lmdb', 'dump_ocr_data',
|
'bezier_to_polygon', 'sort_points', 'recog2lmdb', 'dump_ocr_data',
|
||||||
'recog_anno_to_imginfo', 'rescale_polygons', 'rescale_polygon',
|
'recog_anno_to_imginfo', 'rescale_polygons', 'rescale_polygon',
|
||||||
'rescale_bboxes', 'bbox2poly', 'crop_polygon', 'is_poly_outside_rect',
|
'rescale_bboxes', 'bbox2poly', 'crop_polygon', 'is_poly_inside_rect',
|
||||||
'poly2bbox', 'poly_intersection', 'poly_iou', 'poly_make_valid',
|
'poly2bbox', 'poly_intersection', 'poly_iou', 'poly_make_valid',
|
||||||
'poly_union', 'poly2shapely', 'polys2shapely', 'register_all_modules'
|
'poly_union', 'poly2shapely', 'polys2shapely', 'register_all_modules'
|
||||||
]
|
]
|
||||||
|
|
|
@ -260,19 +260,17 @@ def poly_iou(poly_a: Polygon,
|
||||||
return area_inters / area_union if area_union != 0 else zero_division
|
return area_inters / area_union if area_union != 0 else zero_division
|
||||||
|
|
||||||
|
|
||||||
def is_poly_outside_rect(poly: ArrayLike, rect: np.ndarray) -> bool:
|
def is_poly_inside_rect(poly: ArrayLike, rect: np.ndarray) -> bool:
|
||||||
"""Check if the polygon is outside the target region.
|
"""Check if the polygon is inside the target region.
|
||||||
Args:
|
Args:
|
||||||
poly (ArrayLike): Polygon in shape (N, ).
|
poly (ArrayLike): Polygon in shape (N, ).
|
||||||
rect (ndarray): Target region [x1, y1, x2, y2].
|
rect (ndarray): Target region [x1, y1, x2, y2].
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
bool: Whether the polygon is outside the cropping region.
|
bool: Whether the polygon is inside the cropping region.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
poly = np.array(poly).reshape(-1, 2)
|
poly = poly2shapely(poly)
|
||||||
if poly[:, 0].max() < rect[0] or poly[:, 0].min() > rect[2]:
|
rect = poly2shapely(bbox2poly(rect))
|
||||||
return True
|
inter = poly.intersection(rect)
|
||||||
if poly[:, 1].max() < rect[1] or poly[:, 1].min() > rect[3]:
|
return inter.area == poly.area
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
Loading…
Reference in New Issue