-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathocr_util.py
41 lines (34 loc) · 1.1 KB
/
ocr_util.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
from aip import AipOcr
# 认证信息
APP_ID = ''
API_KEY = 'b52a2d05d6c644518b15d747e795b78c'
SECRET_KEY = 'ceb97436c7e046468dd578db104c6025'
def get_ocr_str(file_path, origin_format=True):
"""
图片转文字
:param file_path: 图片路径
:return:
"""
with open(file_path, 'rb') as fp:
file_bytes = fp.read()
return get_ocr_str_from_bytes(file_bytes, origin_format)
def get_ocr_str_from_bytes(file_bytes, origin_format=True):
"""
图片转文字
:param file_bytes: 图片的字节
:return:
"""
options = {
'detect_direction': 'false',
'language_type': 'CHN_ENG',
}
ocr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
result_dict = ocr.basicGeneral(file_bytes, options)
if origin_format:
result_str = '\n'.join([entity['words'] for entity in result_dict['words_result']])
else:
result_str = ''.join([entity['words'] for entity in result_dict['words_result']])
return result_str
if __name__ == '__main__':
IMAGE_PATH = "test.jpg"
print(get_ocr_str(IMAGE_PATH))