From e54ebaa23e89d0381157415e33d6324c3dd8aecd Mon Sep 17 00:00:00 2001 From: vahidrezanezhad Date: Thu, 3 Jul 2025 15:24:52 +0200 Subject: [PATCH] ocr: make sure that image height or width is not zero --- src/eynollah/eynollah.py | 4 ---- src/eynollah/utils/utils_ocr.py | 34 +++++++++++++++++++-------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/eynollah/eynollah.py b/src/eynollah/eynollah.py index 3b9d898..1260a96 100644 --- a/src/eynollah/eynollah.py +++ b/src/eynollah/eynollah.py @@ -5435,7 +5435,6 @@ class Eynollah_ocr: mask_poly = mask_poly[y:y+h, x:x+w, :] img_crop = img_poly_on_img[y:y+h, x:x+w, :] - #print(file_name, angle_degrees,w*h , mask_poly[:,:,0].sum(), mask_poly[:,:,0].sum() /float(w*h) , 'didi') if not self.do_not_mask_with_textline_contour: @@ -5482,9 +5481,6 @@ class Eynollah_ocr: img_crop, img_crop_bin = break_curved_line_into_small_pieces_and_then_merge(img_crop, mask_poly, img_crop_bin) else: img_crop, _ = break_curved_line_into_small_pieces_and_then_merge(img_crop, mask_poly) - - - if not self.export_textline_images_and_text: if w_scaled < 750:#1.5*image_width: diff --git a/src/eynollah/utils/utils_ocr.py b/src/eynollah/utils/utils_ocr.py index 81a8ae1..1e9162a 100644 --- a/src/eynollah/utils/utils_ocr.py +++ b/src/eynollah/utils/utils_ocr.py @@ -124,23 +124,26 @@ def return_textlines_split_if_needed(textline_image, textline_image_bin, predict else: return None, None def preprocess_and_resize_image_for_ocrcnn_model(img, image_height, image_width): - ratio = image_height /float(img.shape[0]) - w_ratio = int(ratio * img.shape[1]) - - if w_ratio <= image_width: - width_new = w_ratio + if img.shape[0]==0 or img.shape[1]==0: + img_fin = np.ones((image_height, image_width, 3)) else: - width_new = image_width + ratio = image_height /float(img.shape[0]) + w_ratio = int(ratio * img.shape[1]) - if width_new == 0: - width_new = img.shape[1] + if w_ratio <= image_width: + width_new = w_ratio + else: + width_new = image_width + + if width_new == 0: + width_new = img.shape[1] + - - img = resize_image(img, image_height, width_new) - img_fin = np.ones((image_height, image_width, 3))*255 + img = resize_image(img, image_height, width_new) + img_fin = np.ones((image_height, image_width, 3))*255 - img_fin[:,:width_new,:] = img[:,:,:] - img_fin = img_fin / 255. + img_fin[:,:width_new,:] = img[:,:,:] + img_fin = img_fin / 255. return img_fin def get_deskewed_contour_and_bb_and_image(contour, image, deskew_angle): @@ -188,7 +191,10 @@ def rotate_image_with_padding(image, angle, border_value=(0,0,0)): rotation_matrix[1, 2] += (new_h / 2) - center[1] # Perform the rotation - rotated_image = cv2.warpAffine(image, rotation_matrix, (new_w, new_h), borderValue=border_value) + try: + rotated_image = cv2.warpAffine(image, rotation_matrix, (new_w, new_h), borderValue=border_value) + except: + rotated_image = np.copy(image) return rotated_image