#获取token_access
def client():
""" 你的 APPID AK SK """
from aip import AipOcr
""" 你的 APPID AK SK """
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
return AipOcr(APP_ID, API_KEY, SECRET_KEY)
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
#上传照片进行提取文字
import Client
def get_result(path):
image = Client.get_file_content(path)
client = Client.client()
""" 如果有可选参数 """
""" 如果有可选参数 """
options = {}
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
options["detect_language"] = "true"
options["probability"] = "true"
""" 带参数调用通用文字识别, 图片参数为本地图片 """
res = client.basicGeneral(image, options)
print(res)
if res["words_result"]:
return res["words_result"]
else:
return '没有检测到'
#将检测到的结果,显示在桌面上,主程序
from tkinter import *
from PIL import Image, ImageTk
from tkinter.filedialog import askopenfilename
from objectIdentify import *
from objectIdentify import get_result
class Im:
def __init__(self):
self.text = Text(tk, width=40, height=30)
self.t = Text(tk, width=40, height=30)
self.scroll = Scrollbar()
def start(self):
tk.geometry('620x500')
Button(tk, text='上传图片', command=im.choiceImg).place(x=310, y=0)
def choiceImg(self):
# 将滚动条填充
msg = ''
path = askopenfilename(title='选择文件')
load = Image.open(path)
render = ImageTk.PhotoImage(load)
self.text.image_create(END, image=render) # 用这个方法创建一个图片对象,并插入到“END”的位置
img = Label(image=render)
img.image = render
self.text.place(x=0, y=50)
for s in get_result(path):
msg += s['words']
self.showResult(msg)
def showResult(self, msg):
# 将滚动条填充
self.scroll.pack(side=RIGHT, fill=Y) # side是滚动条放置的位置,上下左右。fill是将滚动条沿着y轴填充
self.t.pack(side=LEFT, fill=Y) # 将文本框填充进wuya窗口的左侧,
# 将滚动条与文本框关联
self.scroll.config(command=self.t.yview) # 将文本框关联到滚动条上,滚动条滑动,文本框跟随滑动
self.t.config(yscrollcommand=self.scroll.set) # 将滚动条关联到文本框
self.t.insert(END, msg)
self.t.insert(END, '\n')
self.t.insert(END, '\n')
self.t.insert(END, '\n')
self.t.place(x=300, y=50)
if __name__=='__main__':
tk = Tk()
im = Im()
im.start()
tk.mainloop()