Skip to content

Commit 502e167

Browse files
zovelsanjGreatV
andauthored
Error with pyclipper inhomogeneous expanded array (#12108)
* pyclipper inhomogeneous expanded array solved For some images, `np.array(offset.Execute(distance))` can result in inhomogeneous part of the detection box list, which cannot be casted into numpy array directly. * corrected box reshape position - box reshape was mistakenly done at line 145 which is now correctly done at line 92 of `db_postprocess.py` - if box is empty then continue * reverted mistakenly changed line 147 - reverted mistakenly changed `box.array(box)` to `np.array(box)` * expanded array fix for `det_box_type=quad` * polygons padding For `--det_box_type = poly`, pad the detected polygon arrays if they have different shapes to ensure even shapes of polygon arrays * fix codestyle --------- Co-authored-by: Wang Xin <xinwang614@gmail.com>
1 parent 8b71785 commit 502e167

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

ppocr/postprocess/db_postprocess.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ def polygons_from_bitmap(self, pred, _bitmap, dest_width, dest_height):
8989
continue
9090
else:
9191
continue
92-
box = box.reshape(-1, 2)
92+
box = np.array(box).reshape(-1, 2)
93+
if len(box) == 0:
94+
continue
9395

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

141-
box = self.unclip(points, self.unclip_ratio).reshape(-1, 1, 2)
143+
box = self.unclip(points, self.unclip_ratio)
144+
if len(box) > 1:
145+
continue
146+
box = np.array(box).reshape(-1, 1, 2)
142147
box, sside = self.get_mini_boxes(box)
143148
if sside < self.min_size + 2:
144149
continue
@@ -157,7 +162,7 @@ def unclip(self, box, unclip_ratio):
157162
distance = poly.area * unclip_ratio / poly.length
158163
offset = pyclipper.PyclipperOffset()
159164
offset.AddPath(box, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
160-
expanded = np.array(offset.Execute(distance))
165+
expanded = offset.Execute(distance)
161166
return expanded
162167

163168
def get_mini_boxes(self, contour):

tools/infer/predict_det.py

+15
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ def order_points_clockwise(self, pts):
179179
rect[3] = tmp[np.argmax(diff)]
180180
return rect
181181

182+
def pad_polygons(self, polygon, max_points):
183+
padding_size = max_points - len(polygon)
184+
if padding_size == 0:
185+
return polygon
186+
last_point = polygon[-1]
187+
padding = np.repeat([last_point], padding_size, axis=0)
188+
return np.vstack([polygon, padding])
189+
182190
def clip_det_res(self, points, img_height, img_width):
183191
for pno in range(points.shape[0]):
184192
points[pno, 0] = int(min(max(points[pno, 0], 0), img_width - 1))
@@ -209,6 +217,13 @@ def filter_tag_det_res_only_clip(self, dt_boxes, image_shape):
209217
box = np.array(box)
210218
box = self.clip_det_res(box, img_height, img_width)
211219
dt_boxes_new.append(box)
220+
221+
if len(dt_boxes_new) > 0:
222+
max_points = max(len(polygon) for polygon in dt_boxes_new)
223+
dt_boxes_new = [
224+
self.pad_polygons(polygon, max_points) for polygon in dt_boxes_new
225+
]
226+
212227
dt_boxes = np.array(dt_boxes_new)
213228
return dt_boxes
214229

0 commit comments

Comments
 (0)