mirror of
https://github.com/open-mmlab/mmocr.git
synced 2025-06-03 21:54:47 +08:00
[Utils] Migrate datasets/pepelines/box_utils.py
This commit is contained in:
parent
ef98df8052
commit
77e29adb7b
@ -22,6 +22,13 @@ mmocr/models/textdet/detectors/single_stage_text_detector.py
|
||||
|
||||
# It will be removed after all utils are moved to mmocr.utils
|
||||
mmocr/core/evaluation/utils.py
|
||||
mmocr/models/textdet/postprocessors/utils.py
|
||||
mmocr/utils/bbox_utils.py
|
||||
mmocr/datasets/pipelines/crop.py
|
||||
|
||||
# It will be removed after all models have been refactored
|
||||
mmocr/utils/ocr.py
|
||||
mmocr/datasets/kie_dataset.py
|
||||
|
||||
|
||||
# It will be removed after all models have been refactored
|
||||
|
@ -1,5 +1,4 @@
|
||||
# Copyright (c) OpenMMLab. All rights reserved.
|
||||
from .box_utils import sort_vertex, sort_vertex8
|
||||
from .formatting import PackTextDetInputs, PackTextRecogInputs
|
||||
from .kie_transforms import ResizeNoImg
|
||||
from .loading import LoadOCRAnnotations
|
||||
@ -22,10 +21,10 @@ __all__ = [
|
||||
'ToTensorOCR', 'DBNetTargets', 'PANetTargets', 'RandomRotate',
|
||||
'ScaleAspectJitter', 'MultiRotateAugOCR', 'OCRSegTargets', 'FancyPCA',
|
||||
'RandomPaddingOCR', 'ImgAug', 'RandomRotateImageBox', 'OpencvToPil',
|
||||
'PilToOpencv', 'SourceImagePad', 'TextSnakeTargets', 'sort_vertex',
|
||||
'sort_vertex8', 'FCENetTargets', 'TextDetRandomCropFlip', 'ResizeNoImg',
|
||||
'PyramidRescale', 'TorchVisionWrapper', 'Resize', 'RandomCrop',
|
||||
'TextDetRandomCrop', 'RandomCrop', 'PackTextDetInputs',
|
||||
'PackTextRecogInputs', 'RescaleToHeight', 'PadToWidth',
|
||||
'ShortScaleAspectJitter', 'RandomFlip', 'BoundedScaleAspectJitter'
|
||||
'PilToOpencv', 'SourceImagePad', 'TextSnakeTargets', 'FCENetTargets',
|
||||
'TextDetRandomCropFlip', 'ResizeNoImg', 'PyramidRescale',
|
||||
'TorchVisionWrapper', 'Resize', 'RandomCrop', 'TextDetRandomCrop',
|
||||
'RandomCrop', 'PackTextDetInputs', 'PackTextRecogInputs',
|
||||
'RescaleToHeight', 'PadToWidth', 'ShortScaleAspectJitter', 'RandomFlip',
|
||||
'BoundedScaleAspectJitter'
|
||||
]
|
||||
|
@ -1,53 +0,0 @@
|
||||
# Copyright (c) OpenMMLab. All rights reserved.
|
||||
import numpy as np
|
||||
|
||||
import mmocr.utils as utils
|
||||
|
||||
|
||||
def sort_vertex(points_x, points_y):
|
||||
"""Sort box vertices in clockwise order from left-top first.
|
||||
|
||||
Args:
|
||||
points_x (list[float]): x of four vertices.
|
||||
points_y (list[float]): y of four vertices.
|
||||
Returns:
|
||||
sorted_points_x (list[float]): x of sorted four vertices.
|
||||
sorted_points_y (list[float]): y of sorted four vertices.
|
||||
"""
|
||||
assert utils.is_type_list(points_x, (float, int))
|
||||
assert utils.is_type_list(points_y, (float, int))
|
||||
assert len(points_x) == 4
|
||||
assert len(points_y) == 4
|
||||
vertices = np.stack((points_x, points_y), axis=-1).astype(np.float32)
|
||||
vertices = _sort_vertex(vertices)
|
||||
sorted_points_x = list(vertices[:, 0])
|
||||
sorted_points_y = list(vertices[:, 1])
|
||||
return sorted_points_x, sorted_points_y
|
||||
|
||||
|
||||
def _sort_vertex(vertices):
|
||||
assert vertices.ndim == 2
|
||||
assert vertices.shape[-1] == 2
|
||||
N = vertices.shape[0]
|
||||
if N == 0:
|
||||
return vertices
|
||||
|
||||
center = np.mean(vertices, axis=0)
|
||||
directions = vertices - center
|
||||
angles = np.arctan2(directions[:, 1], directions[:, 0])
|
||||
sort_idx = np.argsort(angles)
|
||||
vertices = vertices[sort_idx]
|
||||
|
||||
left_top = np.min(vertices, axis=0)
|
||||
dists = np.linalg.norm(left_top - vertices, axis=-1, ord=2)
|
||||
lefttop_idx = np.argmin(dists)
|
||||
indexes = (np.arange(N, dtype=np.int) + lefttop_idx) % N
|
||||
return vertices[indexes]
|
||||
|
||||
|
||||
def sort_vertex8(points):
|
||||
"""Sort vertex with 8 points [x1 y1 x2 y2 x3 y3 x4 y4]"""
|
||||
assert len(points) == 8
|
||||
vertices = _sort_vertex(np.array(points, dtype=np.float32).reshape(-1, 2))
|
||||
sorted_box = list(vertices.flatten())
|
||||
return sorted_box
|
@ -4,7 +4,6 @@ import numpy as np
|
||||
from shapely.geometry import LineString, Point
|
||||
|
||||
import mmocr.utils as utils
|
||||
from .box_utils import sort_vertex
|
||||
|
||||
|
||||
def box_jitter(points_x, points_y, jitter_ratio_x=0.5, jitter_ratio_y=0.1):
|
||||
@ -56,7 +55,7 @@ def warp_img(src_img,
|
||||
points_x = [min(max(x, 0), w) for x in box[0:8:2]]
|
||||
points_y = [min(max(y, 0), h) for y in box[1:9:2]]
|
||||
|
||||
points_x, points_y = sort_vertex(points_x, points_y)
|
||||
points_x, points_y = utils.sort_vertex(points_x, points_y)
|
||||
|
||||
if jitter_flag:
|
||||
box_jitter(
|
||||
|
@ -3,7 +3,7 @@ from mmcv.utils import Registry, build_from_cfg
|
||||
|
||||
from .api_utils import disable_text_recog_aug_test
|
||||
from .bbox_utils import (bbox2poly, box_center_distance, box_diag,
|
||||
rescale_bboxes)
|
||||
rescale_bboxes, sort_vertex, sort_vertex8)
|
||||
from .box_util import (bezier_to_polygon, is_on_same_line, sort_points,
|
||||
stitch_boxes_into_lines)
|
||||
from .check_argument import (equal_len, is_2dlist, is_3dlist, is_none_or_type,
|
||||
@ -36,8 +36,9 @@ __all__ = [
|
||||
'rescale_bboxes', 'bbox2poly', 'crop_polygon', 'is_poly_inside_rect',
|
||||
'poly2bbox', 'poly_intersection', 'poly_iou', 'poly_make_valid',
|
||||
'poly_union', 'poly2shapely', 'polys2shapely', 'register_all_modules',
|
||||
'dist_points2line', 'offset_polygon', 'disable_text_recog_aug_test',
|
||||
'box_center_distance', 'box_diag', 'compute_hmean', 'filter_2dlist_result',
|
||||
'many2one_match_ic13', 'one2one_match_ic13', 'select_top_boundary',
|
||||
'point_distance', 'points_center', 'boundary_iou'
|
||||
'dist_points2line', 'offset_polygon', 'sort_vertex8', 'sort_vertex',
|
||||
'disable_text_recog_aug_test', 'box_center_distance', 'box_diag',
|
||||
'compute_hmean', 'filter_2dlist_result', 'many2one_match_ic13',
|
||||
'one2one_match_ic13', 'select_top_boundary', 'boundary_iou',
|
||||
'point_distance', 'points_center'
|
||||
]
|
||||
|
@ -4,6 +4,7 @@ from typing import Tuple
|
||||
import numpy as np
|
||||
from numpy.typing import ArrayLike
|
||||
|
||||
from mmocr.utils.check_argument import is_type_list
|
||||
from mmocr.utils.point_utils import point_distance, points_center
|
||||
|
||||
|
||||
@ -78,6 +79,58 @@ def bbox2poly(bbox: ArrayLike) -> np.array:
|
||||
])
|
||||
|
||||
|
||||
def sort_vertex(points_x, points_y):
|
||||
# TODO Add typehints & docstring & test
|
||||
"""Sort box vertices in clockwise order from left-top first.
|
||||
|
||||
Args:
|
||||
points_x (list[float]): x of four vertices.
|
||||
points_y (list[float]): y of four vertices.
|
||||
Returns:
|
||||
sorted_points_x (list[float]): x of sorted four vertices.
|
||||
sorted_points_y (list[float]): y of sorted four vertices.
|
||||
"""
|
||||
assert is_type_list(points_x, (float, int))
|
||||
assert is_type_list(points_y, (float, int))
|
||||
assert len(points_x) == 4
|
||||
assert len(points_y) == 4
|
||||
vertices = np.stack((points_x, points_y), axis=-1).astype(np.float32)
|
||||
vertices = _sort_vertex(vertices)
|
||||
sorted_points_x = list(vertices[:, 0])
|
||||
sorted_points_y = list(vertices[:, 1])
|
||||
return sorted_points_x, sorted_points_y
|
||||
|
||||
|
||||
def _sort_vertex(vertices):
|
||||
# TODO Add typehints & docstring & test
|
||||
assert vertices.ndim == 2
|
||||
assert vertices.shape[-1] == 2
|
||||
N = vertices.shape[0]
|
||||
if N == 0:
|
||||
return vertices
|
||||
|
||||
center = np.mean(vertices, axis=0)
|
||||
directions = vertices - center
|
||||
angles = np.arctan2(directions[:, 1], directions[:, 0])
|
||||
sort_idx = np.argsort(angles)
|
||||
vertices = vertices[sort_idx]
|
||||
|
||||
left_top = np.min(vertices, axis=0)
|
||||
dists = np.linalg.norm(left_top - vertices, axis=-1, ord=2)
|
||||
lefttop_idx = np.argmin(dists)
|
||||
indexes = (np.arange(N, dtype=np.int) + lefttop_idx) % N
|
||||
return vertices[indexes]
|
||||
|
||||
|
||||
def sort_vertex8(points):
|
||||
# TODO Add typehints & docstring & test
|
||||
"""Sort vertex with 8 points [x1 y1 x2 y2 x3 y3 x4 y4]"""
|
||||
assert len(points) == 8
|
||||
vertices = _sort_vertex(np.array(points, dtype=np.float32).reshape(-1, 2))
|
||||
sorted_box = list(vertices.flatten())
|
||||
return sorted_box
|
||||
|
||||
|
||||
def box_center_distance(b1, b2):
|
||||
# TODO typehints & docstring & test
|
||||
assert isinstance(b1, np.ndarray)
|
||||
|
Loading…
x
Reference in New Issue
Block a user