[Update] Update TextDetRandomCropFlip

pull/1178/head
jiangqing.vendor 2022-06-17 09:54:31 +00:00 committed by gaotongxiao
parent f71852398d
commit 55c99dd0c1
2 changed files with 36 additions and 5 deletions

View File

@ -570,8 +570,9 @@ class TextDetRandomCropFlip(BaseTransform):
assert 'img' in results, '`img` is not found in results'
for _ in range(self.iter_num):
results = self._random_crop_flip_polygons(results)
# TODO Add random_crop_flip_bboxes (will be added after the poly2box
# and box2poly have been merged)
bboxes = [poly2bbox(poly) for poly in results['gt_polygons']]
results['gt_bboxes'] = np.array(
bboxes, dtype=np.float32).reshape(-1, 4)
return results
def _random_crop_flip_polygons(self, results: Dict) -> Dict:
@ -604,6 +605,7 @@ class TextDetRandomCropFlip(BaseTransform):
for _ in range(10):
polys_keep = []
polys_new = []
kept_idxs = []
xx = self._random_choice(w_axis)
yy = self._random_choice(h_axis)
xmin = np.clip(np.min(xx) - pad_w, 0, w - 1)
@ -618,7 +620,7 @@ class TextDetRandomCropFlip(BaseTransform):
[ymin, ymin, ymax, ymax]]).T.astype(np.int32)
pp = plg(pts)
success_flag = True
for polygon in polygons:
for poly_idx, polygon in enumerate(polygons):
ppi = plg(polygon.reshape(-1, 2))
# TODO Move this eval_utils to point_utils?
ppiou = eval_utils.poly_intersection(ppi, pp)
@ -626,6 +628,7 @@ class TextDetRandomCropFlip(BaseTransform):
np.abs(ppiou) > self.epsilon:
success_flag = False
break
kept_idxs.append(poly_idx)
if np.abs(ppiou - float(ppi.area)) < self.epsilon:
polys_new.append(polygon)
else:
@ -636,7 +639,6 @@ class TextDetRandomCropFlip(BaseTransform):
cropped = image[ymin:ymax, xmin:xmax, :]
select_type = self._random_flip_type()
print(select_type)
if select_type == 0:
img = np.ascontiguousarray(cropped[:, ::-1])
elif select_type == 1:
@ -665,8 +667,11 @@ class TextDetRandomCropFlip(BaseTransform):
poly[:, 1] = height - poly[:, 1] + 2 * ymin
polys_new[idx] = poly.reshape(-1, )
polygons = polys_keep + polys_new
# ignored = polys_keep_ignore_idx + polys_new_ignore_idx
results['gt_polygons'] = polygons
results['gt_ignored'] = results['gt_ignored'][kept_idxs]
results['gt_bboxes_labels'] = results['gt_bboxes_labels'][
kept_idxs]
return results
def _generate_crop_target(self, image: np.ndarray,

View File

@ -73,6 +73,17 @@ class TestTextDetRandomCropFlip(unittest.TestCase):
self.data_info2 = dict(
img=copy.deepcopy(img),
gt_polygons=[np.array([1., 1., 1., 9., 9., 9., 9., 1.])],
gt_bboxes_labels=np.array([0], dtype=np.int64),
gt_ignored=np.array([True], dtype=np.bool_),
img_shape=[10, 10])
self.data_info3 = dict(
img=copy.deepcopy(img),
gt_polygons=[
np.array([0., 0., 4., 0., 4., 4., 0., 4.]),
np.array([4., 0., 8., 0., 8., 4., 4., 4.])
],
gt_bboxes_labels=np.array([0, 0], dtype=np.int64),
gt_ignored=np.array([True, True], dtype=np.bool_),
img_shape=[10, 10])
def test_init(self):
@ -93,6 +104,21 @@ class TestTextDetRandomCropFlip(unittest.TestCase):
self.assertTrue(
np.allclose(results['gt_polygons'],
self.data_info2['gt_polygons']))
self.assertEqual(
len(results['gt_bboxes']), len(results['gt_polygons']))
self.assertTrue(
poly2shapely(results['gt_polygons'][0]).equals(
poly2shapely(bbox2poly(results['gt_bboxes'][0]))))
def test_size(self):
transform = TextDetRandomCropFlip(crop_ratio=1.0, iter_num=3)
results = transform(self.data_info3)
self.assertEqual(
len(results['gt_bboxes']), len(results['gt_polygons']))
self.assertEqual(
len(results['gt_polygons']), len(results['gt_ignored']))
self.assertEqual(
len(results['gt_ignored']), len(results['gt_bboxes_labels']))
def test_generate_crop_target(self):
transform = TextDetRandomCropFlip(