This repository was archived by the owner on Nov 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathocr.py
More file actions
54 lines (45 loc) · 1.54 KB
/
ocr.py
File metadata and controls
54 lines (45 loc) · 1.54 KB
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
from aip import AipOcr
from settings import API_KEY,APP_ID,SECRET_KEY
class OCR(object):
def __init__(self):
self.client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
@staticmethod
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
content = fp.read()
fp.close()
return content
def getResult(self,filePath):
'''
返回OCR识别结果
:param filePath: 图片路径
:return: {'code': 0/1 (是否成功),'text':str (信息)}
'''
image = OCR.get_file_content(filePath)
options = {}
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
options["detect_language"] = "true"
options["probability"] = "true"
res = self.client.basicGeneral(image,options)
output = {'code': 0,
'text': ''}
# 出错情况
if( 'error_code' in res):
output['code'] = 0
if(res['error_code'] == '17'):
output['text'] = "每天流量超限额"
else:
output['text'] = '错误代码:{}'.format(res['error_code'])
# 正常情况
else:
output['code'] = 1
text = ''
for elem in res['words_result']:
text = text + elem['words'] + '\n'
output['text'] = text
return output
# if __name__ == '__main__':
# test = OCR()
# a = test.getResult('H:\\pro\\WebOCR\\img\\2018-02-02T22-42-08.PNG')
# print(a)