Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with pyclipper inhomogeneous expanded array #12108

Merged
merged 6 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions ppocr/postprocess/db_postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ def polygons_from_bitmap(self, pred, _bitmap, dest_width, dest_height):
continue
else:
continue
box = box.reshape(-1, 2)
box = np.array(box).reshape(-1, 2)
if len(box) == 0:
continue

_, sside = self.get_mini_boxes(box.reshape((-1, 1, 2)))
if sside < self.min_size + 2:
Expand Down Expand Up @@ -138,7 +140,10 @@ def boxes_from_bitmap(self, pred, _bitmap, dest_width, dest_height):
if self.box_thresh > score:
continue

box = self.unclip(points, self.unclip_ratio).reshape(-1, 1, 2)
box = self.unclip(points, self.unclip_ratio)
if len(box) > 1:
continue
box = np.array(box).reshape(-1, 1, 2)
box, sside = self.get_mini_boxes(box)
if sside < self.min_size + 2:
continue
Expand All @@ -157,7 +162,7 @@ def unclip(self, box, unclip_ratio):
distance = poly.area * unclip_ratio / poly.length
offset = pyclipper.PyclipperOffset()
offset.AddPath(box, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
expanded = np.array(offset.Execute(distance))
expanded = offset.Execute(distance)
return expanded

def get_mini_boxes(self, contour):
Expand Down
15 changes: 15 additions & 0 deletions tools/infer/predict_det.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ def order_points_clockwise(self, pts):
rect[3] = tmp[np.argmax(diff)]
return rect

def pad_polygons(self, polygon, max_points):
padding_size = max_points - len(polygon)
if padding_size == 0:
return polygon
last_point = polygon[-1]
padding = np.repeat([last_point], padding_size, axis=0)
return np.vstack([polygon, padding])

def clip_det_res(self, points, img_height, img_width):
for pno in range(points.shape[0]):
points[pno, 0] = int(min(max(points[pno, 0], 0), img_width - 1))
Expand Down Expand Up @@ -209,6 +217,13 @@ def filter_tag_det_res_only_clip(self, dt_boxes, image_shape):
box = np.array(box)
box = self.clip_det_res(box, img_height, img_width)
dt_boxes_new.append(box)

if len(dt_boxes_new) > 0:
max_points = max(len(polygon) for polygon in dt_boxes_new)
dt_boxes_new = [
self.pad_polygons(polygon, max_points) for polygon in dt_boxes_new
]

dt_boxes = np.array(dt_boxes_new)
return dt_boxes

Expand Down
Loading