-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathocrapi.py
176 lines (145 loc) · 5.34 KB
/
ocrapi.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
# -*- coding: UTF-8 -*-
import time
import uuid
import base64
import urllib.parse
import hashlib
import requests
from gl import Global
def get_file_content(file_path):
with open(file_path, "rb") as fp:
return fp.read()
def get_exp_account():
url = Global.config.get_value("experience_account_url")
t = int(time.time())
data = dict({
"token": hashlib.md5(str(t).encode("utf-8")).hexdigest().upper(),
"timestamp": t
})
try:
response = post_request(url, None, data).json()
if response["status"] == 0:
return True, response["app_id"], response["app_key"]
return False, None, None
except:
Global.gui.show_message_box("错误", "获取共享API过程中出现网络错误!\n请检查网络连接后重试")
return False, None, None
def post_request(url, params, data):
return requests.post(url, params=params, data=data)
class BdOcr:
def __init__(self):
self.api_key = Global.config.get_value("bd_api_key")
self.secret_key = Global.config.get_value("bd_secret_key")
self.token = None
self.api_list = {
"general_basic": self.__general_basic,
"general": self.__general,
"accurate_basic": self.__accurate_basic,
"accurate": self.__accurate,
"web_image": self.__web_image,
}
self.api_url_list = {
"token": Global.config.get_value("bd_access_token_url"),
"general_basic": Global.config.get_value("bd_general_basic_url"),
"general": Global.config.get_value("bd_general_url"),
"accurate_basic": Global.config.get_value("bd_accurate_basic_url"),
"accurate": Global.config.get_value("bd_accurate_url"),
"web_image": Global.config.get_value("bd_web_image_url")
}
def __del__(self):
print("BdOcr has been destroyed.")
def get_token(self):
params = {
"grant_type": "client_credentials",
"client_id": self.api_key,
"client_secret": self.secret_key
}
response = requests.post(url=self.api_url_list["token"], data=params).json()
if isinstance(response.get("error", -1), int):
self.token = response["access_token"]
return True
return False
def __general_basic(self, kwargs):
image2base64 = base64.b64encode(get_file_content(kwargs["image"]))
ocr_url = self.api_url_list["general_basic"]
params = {
"access_token": self.token
}
data = {
"image": image2base64,
"language_type": kwargs["lang"],
"probability": "true"
}
return post_request(ocr_url, params, data).json()
def __general(self, kwargs):
image2base64 = base64.b64encode(get_file_content(kwargs["image"]))
ocr_url = self.api_url_list["general"]
params = {
"access_token": self.token
}
data = {
"image": image2base64,
"language_type": kwargs["lang"],
"probability": "true"
}
return post_request(ocr_url, params, data).json()
def __accurate_basic(self, kwargs):
image2base64 = base64.b64encode(get_file_content(kwargs["image"]))
ocr_url = self.api_url_list["accurate_basic"]
params = {
"access_token": self.token
}
data = {
"image": image2base64,
"probability": "true"
}
return post_request(ocr_url, params, data).json()
def __accurate(self, kwargs):
image2base64 = base64.b64encode(get_file_content(kwargs["image"]))
ocr_url = self.api_url_list["accurate"]
params = {
"access_token": self.token
}
data = {
"image": image2base64,
"probability": "true"
}
return post_request(ocr_url, params, data).json()
def __web_image(self, kwargs):
image2base64 = base64.b64encode(get_file_content(kwargs["image"]))
ocr_url = self.api_url_list["web_image"]
params = {
"access_token": self.token
}
data = {
"image": image2base64
}
return post_request(ocr_url, params, data).json()
def get_ocr(self, api="general_basic", **kwargs):
return self.api_list[api](kwargs)
class TxOcr:
def __init__(self, _id, _key):
self.app_id = _id
self.app_key = _key
self.general_ocr_url = Global.config.get_value("tx_general_ocr_url")
def __del__(self):
print("TxOcr has been destroyed.")
@staticmethod
def __generate_signature(_params, _app_key):
_sorted_keys = sorted(_params.keys())
_str = ""
for _key in _sorted_keys:
if _params[_key] == "":
continue
_str += urllib.parse.urlencode({_key: _params[_key]}) + "&"
_str += "app_key=" + _app_key
return hashlib.md5(_str.encode("utf-8")).hexdigest().upper()
def get_ocr(self, image):
data = dict({
"app_id": self.app_id,
"time_stamp": int(time.time()),
"nonce_str": uuid.uuid4().hex,
"image": base64.b64encode(get_file_content(image))
})
data["sign"] = self.__generate_signature(data, self.app_key)
return post_request(self.general_ocr_url, None, data).json()