Skip to content

Commit

Permalink
fix bug in copy_paste (PaddlePaddle#3245)
Browse files Browse the repository at this point in the history
* fix bug in copy_paste

* limit paste img not outside
  • Loading branch information
WenmuZhou authored Jul 3, 2021
1 parent 9d44728 commit 605bf83
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions ppocr/data/imaug/copy_paste.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,21 @@ def paste_img(self, src_img, box_img, src_polys):
box_img_pil = Image.fromarray(box_img).convert('RGBA')
src_w, src_h = src_img.size
box_w, box_h = box_img_pil.size
if box_w > src_w or box_h > src_h:
return src_img, None

angle = np.random.randint(0, 360)
box = np.array([[[0, 0], [box_w, 0], [box_w, box_h], [0, box_h]]])
box = rotate_bbox(box_img, box, angle)[0]
box_img_pil = box_img_pil.rotate(angle, expand=1)
box_w, box_h = box_img_pil.width, box_img_pil.height
if src_w - box_w < 0 or src_h - box_h < 0:
return src_img, None

paste_x, paste_y = self.select_coord(src_polys, box, src_w - box_w,
src_h - box_h)
if paste_x is None:
return src_img, None
box[:, 0] += paste_x
box[:, 1] += paste_y
box_img_pil = box_img_pil.rotate(angle, expand=1)
r, g, b, A = box_img_pil.split()
src_img.paste(box_img_pil, (paste_x, paste_y), mask=A)

Expand All @@ -105,7 +107,7 @@ def select_coord(self, src_polys, box, endx, endy):

num_poly_in_rect = 0
for poly in src_polys:
if not is_poly_outside_rect(poly, xmax1, ymin1,
if not is_poly_outside_rect(poly, xmin1, ymin1,
xmax1 - xmin1, ymax1 - ymin1):
num_poly_in_rect += 1
break
Expand Down

0 comments on commit 605bf83

Please sign in to comment.