Skip to content

Commit 5b04def

Browse files
authored
Bug fix: Clamp bbox to image boundaries to avoid cv2 errors when cropping image region (#60)
1 parent b2d4aff commit 5b04def

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

glmocr/utils/layout_postprocess_utils.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ def apply_layout_postprocess(
349349
boxes_array = unclip_boxes(boxes_array, layout_unclip_ratio)
350350

351351
# Convert to PaddleOCR format
352+
img_width, img_height = img_size
352353
image_results = []
353354
for i, box_data in enumerate(boxes_array):
354355
cls_id = int(box_data[0])
@@ -357,13 +358,22 @@ def apply_layout_postprocess(
357358
order = int(box_data[6]) if box_data[6] > 0 else None
358359
label_name = id2label.get(cls_id, f"class_{cls_id}")
359360

360-
# Get polygon points (try to find original index)
361+
# Clamp bbox to image boundaries
362+
x1 = max(0, min(float(x1), img_width))
363+
y1 = max(0, min(float(y1), img_height))
364+
x2 = max(0, min(float(x2), img_width))
365+
y2 = max(0, min(float(y2), img_height))
366+
367+
# Skip invalid bbox
368+
if x1 >= x2 or y1 >= y2:
369+
continue
370+
361371
# Since we may have filtered boxes, we need to match by coordinates
362372
poly = None
363373
if len(polygon_points) > 0:
364374
# Try to find matching polygon by comparing coordinates
365375
for orig_idx in range(len(boxes)):
366-
if np.allclose(boxes[orig_idx], [x1, y1, x2, y2], atol=1.0):
376+
if np.allclose(boxes[orig_idx], box_data[2:6], atol=1.0):
367377
if orig_idx < len(polygon_points):
368378
poly = polygon_points[orig_idx].astype(np.float32)
369379
break
@@ -373,6 +383,10 @@ def apply_layout_postprocess(
373383
poly = np.array(
374384
[[x1, y1], [x2, y1], [x2, y2], [x1, y2]], dtype=np.float32
375385
)
386+
else:
387+
# Clamp polygon points to image boundaries
388+
poly[:, 0] = np.clip(poly[:, 0], 0, img_width)
389+
poly[:, 1] = np.clip(poly[:, 1], 0, img_height)
376390

377391
image_results.append(
378392
{

0 commit comments

Comments
 (0)