mirror of
https://github.com/PaddlePaddle/PaddleOCR.git
synced 2026-08-30 17:23:03 +08:00
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 PaddlePaddle#17354 Co-authored-by: Lin Manhui <mhlin425@whu.edu.cn>
This commit is contained in:
@@ -495,7 +495,14 @@ class GoTImgDecode:
|
||||
data = (data - min_val) / (max_val - min_val) * 255
|
||||
gray = 255 * (data < 200).astype(np.uint8)
|
||||
coords = cv2.findNonZero(gray) # Find all non-zero points (text)
|
||||
if coords is None:
|
||||
return img
|
||||
a, b, w, h = cv2.boundingRect(coords) # Find minimum spanning bounding box
|
||||
if w == 0 or h == 0:
|
||||
return img
|
||||
# Avoid extreme aspect ratios that cause errors in downstream processing
|
||||
if max(w, h) / min(w, h) > 200:
|
||||
return img
|
||||
return img.crop((a, b, w + a, h + b))
|
||||
|
||||
def get_dimensions(self, img):
|
||||
@@ -595,7 +602,14 @@ class UniMERNetImgDecode:
|
||||
data = (data - min_val) / (max_val - min_val) * 255
|
||||
gray = 255 * (data < 200).astype(np.uint8)
|
||||
coords = cv2.findNonZero(gray) # Find all non-zero points (text)
|
||||
if coords is None:
|
||||
return img
|
||||
a, b, w, h = cv2.boundingRect(coords) # Find minimum spanning bounding box
|
||||
if w == 0 or h == 0:
|
||||
return img
|
||||
# Avoid extreme aspect ratios that cause errors in downstream processing
|
||||
if max(w, h) / min(w, h) > 200:
|
||||
return img
|
||||
return img.crop((a, b, w + a, h + b))
|
||||
|
||||
def get_dimensions(self, img):
|
||||
@@ -720,7 +734,14 @@ class UniMERNetResize:
|
||||
gray = 255 * (data < 200).astype(np.uint8)
|
||||
|
||||
coords = cv2.findNonZero(gray) # Find all non-zero points (text)
|
||||
if coords is None:
|
||||
return img
|
||||
a, b, w, h = cv2.boundingRect(coords) # Find minimum spanning bounding box
|
||||
if w == 0 or h == 0:
|
||||
return img
|
||||
# Avoid extreme aspect ratios that cause errors in downstream processing
|
||||
if max(w, h) / min(w, h) > 200:
|
||||
return img
|
||||
return img.crop((a, b, w + a, h + b))
|
||||
|
||||
def get_dimensions(self, img):
|
||||
|
||||
Reference in New Issue
Block a user