Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gaotongxiao committed Mar 9, 2023
1 parent 60e6340 commit eb363b4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 411 deletions.
44 changes: 1 addition & 43 deletions mmocr/utils/bbox_utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Copyright (c) OpenMMLab. All rights reserved.
import functools
from typing import List, Tuple

import numpy as np
from shapely.geometry import LineString, Point

from mmocr.utils.check_argument import is_2dlist, is_type_list
from mmocr.utils.check_argument import is_type_list
from mmocr.utils.point_utils import point_distance, points_center
from mmocr.utils.typing_utils import ArrayLike

Expand Down Expand Up @@ -248,47 +247,6 @@ def bezier2polygon(bezier_points: np.ndarray,
return points.tolist()


def sort_points(points):
# TODO Add typehints & test & docstring
"""Sort arbitory points in clockwise order. Reference:
https://stackoverflow.com/a/6989383.
Args:
points (list[ndarray] or ndarray or list[list]): A list of unsorted
boundary points.
Returns:
list[ndarray]: A list of points sorted in clockwise order.
"""

assert is_type_list(points, np.ndarray) or isinstance(points, np.ndarray) \
or is_2dlist(points)

points = np.array(points)
center = np.mean(points, axis=0)

def cmp(a, b):
oa = a - center
ob = b - center

# Some corner cases
if oa[0] >= 0 and ob[0] < 0:
return 1
if oa[0] < 0 and ob[0] >= 0:
return -1

prod = np.cross(oa, ob)
if prod > 0:
return 1
if prod < 0:
return -1

# a, b are on the same line from the center
return 1 if (oa**2).sum() < (ob**2).sum() else -1

return sorted(points, key=functools.cmp_to_key(cmp))


def sort_vertex(points_x, points_y):
# TODO Add typehints & docstring & test
"""Sort box vertices in clockwise order from left-top first.
Expand Down
190 changes: 0 additions & 190 deletions mmocr/utils/box_util.py

This file was deleted.

47 changes: 19 additions & 28 deletions mmocr/utils/polygon_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Copyright (c) OpenMMLab. All rights reserved.
import functools
import math
import operator
from functools import reduce
from typing import List, Optional, Sequence, Tuple, Union

import numpy as np
Expand Down Expand Up @@ -374,8 +376,14 @@ def boundary_iou(src: List,

def sort_points(points):
# TODO Add typehints & test & docstring
"""Sort arbitory points in clockwise order. Reference:
https://stackoverflow.com/a/6989383.
"""Sort arbitrary points in clockwise order in Cartesian coordinate, you
may need to reverse the output sequence if you are using OpenCV's image
coordinate.
Reference:
https://github.com/novioleo/Savior/blob/master/Utils/GeometryUtils.py.
Warning: This function can only sort convex polygons.
Args:
points (list[ndarray] or ndarray or list[list]): A list of unsorted
Expand All @@ -384,33 +392,16 @@ def sort_points(points):
Returns:
list[ndarray]: A list of points sorted in clockwise order.
"""

assert is_list_of(points, np.ndarray) or isinstance(points, np.ndarray) \
or is_2dlist(points)

points = np.array(points)
center = np.mean(points, axis=0)

def cmp(a, b):
oa = a - center
ob = b - center

# Some corner cases
if oa[0] >= 0 and ob[0] < 0:
return 1
if oa[0] < 0 and ob[0] >= 0:
return -1

prod = np.cross(oa, ob)
if prod > 0:
return 1
if prod < 0:
return -1

# a, b are on the same line from the center
return 1 if (oa**2).sum() < (ob**2).sum() else -1

return sorted(points, key=functools.cmp_to_key(cmp))
center_point = tuple(
map(operator.truediv,
reduce(lambda x, y: map(operator.add, x, y), points),
[len(points)] * 2))
return sorted(
points,
key=lambda coord: (180 + math.degrees(
math.atan2(*tuple(map(operator.sub, coord, center_point))))) % 360)


def sort_vertex(points_x, points_y):
Expand Down
Loading

0 comments on commit eb363b4

Please sign in to comment.