Skip to content

Commit

Permalink
Merge pull request #450 from interactivetech/master
Browse files Browse the repository at this point in the history
Vertical Text Support
  • Loading branch information
rkcosmos authored Jun 13, 2021
2 parents 2e94e15 + 314fab4 commit 1c9bb05
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,7 @@ ENV/
.mypy_cache/

# macOS
*.DS_Store
*.DS_Store

#virtual env
.vscode/
50 changes: 43 additions & 7 deletions easyocr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,29 @@ def group_text_box(polys, slope_ths = 0.1, ycenter_ths = 0.5, height_ths = 0.5,
# may need to check if box is really in image
return merged_list, free_list

def calculate_ratio(width,height):
'''
Calculate aspect ratio for normal use case (w>h) and vertical text (h>w)
'''
ratio = width/height
if ratio<1.0:
ratio = 1./ratio
return ratio

def compute_ratio_and_resize(img,width,height,model_height):
'''
Calculate ratio and resize correctly for both horizontal text
and vertical case
'''
ratio = width/height
if ratio<1.0:
ratio = calculate_ratio(width,height)
img = cv2.resize(img,(model_height,int(model_height*ratio)), interpolation=Image.ANTIALIAS)
else:
img = cv2.resize(img,(int(model_height*ratio),model_height),interpolation=Image.ANTIALIAS)
return img,ratio


def get_image_list(horizontal_list, free_list, img, model_height = 64, sort_output = True):
image_list = []
maximum_y,maximum_x = img.shape
Expand All @@ -519,12 +542,12 @@ def get_image_list(horizontal_list, free_list, img, model_height = 64, sort_outp
for box in free_list:
rect = np.array(box, dtype = "float32")
transformed_img = four_point_transform(img, rect)
ratio = transformed_img.shape[1]/transformed_img.shape[0]
ratio = calculate_ratio(transformed_img.shape[1],transformed_img.shape[0])
new_width = int(model_height*ratio)
if new_width == 0:
pass
else:
crop_img = cv2.resize(transformed_img, (new_width, model_height), interpolation = Image.ANTIALIAS)
crop_img,ratio = compute_ratio_and_resize(transformed_img,transformed_img.shape[1],transformed_img.shape[0],model_height)
image_list.append( (box,crop_img) ) # box = [[x1,y1],[x2,y2],[x3,y3],[x4,y4]]
max_ratio_free = max(ratio, max_ratio_free)

Expand All @@ -539,12 +562,12 @@ def get_image_list(horizontal_list, free_list, img, model_height = 64, sort_outp
crop_img = img[y_min : y_max, x_min:x_max]
width = x_max - x_min
height = y_max - y_min
ratio = width/height
ratio = calculate_ratio(width,height)
new_width = int(model_height*ratio)
if new_width == 0:
pass
else:
crop_img = cv2.resize(crop_img, (new_width, model_height), interpolation = Image.ANTIALIAS)
crop_img,ratio = compute_ratio_and_resize(crop_img,width,height,model_height)
image_list.append( ( [[x_min,y_min],[x_max,y_min],[x_max,y_max],[x_min,y_max]] ,crop_img) )
max_ratio_hori = max(ratio, max_ratio_hori)

Expand Down Expand Up @@ -707,17 +730,30 @@ def make_rotated_img_list(rotationInfo, img_list):
result_img_list = img_list[:]

# add rotated images to original image_list
max_ratio=1
if 90 in rotationInfo:
for img_info in img_list:
result_img_list.append((img_info[0], cv2.rotate(img_info[1], cv2.ROTATE_90_COUNTERCLOCKWISE)))
ninty_image = cv2.rotate(img_info[1],cv2.ROTATE_90_COUNTERCLOCKWISE)
height,width = ninty_image.shape
ratio = calculate_ratio(width,height)
max_ratio = max(max_ratio,ratio)
result_img_list.append((img_info[0],ninty_image))

if 180 in rotationInfo:
for img_info in img_list:
result_img_list.append((img_info[0], cv2.rotate(img_info[1], cv2.ROTATE_180)))
one_eighty_image = cv2.rotate(img_info[1],cv2.ROTATE_180)
height,width = one_eighty_image.shape
ratio = calculate_ratio(width,height)
max_ratio = max(max_ratio,ratio)
result_img_list.append((img_info[0], one_eighty_image))

if 270 in rotationInfo:
for img_info in img_list:
result_img_list.append((img_info[0], cv2.rotate(img_info[1], cv2.ROTATE_90_CLOCKWISE)))
two_seventy_image = cv2.rotate(img_info[1], cv2.ROTATE_90_CLOCKWISE)
height,width = two_seventy_image.shape
ratio = calculate_ratio(width,height)
max_ratio = max(max_ratio,ratio)
result_img_list.append((img_info[0],two_seventy_image))

return result_img_list

Expand Down

0 comments on commit 1c9bb05

Please sign in to comment.