ocr: make sure that image height or width is not zero

This commit is contained in:
vahidrezanezhad 2025-07-03 15:24:52 +02:00
parent 59ea493803
commit e54ebaa23e
2 changed files with 20 additions and 18 deletions

View file

@ -5436,7 +5436,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:
if angle_degrees > 3:
@ -5483,9 +5482,6 @@ class Eynollah_ocr:
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:
img_fin = preprocess_and_resize_image_for_ocrcnn_model(img_crop, image_height, image_width)

View file

@ -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