这是基于百度aip的图片文字识别,识别率还可以,自带gui界面。感觉很好玩。
# coding =utf-8
'''
name:图片识别
function:将图片里面的文字识别出来
author:ww
time:2019/1/8 13:30
software:PyCharm
'''
from tkinter import *
from tkinter.filedialog import askdirectory
from os import path
import tkinter
from aip import AipOcr
# 定义常量
APP_ID = '15345466'
API_KEY = 'u4Uh1pg7rNBi0VQazfPSCQB7'
SECRET_KEY = 'ivmLcC3FWCUy6cwqHG2ViiObLNFbmEa5'
# 初始化AipFace对象
aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
global testd
testd = ''
global path_
def selectPath():
global path_
path_ = tkinter.filedialog.askopenfilename()
path.set(path_)
def get():
def get_file_content(path_):
with open(path_, 'rb') as fp:
return fp.read()
# 定义参数变量
options = {'detect_direction': 'true', 'language_type': 'CHN_ENG', }
# 调用通用文字识别接口
result = aipOcr.basicGeneral(get_file_content(path_), options)
a = result['words_result']
testd = ''
for i in a:
for k, v in i.items():
print(k, v)
testd += v
testd += '\n'
text_result.delete(0.0, END)
text_result.insert(1.0, testd)
root = Tk()
path = StringVar()
root.title("图片文字识别") # 设置窗口标题
root.geometry("500x400") # 设置窗口大小 注意:是x 不是*
root.resizable(width=False, height=True) # 设置窗口是否可以变化长/宽,False不可变,True可变,默认为True
# text = tkinter.Text(root).grid(row=2, column=1)
Label(root, text="").grid(row=0, column=0)
Button(root, text="路径选择", command=selectPath).grid(row=1, column=0)
Label(root, text="图片路径:").grid(row=1, column=0, rowspan=10, columnspan=10)
Entry(root, textvariable=path).grid(row=1, column=1, ipadx=1, ipady=1, columnspan=1, rowspan=1)
Button(root, text="转换文字", command=get).grid(row=2, column=0)
text_result = Text(root, width=68, height=20)
text_result.grid(row=4, column=0, columnspan=2, sticky=W, padx=10)
Label(root, text="-- by ww").grid(row=8, column=1, sticky=E, padx=10, pady=10)
root.mainloop()
运行效果
点击路径选择选择图片
点击转换文字,运行效果。
是不是很好玩。