From 77e29adb7b9de83287d5aa31d02c23f476179247 Mon Sep 17 00:00:00 2001 From: wangxinyu Date: Mon, 4 Jul 2022 07:09:10 +0000 Subject: [PATCH] [Utils] Migrate datasets/pepelines/box_utils.py --- .dev_scripts/covignore.cfg | 7 ++++ mmocr/datasets/pipelines/__init__.py | 13 +++---- mmocr/datasets/pipelines/box_utils.py | 53 --------------------------- mmocr/datasets/pipelines/crop.py | 3 +- mmocr/utils/__init__.py | 11 +++--- mmocr/utils/bbox_utils.py | 53 +++++++++++++++++++++++++++ 6 files changed, 73 insertions(+), 67 deletions(-) delete mode 100644 mmocr/datasets/pipelines/box_utils.py diff --git a/.dev_scripts/covignore.cfg b/.dev_scripts/covignore.cfg index fb6b595f..79f6b7a8 100644 --- a/.dev_scripts/covignore.cfg +++ b/.dev_scripts/covignore.cfg @@ -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 diff --git a/mmocr/datasets/pipelines/__init__.py b/mmocr/datasets/pipelines/__init__.py index fc66b3b4..1a0e191a 100644 --- a/mmocr/datasets/pipelines/__init__.py +++ b/mmocr/datasets/pipelines/__init__.py @@ -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' ] diff --git a/mmocr/datasets/pipelines/box_utils.py b/mmocr/datasets/pipelines/box_utils.py deleted file mode 100644 index 12447585..00000000 --- a/mmocr/datasets/pipelines/box_utils.py +++ /dev/null @@ -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 diff --git a/mmocr/datasets/pipelines/crop.py b/mmocr/datasets/pipelines/crop.py index 416339ec..47b87a6a 100644 --- a/mmocr/datasets/pipelines/crop.py +++ b/mmocr/datasets/pipelines/crop.py @@ -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( diff --git a/mmocr/utils/__init__.py b/mmocr/utils/__init__.py index 15daad0e..b353a358 100644 --- a/mmocr/utils/__init__.py +++ b/mmocr/utils/__init__.py @@ -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' ] diff --git a/mmocr/utils/bbox_utils.py b/mmocr/utils/bbox_utils.py index 45b3d4fe..0469821b 100644 --- a/mmocr/utils/bbox_utils.py +++ b/mmocr/utils/bbox_utils.py @@ -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)