-
Notifications
You must be signed in to change notification settings - Fork 336
/
Copy patheasyocr.py
203 lines (162 loc) · 7.23 KB
/
easyocr.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import sys
import time
import ailia
import cv2
import numpy as np
# import original modules
sys.path.append('../../util')
# logger
from logging import getLogger # noqa: E402
import webcamera_utils # noqa: E402
from image_utils import imread # noqa: E402
from model_utils import check_and_download_models # noqa: E402
from arg_utils import get_base_parser, get_savepath, update_parser # noqa: E402
logger = getLogger(__name__)
# for EasyOCR
from easyocr_utils import *
# ======================
# PARAMETERS
# ======================
DETECTOR_MODEL_PATH = 'detector_craft.onnx.prototxt'
DETECTOR_WEIGHT_PATH = 'detector_craft.onnx'
RECOGNIZER_CHINESE_MODEL_PATH = 'recognizer_zh_sim_g2.onnx.prototxt'
RECOGNIZER_CHINESE_WEIGHT_PATH = 'recognizer_zh_sim_g2.onnx'
RECOGNIZER_JAPANESE_MODEL_PATH = 'recognizer_japanese_g2.onnx.prototxt'
RECOGNIZER_JAPANESE_WEIGHT_PATH = 'recognizer_japanese_g2.onnx'
RECOGNIZER_ENGLISH_MODEL_PATH = 'recognizer_english_g2.onnx.prototxt'
RECOGNIZER_ENGLISH_WEIGHT_PATH = 'recognizer_english_g2.onnx'
RECOGNIZER_FRENCH_MODEL_PATH = 'recognizer_latin_g2.onnx.prototxt'
RECOGNIZER_FRENCH_WEIGHT_PATH = 'recognizer_latin_g2.onnx'
RECOGNIZER_KOREAN_MODEL_PATH = 'recognizer_korean_g2.onnx.prototxt'
RECOGNIZER_KOREAN_WEIGHT_PATH = 'recognizer_korean_g2.onnx'
RECOGNIZER_THAI_MODEL_PATH = 'recognizer_thai.onnx.prototxt'
RECOGNIZER_THAI_WEIGHT_PATH = 'recognizer_thai.onnx'
REMOTE_PATH = 'https://storage.googleapis.com/ailia-models/easyocr/'
IMAGE_PATH = 'example/chinese.jpg'
IMAGE_OR_VIDEO_PATH = 'input.jpg'
SAVE_IMAGE_OR_VIDEO_PATH = 'output.png'
IMAGE_WIDTH = 640
IMAGE_HEIGHT = 339
# ======================
# Arguemnt Parser Config
# ======================
parser = get_base_parser(
'Ready-to-use OCR', IMAGE_PATH, SAVE_IMAGE_OR_VIDEO_PATH
)
parser.add_argument(
'-l', '--language', type=str, default='chinese',
)
args = update_parser(parser)
# ======================
# Main functions
# ======================
def predict(img, img_grey):
# detect
horizontal_list, free_list = detector_predict(detector, img)
# recognize
result = recognizer_predict(args.language, lang_list, character, symbol, recognizer, img_grey, horizontal_list[0], free_list[0])
return result
def recognize_from_image():
for image_path in args.input:
# prepare input data
logger.info(image_path)
img_gray = imread(image_path, cv2.IMREAD_GRAYSCALE)
img = imread(image_path)
# predict
result = predict(img, img_gray)
# print result
print('==============================================================')
for r in result:
print(f' word={r[1]} confidence={r[2]} bbox={r[0]}')
# draw result
draw_img = draw_ocr_box_txt(img, result)
cv2.imwrite(args.savepath, draw_img)
logger.info('Script finished successfully.')
def recognize_from_video():
capture = webcamera_utils.get_capture(args.video)
if args.savepath != SAVE_IMAGE_OR_VIDEO_PATH:
f_h = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
f_w = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)) * 2
video_writer = webcamera_utils.get_writer(args.savepath, f_h, f_w)
else:
video_writer = None
i = 0
frame_shown = False
while(True):
i += 1
ret, frame = capture.read()
if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret:
break
print(cv2.getWindowProperty('frame', cv2.WND_PROP_VISIBLE))
if frame_shown and cv2.getWindowProperty('frame', cv2.WND_PROP_VISIBLE) == 0:
break
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# predict
result = predict(frame, frame_gray)
# print result
print('==============================================================')
for r in result:
print(f' word={r[1]} confidence={r[2]} bbox={r[0]}')
# write a frame image to video
draw_frame = draw_ocr_box_txt(frame, result)
if video_writer is not None:
video_writer.write(draw_frame)
cv2.imshow('frame', draw_frame)
frame_shown = True
capture.release()
cv2.destroyAllWindows()
if video_writer is not None:
video_writer.release()
logger.info('finished process and write result to %s!' % args.savepath)
logger.info('Script finished successfully.')
if __name__ == '__main__':
# model files check and download
check_and_download_models(DETECTOR_WEIGHT_PATH, DETECTOR_MODEL_PATH, REMOTE_PATH)
check_and_download_models(RECOGNIZER_CHINESE_WEIGHT_PATH, RECOGNIZER_CHINESE_MODEL_PATH, REMOTE_PATH)
check_and_download_models(RECOGNIZER_JAPANESE_WEIGHT_PATH, RECOGNIZER_JAPANESE_MODEL_PATH, REMOTE_PATH)
check_and_download_models(RECOGNIZER_ENGLISH_WEIGHT_PATH, RECOGNIZER_ENGLISH_MODEL_PATH, REMOTE_PATH)
check_and_download_models(RECOGNIZER_FRENCH_WEIGHT_PATH, RECOGNIZER_FRENCH_MODEL_PATH, REMOTE_PATH)
check_and_download_models(RECOGNIZER_KOREAN_WEIGHT_PATH, RECOGNIZER_KOREAN_MODEL_PATH, REMOTE_PATH)
check_and_download_models(RECOGNIZER_THAI_WEIGHT_PATH, RECOGNIZER_THAI_MODEL_PATH, REMOTE_PATH)
# set model
detector = ailia.Net(DETECTOR_MODEL_PATH, DETECTOR_WEIGHT_PATH, env_id=args.env_id)
if args.language == 'chinese':
recognizer = ailia.Net(RECOGNIZER_CHINESE_MODEL_PATH, RECOGNIZER_CHINESE_WEIGHT_PATH, env_id=args.env_id)
lang_list = ['ch_sim','en']
character = recognition_models['zh_sim_g2']['characters']
symbol = recognition_models['zh_sim_g2']['symbols']
elif args.language == 'japanese':
recognizer = ailia.Net(RECOGNIZER_JAPANESE_MODEL_PATH, RECOGNIZER_JAPANESE_WEIGHT_PATH, env_id=args.env_id)
lang_list = ['ja','en']
character = recognition_models['japanese_g2']['characters']
symbol = recognition_models['japanese_g2']['symbols']
elif args.language == 'english':
recognizer = ailia.Net(RECOGNIZER_ENGLISH_MODEL_PATH, RECOGNIZER_ENGLISH_WEIGHT_PATH, env_id=args.env_id)
lang_list = ['en']
character = recognition_models['english_g2']['characters']
symbol = recognition_models['english_g2']['symbols']
elif args.language == 'french':
recognizer = ailia.Net(RECOGNIZER_FRENCH_MODEL_PATH, RECOGNIZER_FRENCH_WEIGHT_PATH, env_id=args.env_id)
lang_list = ['fr', 'en']
character = recognition_models['latin_g2']['characters']
symbol = recognition_models['latin_g2']['symbols']
elif args.language == 'korean':
recognizer = ailia.Net(RECOGNIZER_KOREAN_MODEL_PATH, RECOGNIZER_KOREAN_WEIGHT_PATH, env_id=args.env_id)
lang_list = ['ko', 'en']
character = recognition_models['korean_g2']['characters']
symbol = recognition_models['korean_g2']['symbols']
elif args.language == 'thai':
recognizer = ailia.Net(RECOGNIZER_THAI_MODEL_PATH, RECOGNIZER_THAI_WEIGHT_PATH, env_id=args.env_id)
lang_list = ['th']
character = recognition_models['thai_g1']['characters']
symbol = recognition_models['thai_g1']['symbols']
else:
logger.info('invalid language.')
exit()
# predict
if args.video is not None:
# video mode
recognize_from_video()
else:
# image mode
recognize_from_image()