Skip to content

Commit 925e2ed

Browse files
Fix crop_margin crash with extreme aspect ratio images (#17686)
Add bounds checking to crop_margin in GoTImgDecode, UniMERNetImgDecode, and UniMERNetResize to handle edge cases: - Return original image when cv2.findNonZero returns None (no text found) - Return original image when bounding rect has zero width or height - Return original image when cropped result would have aspect ratio > 200, which causes ValueError in downstream image processing Fixes #17354 Co-authored-by: Lin Manhui <mhlin425@whu.edu.cn>
1 parent b6a561d commit 925e2ed

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

ppocr/data/imaug/unimernet_aug.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,14 @@ def crop_margin(self, img):
495495
data = (data - min_val) / (max_val - min_val) * 255
496496
gray = 255 * (data < 200).astype(np.uint8)
497497
coords = cv2.findNonZero(gray) # Find all non-zero points (text)
498+
if coords is None:
499+
return img
498500
a, b, w, h = cv2.boundingRect(coords) # Find minimum spanning bounding box
501+
if w == 0 or h == 0:
502+
return img
503+
# Avoid extreme aspect ratios that cause errors in downstream processing
504+
if max(w, h) / min(w, h) > 200:
505+
return img
499506
return img.crop((a, b, w + a, h + b))
500507

501508
def get_dimensions(self, img):
@@ -595,7 +602,14 @@ def crop_margin(self, img):
595602
data = (data - min_val) / (max_val - min_val) * 255
596603
gray = 255 * (data < 200).astype(np.uint8)
597604
coords = cv2.findNonZero(gray) # Find all non-zero points (text)
605+
if coords is None:
606+
return img
598607
a, b, w, h = cv2.boundingRect(coords) # Find minimum spanning bounding box
608+
if w == 0 or h == 0:
609+
return img
610+
# Avoid extreme aspect ratios that cause errors in downstream processing
611+
if max(w, h) / min(w, h) > 200:
612+
return img
599613
return img.crop((a, b, w + a, h + b))
600614

601615
def get_dimensions(self, img):
@@ -720,7 +734,14 @@ def crop_margin(self, img):
720734
gray = 255 * (data < 200).astype(np.uint8)
721735

722736
coords = cv2.findNonZero(gray) # Find all non-zero points (text)
737+
if coords is None:
738+
return img
723739
a, b, w, h = cv2.boundingRect(coords) # Find minimum spanning bounding box
740+
if w == 0 or h == 0:
741+
return img
742+
# Avoid extreme aspect ratios that cause errors in downstream processing
743+
if max(w, h) / min(w, h) > 200:
744+
return img
724745
return img.crop((a, b, w + a, h + b))
725746

726747
def get_dimensions(self, img):

0 commit comments

Comments
 (0)