add mode to rescale_polygon(s)

pull/1178/head
gaotongxiao 2022-05-18 16:07:02 +08:00
parent df2f7b69db
commit 6478499073
3 changed files with 43 additions and 8 deletions

View File

@ -119,7 +119,7 @@ class BaseTextDetPostProcessor:
scale_factor = np.asarray(scale_factor)
for key in self.rescale_fields:
results.pred_instances[key] = rescale_polygons(
results.pred_instances[key], scale_factor)
results.pred_instances[key], scale_factor, mode='div')
return results
def get_text_instances(self, pred_results: dict,

View File

@ -6,43 +6,64 @@ from numpy.typing import ArrayLike
def rescale_polygon(polygon: ArrayLike,
scale_factor: Tuple[int, int]) -> np.ndarray:
scale_factor: Tuple[int, int],
mode: str = 'mul') -> np.ndarray:
"""Rescale a polygon according to scale_factor.
The behavior is different depending on the mode. When mode is 'mul', the
coordinates will be multiplied by scale_factor, which is usually used in
preprocessing transforms such as :func:`Resize`.
The coordinates will be divided by scale_factor if mode is 'div'. It can be
used in postprocessors to recover the polygon in the original
image size.
Args:
polygon (ArrayLike): A polygon. In any form can be converted
to an 1-D numpy array. E.g. list[float], np.ndarray,
or torch.Tensor. Polygon is written in
[x1, y1, x2, y2, ...].
scale_factor (tuple(int, int)): (w_scale, h_scale)
model (str): Rescale mode. Can be 'mul' or 'div'. Defaults to 'mul'.
Returns:
np.ndarray: Rescaled polygon.
"""
assert len(polygon) % 2 == 0
assert mode in ['mul', 'div']
polygon = np.array(polygon, dtype=np.float32)
poly_shape = polygon.shape
reshape_polygon = polygon.reshape(-1, 2)
scale_factor = np.array(scale_factor, dtype=float)
polygon = (reshape_polygon / scale_factor[None]).reshape(poly_shape)
if mode == 'div':
scale_factor = 1 / scale_factor
polygon = (reshape_polygon * scale_factor[None]).reshape(poly_shape)
return polygon
def rescale_polygons(polygons: Sequence[ArrayLike],
scale_factor: Tuple[int, int]) -> Sequence[np.ndarray]:
scale_factor: Tuple[int, int],
mode: str = 'mul') -> Sequence[np.ndarray]:
"""Rescale polygons according to scale_factor.
The behavior is different depending on the mode. When mode is 'mul', the
coordinates will be multiplied by scale_factor, which is usually used in
preprocessing transforms such as :func:`Resize`.
The coordinates will be divided by scale_factor if mode is 'div'. It can be
used in postprocessors to recover the polygon in the original
image size.
Args:
polygon (list[ArrayLike]): A list of polygons, each written in
[x1, y1, x2, y2, ...] and in any form can be converted
to an 1-D numpy array. E.g. list[list[float]],
list[np.ndarray], or list[torch.Tensor].
scale_factor (tuple(int, int)): (w_scale, h_scale)
model (str): Rescale mode. Can be 'mul' or 'div'. Defaults to 'mul'.
Returns:
list[np.ndarray]: Rescaled polygons.
"""
results = []
for polygon in polygons:
results.append(rescale_polygon(polygon, scale_factor))
results.append(rescale_polygon(polygon, scale_factor, mode))
return results

View File

@ -19,8 +19,12 @@ class TestPolygonUtils(unittest.TestCase):
polygons = [0, 0, 1, 0, 1, 1, 0, 1]
self.assertTrue(
np.allclose(
rescale_polygon(polygons, scale_factor),
rescale_polygon(polygons, scale_factor, mode='div'),
np.array([0, 0, 1 / 0.3, 0, 1 / 0.3, 1 / 0.4, 0, 1 / 0.4])))
self.assertTrue(
np.allclose(
rescale_polygon(polygons, scale_factor, mode='mul'),
np.array([0, 0, 0.3, 0, 0.3, 0.4, 0, 0.4])))
def test_rescale_polygons(self):
polygons = [
@ -30,14 +34,24 @@ class TestPolygonUtils(unittest.TestCase):
scale_factor = (0.5, 0.5)
self.assertTrue(
np.allclose(
rescale_polygons(polygons, scale_factor), [
rescale_polygons(polygons, scale_factor, mode='div'), [
np.array([0, 0, 2, 0, 2, 2, 0, 2]),
np.array([2, 2, 4, 2, 4, 4, 2, 4])
]))
self.assertTrue(
np.allclose(
rescale_polygons(polygons, scale_factor, mode='mul'), [
np.array([0, 0, 0.5, 0, 0.5, 0.5, 0, 0.5]),
np.array([0.5, 0.5, 1, 0.5, 1, 1, 0.5, 1])
]))
polygons = [torch.Tensor([0, 0, 1, 0, 1, 1, 0, 1])]
scale_factor = (0.3, 0.4)
self.assertTrue(
np.allclose(
rescale_polygons(polygons, scale_factor),
rescale_polygons(polygons, scale_factor, mode='div'),
[np.array([0, 0, 1 / 0.3, 0, 1 / 0.3, 1 / 0.4, 0, 1 / 0.4])]))
self.assertTrue(
np.allclose(
rescale_polygons(polygons, scale_factor, mode='mul'),
[np.array([0, 0, 0.3, 0, 0.3, 0.4, 0, 0.4])]))