-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiocr.py
101 lines (76 loc) · 3.04 KB
/
iocr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import os
import shutil
import argparse
import sys
# Appending vitaFlow main Path
import warnings
from absl import logging
from tqdm import tqdm
from crop_to_box import crop_to_box
from ocr_calamari import calamari, get_all_input_files
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
logging.set_verbosity(logging.ERROR)
sys.path.append(os.path.abspath('.'))
warnings.simplefilter(action='ignore', category=FutureWarning)
warnings.simplefilter(action='ignore', category=Warning)
import time
import cv2
import numpy as np
import fire
import tensorflow as tf
from prediction import resize_image, sort_poly, detect
from grpc_predict import get_text_segmentation_pb, read_image
tf.get_logger().setLevel(logging.ERROR)
OUT_DIR = "/tmp/oct/"
def east(image_file_path, out_dir, model_dir="models/vf_east_models/east/EASTIEstimatorModel/exported/1558013588/"):
print(image_file_path)
predict_fn = tf.contrib.predictor.from_saved_model(model_dir)
im, img_resized, ratio_h, ratio_w = read_image(image_file_path)
result = predict_fn({'images': img_resized})
get_text_segmentation_pb(img_mat=im,
result=result,
output_dir=out_dir,
file_name=os.path.basename(image_file_path),
ratio_h=ratio_h,
ratio_w=ratio_w)
def get_text_file(pred_text_dir, out_file_path):
lines = []
in_files = get_all_input_files(source_dir=pred_text_dir,
input_files_types=[".txt"])
for each_in_text_prediction in tqdm(in_files):
if os.path.isfile(out_file_path):
# read the file and append values
with open(out_file_path, "a") as fd:
line = open(each_in_text_prediction, "r").read()
lines.append(line)
fd.write(line)
fd.write("\n")
else:
# create a new file with headers
with open(out_file_path, "w") as fd:
line = open(each_in_text_prediction, "r").read()
lines.append(line)
fd.write(line)
fd.write("\n")
return lines
def ocr(in_file_path, temp_dir="/tmp/ocr/"):
"""
:param in_file_path:
:return:
"""
global OUT_DIR
OUT_DIR = temp_dir
file_name = os.path.basename(in_file_path)
file_name_base = str(file_name.split(".")[0])
east(in_file_path, out_dir=OUT_DIR + file_name_base + "/east")
crop_to_box(gt_text_file_loc=OUT_DIR + file_name_base + "/east/" + file_name_base + ".txt",
source_image_loc=OUT_DIR + file_name_base + "/east/" + file_name,
cropped_dir=OUT_DIR + file_name_base + "/cropped/")
calamari(source_dir=OUT_DIR + file_name_base + "/cropped/",
destination_dir=OUT_DIR + file_name_base + "/text")
ret = get_text_file(pred_text_dir=OUT_DIR + file_name_base + "/text",
out_file_path=OUT_DIR + file_name_base + ".txt")
print(ret)
return ret
if __name__ == '__main__':
fire.Fire(ocr)