-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOCRGUIEXL_V1.0.py
234 lines (178 loc) · 8 KB
/
OCRGUIEXL_V1.0.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#V1.4 创建一个单独线程用于调用百度OCR接口,避免调用时间过长导致界面程序卡顿
import wx
from aip import AipOcr
from PIL import ImageGrab
from PIL import Image
import io
from threading import Thread
from pubsub import pub
#from wx.lib.pubsub import pub
import filetype
import time
import requests
import os
# """ 你的 APPID AK SK """
APP_ID = '11389544'
API_KEY = '8mkOOPxRRXbhaPDChh7jeKPM'
SECRET_KEY = '8pGmWq2o3loYlNLHNkSxRbBiek3h5Vdd'
# """ OCR可选参数 """
options = {}
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
options["detect_language"] = "true"
options["probability"] = "true"
#以二进制方式读取本地图片文件
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
#创建一个单独线程用于调用百度OCG,避免调用时间过长导致界面卡顿
class threadocr(Thread):
def __init__(self):
"""Init Worker Thread Class."""
Thread.__init__(self)
self.start() # start the thread
def run(self):
# """ 带参数调用通用文字识别, 图片参数为本地图片 """
# AipOcr.setConnectionTimeoutInMillis(self,ms=5000) #设置连接超时时间,一般没必要设置
# AipOcr.setSocketTimeoutInMillis(self,ms=6000) #设置传送文件超时时间,一般么没必要设置
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
#result={}
try:
result=client.tableRecognitionAsync(image,options)
print(result)
if result.get("error_msg"):
print(result['error_msg'])
contents.SetValue(result['error_msg'])
else:
requestid=result['result'][0]['request_id']
print(requestid)
#获取表格识别结果
try:
for i in range(5):
print(i)
time.sleep(4)
optiontype={'result_type':'excel'}
resultexcel=client.getTableRecognitionResult(requestid,optiontype)
print(resultexcel)
if resultexcel.get("error_msg"):
print(result['error_msg'])
contents.SetValue(resultexcel['error_msg'])
else:
if resultexcel.get('result')['ret_msg']=="已完成":
resultexcelurl=resultexcel.get('result')['result_data']
wx.CallAfter(pub.sendMessage,'update',re_msg="以下为excel文件保存目录")
wx.CallAfter(pub.sendMessage,'update',re_msg=os.getcwd())
downwork=requests.get(resultexcelurl)
with open(r'data.xls','wb') as data:
data.write(downwork.content)
break
else:
if i==4:
print('超时,请重试')
wx.CallAfter(pub.sendMessage,'update',re_msg='超时,请重试')
continue
except:
print('发生错误2')
wx.CallAfter(pub.sendMessage,'update',re_msg='发生错误2\n请重试')
# wx.CallAfter(pub.sendMessage,'update',re_msg="以下为识别内容")
# wx.CallAfter(pub.sendMessage,'update',re_msg=resultexcelurl)
except:
print('发生错误1')
wx.CallAfter(pub.sendMessage,'update',re_msg="发生错误1\n请重试")
class MyForm(wx.Frame):
#-------------------------------------------------------------------
#set the window layout
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "OCR_EXCEL程序", size =(410,335))
global filename,contents,get_file_content,imgmark
imgmark=""
#app=wx.App()
#win=wx.Frame(None,title='OCR程序',size=(410,335))
bkg=wx.Panel(self,wx.ID_ANY)
loadButton=wx.Button(bkg,label='选择文件')
loadButton.Bind(wx.EVT_BUTTON,self.choose)
ocrButton=wx.Button(bkg,label='识别表格图片')
ocrButton.Bind(wx.EVT_BUTTON,self.OCR)
clipButton=wx.Button(bkg,label="获取剪贴板")
clipButton.Bind(wx.EVT_BUTTON,self.clipOCR)
filename=wx.TextCtrl(bkg)
contents=wx.TextCtrl(bkg,style=wx.TE_MULTILINE | wx.HSCROLL)
#contents.SetStyle(410,335,wx.TextAttr("RED","YELLOW"))
hbox=wx.BoxSizer()
hbox.Add(filename,1,wx.EXPAND)
hbox.Add(loadButton,0,wx.LEFT,5)
hbox.Add(clipButton,0,wx.LEFT,5)
hbox.Add(ocrButton,0,wx.LEFT,5)
vbox=wx.BoxSizer(wx.VERTICAL)
vbox.Add(hbox,0,wx.EXPAND | wx.ALL,5)
vbox.Add(contents,1,wx.EXPAND | wx.LEFT |wx.BOTTOM | wx.RIGHT,5)
bkg.SetSizer(vbox)
#创建一个接收器
pub.subscribe(self.updatedispaly,'update')
# 创建一个文件对话框,用于选择需要识别的图片并
def choose(self,event):
global imgmark
wildcard1 = "All files (*.*)|*.*|" \
"Python source (*.py; *.pyc)|*.py;*.pyc" #文件过滤器
dlg = wx.FileDialog(self,message="Choose a file",defaultFile="",wildcard=wildcard1,style=wx.FD_OPEN | wx.FD_CHANGE_DIR)
tmp=""
if dlg.ShowModal() == wx.ID_OK:
paths = dlg.GetPaths() #返回的是一个包含路径字符串的列表
tmp=paths[0] #只取第一个文的路径,不做多选
#filename.SetValue(tmp)
gusstype=filetype.guess_extension(tmp)
print(gusstype)
if gusstype in ['jpg','png','bmp']:
print('已选取图片')
contents.SetValue("已选取图片\n\n")
filename.SetValue(tmp)
imgmark="已选取图片"
else:
print('请选取格式为jpg、png或bmp格式的图片')
contents.SetValue("请选取格式为jpg、png或bmp格式的图片\n\n")
else:
print('还未选择图片')
contents.SetValue('还未选择图片\n\n')
dlg.Destroy()
#创建获取剪贴板函数,通过PIL模块实现
def clipOCR(self,event):
global imagefile,imgmark
im = ImageGrab.grabclipboard()
imagefile=io.BytesIO()
#photo=Image.new('RGB',(1000,1000))
if isinstance(im, Image.Image):
print(im.format, im.size, im.mode)
contents.SetValue("已获取剪贴板图片\n\n")
im.save(imagefile,'JPEG')
imagefile.seek(0)
imgmark='已获取剪贴板图片'
else:
contents.SetValue("剪贴板无图片\n\n")
#@timeout_decorator.timeout(10,use_signals=False,timeout_exception=TimeoutError)
#
def OCR(self,event):
global image,imgmark
#AipOcr.setConnectionTimeoutInMillis(self,2000)
#AipOcr.setSocketTimeoutInMillis(self,3000)
#print(imgmark)
if imgmark == "已获取剪贴板图片":
image=imagefile.read()
#@timeout_decorator.timeout(10,use_signals=False)
contents.AppendText("请稍等\n\n")
threadocr()
elif imgmark== "已选取图片":
image = get_file_content(filename.GetValue())
contents.AppendText("请稍等\n\n")
threadocr()
else:
contents.SetValue("请选择图片或粘贴图片\n\n")
#return image
def updatedispaly(self,re_msg):
displaymessage=re_msg
contents.AppendText(displaymessage+'\n\n')
#主函数
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()