-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOCR_PaddleOCR.py
117 lines (95 loc) · 3.71 KB
/
OCR_PaddleOCR.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
from paddleocr import PaddleOCR
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.scrolledtext import ScrolledText
from threading import Thread
import queue
import time
import filetype
import pyperclip
from PIL import ImageGrab
from PIL import Image
import numpy as np
import os
import sys
sys.coinit_flags = 2
class GUI(Frame):
def __init__(self,parent=None, **kwargs):
Frame.__init__(self,parent,**kwargs)
self.config(height=500,width=300,bg="white")
self.pack()
self.dataqueue=queue.Queue(maxsize=100)
#self.var=StringVar()
#openfile=lambda buttonReturn=askopenfilename:self.fileOCR(buttonReturn)
self.FileOCRButton=Button(self,text="本地图片识别",command=self.filemakethread)
self.FileOCRButton.grid(row=0,sticky=E,padx=15,pady=3)
self.ClipOCRButton=Button(self,text="剪贴板图片识别",command=self.clipmakethread)
self.ClipOCRButton.grid(row=0,sticky=W,padx=15,pady=3)
self.content=ScrolledText(self)
self.content.grid(row=1,sticky=W+E+N+S)
#self.content.pack(side=BOTTOM,expand=YES,fill=BOTH)
self.contentDisplay()
def contentDisplay(self):
try:
data=self.dataqueue.get(block=False)
except queue.Empty:
pass
else:
self.content.insert('end',"%s\n" % str(data))
self.content.see('end')
self.content.after(50,self.contentDisplay)
def fileOCR(self):
self.content.delete(1.0,"end")
filepath = askopenfilename()
filekind=filetype.guess_extension(filepath)
if filekind in ['jpg','png','bmp']:
filename=os.path.split(filepath)
self.dataqueue.put("已获取本地图片文件: %s\n" % filename[1])
contentR = []
ocr = PaddleOCR(use_angle_cls=True)
result = ocr.ocr(filepath, cls=True)
for line in result:
contentR.append(line[1][0])
self.ocrcontent = '\n'.join(contentR)
print(contentR)
self.dataqueue.put("%s" % self.ocrcontent)
#把内容写入剪贴板
try:
pyperclip.copy(self.ocrcontent)
self.dataqueue.put("\n识别内容写入剪贴板")
except:
self.dataqueue.put("\n 识别内容写入剪贴板失败")
else:
self.dataqueue.put("文件不是图片,无法识别!")
def clipOCR(self):
self.content.delete(1.0, "end")
im=ImageGrab.grabclipboard()
if isinstance(im,Image.Image):
imdata=np.array(im)
print(im.format, im.size, im.mode)
self.dataqueue.put("已读取剪贴板图片,图片格式%s,图片大小%s,图片模式%s \n" % (im.format,im.size,im.mode))
contentR = []
ocr = PaddleOCR(use_angle_cls=True)
result = ocr.ocr(imdata, cls=True)
for line in result:
contentR.append(line[1][0])
self.ocrcontent = '\n'.join(contentR)
print(contentR)
self.dataqueue.put("%s" % self.ocrcontent)
# 把内容写入剪贴板
try:
pyperclip.copy(self.ocrcontent)
self.dataqueue.put("\n识别内容写入剪贴板")
except:
self.dataqueue.put("\n 识别内容写入剪贴板失败")
else:
self.dataqueue.put("剪贴板无图片!")
def filemakethread(self):
Thread(target=self.fileOCR,args=()).start()
def clipmakethread(self):
Thread(target=self.clipOCR,args=()).start()
if __name__=="__main__":
root=Tk()
root.title("OCRPaddle")
OCRGUI=GUI(root)
OCRGUI.mainloop()