利用Python和百度开放API接口,写一个文字或表格识别软件
本帖最后由 990708 于 2020-9-3 15:13 编辑在论坛看到了有人分享截图识别软件的制作,就想到把自己以前做的一个表格和文字识别的软件分享出来
但由于本人菜鸟编程水平,没有做后续的优化,有能力的大佬可以优化代码{:301_1001:}
代码中GUI绘制用的是PYQT5及相关的模块,代码测试好后用pyinstaller打包成exe使用
打开后输入app_id,api_key,api_secret后选择文件,然后选择图片开始识别,推荐使用高精度的,准确率较高,但是每日免费次数较少
成品自带我自己的免费接口,等哪天我想起来打开百度开放API的时候就关了吧{:1_926:}
表格识别因为百度接口原因只能在识别结果网址中下载打开,本人水平有限{:301_1008:}
2020年9月3日15:09:26 更新
我想起来了各位{:301_1001:},虽然现在用的人不多,但是我等小屁民还是怕免费次数超标,所以今天我就把api关闭了。
各位如果有时间的话可以自己申请一个使用,https://cloud.baidu.com/doc/OCR/s/Ek3h7xypm。
可以去看下官方文档创建一个应用即可使用。望君前程似锦!
下面是效果图
原图
https://attach.52pojie.cn//forum/201910/09/143158tqmp6ffqbfdfxwrr.jpg?l
文字识别(普通) 文字识别高精度
https://attach.52pojie.cn//forum/201910/09/143059e64b64o6ssbszpa8.png?l https://attach.52pojie.cn//forum/201910/09/143106c57ncs0vurv2v0ju.png?l
成品和源代码打包分享
蓝奏云:https://www.lanzouj.com/b00t337ha/密码:4pv7
百度云:链接: https://pan.baidu.com/s/1nTDoLryaOuAqWfTP25jVFQ 提取码: xmej
以下为源代码
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ocr.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
# WARNING! All changes made in this file will be lost!
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from aip import AipOcr
def get_file(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
def message(Title,Text):
message = QMessageBox()
message.addButton(QPushButton('确定'), QMessageBox.YesRole)
message.setWindowTitle(Title)
message.setText(Text)
message.exec_()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.mod = 0
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1024,720)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setObjectName("result")
self.gridLayout.addWidget(self.textEdit,0,0,50,1)
self.textEdit_2 = QtWidgets.QTextEdit(self.centralwidget)
self.gridLayout.addWidget(self.textEdit_2,0,1,1,3)
self.textEdit_2.setObjectName("filepath")
self.textEdit_2.isReadOnly()
self.textEdit_3 = QtWidgets.QTextEdit(self.centralwidget)
self.gridLayout.addWidget(self.textEdit_3,3,1,1,3)
self.textEdit_3.setObjectName("url")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.gridLayout.addWidget(self.pushButton,2,2,1,1)
self.pushButton.setObjectName("choose_filepath")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.gridLayout.addWidget(self.pushButton_2,49,3,1,1)
self.pushButton_2.setObjectName("start_OCR")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.gridLayout.addWidget(self.pushButton_3,4,2,1,1)
self.pushButton_3.setObjectName("set_url")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.gridLayout.addWidget(self.pushButton_4,49,1,1,1)
self.pushButton_4.setObjectName("set_url")
self.textEdit_4 = QtWidgets.QTextEdit(self.centralwidget)
self.gridLayout.addWidget(self.textEdit_4,5,1,1,2)
self.textEdit_4.setObjectName("app_id")
self.label = QtWidgets.QLabel(self.centralwidget)
self.gridLayout.addWidget(self.label,5,3,1,1)
self.label.setObjectName("set_app_id")
self.textEdit_5 = QtWidgets.QTextEdit(self.centralwidget)
self.gridLayout.addWidget(self.textEdit_5,6,1,1,2)
self.textEdit_5.setObjectName("app_id")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.gridLayout.addWidget(self.label_2,6,3,1,1)
self.label_2.setObjectName("set_api_key")
self.textEdit_6 = QtWidgets.QTextEdit(self.centralwidget)
self.gridLayout.addWidget(self.textEdit_6,7,1,1,2)
self.textEdit_6.setObjectName("secret_key")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.gridLayout.addWidget(self.label_3,7,3,1,1)
self.label_3.setObjectName("set_secret")
self.radiobutton = QtWidgets.QRadioButton(self.centralwidget)
self.gridLayout.addWidget(self.radiobutton,8,1,1,1)
self.radiobutton.setChecked(True)
self.radiobutton_2 = QtWidgets.QRadioButton(self.centralwidget)
self.gridLayout.addWidget(self.radiobutton_2,8,2,1,1)
self.radiobutton_3 = QtWidgets.QRadioButton(self.centralwidget)
self.gridLayout.addWidget(self.radiobutton_3,9,1,1,1)
self.radiobutton_4 = QtWidgets.QRadioButton(self.centralwidget)
self.gridLayout.addWidget(self.radiobutton_4,9,2,1,1)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.setFont(QtGui.QFont("华文新魏", 15))
self.pushButton_2.setFont(QtGui.QFont("华文新魏", 15))
self.pushButton_3.setFont(QtGui.QFont("华文新魏", 15))
self.pushButton_4.setFont(QtGui.QFont("华文新魏", 15))
self.textEdit.setFont(QtGui.QFont("", 10))
self.textEdit.setText("识别前请填入app_id、api_key和secret_key\r\n"
"填入完成后选择本地文件或者输入网址\r\n"
"文字识别完成后会显示在这里\r\n"
"图片识别完成后是一个链接,打开下载到本地用Excel打开即可\r\n")
self.textEdit_2.setFont(QtGui.QFont("华文新魏", 10))
self.textEdit_2.setText("等待选择文件")
self.textEdit_3.setFont(QtGui.QFont("华文新魏", 10))
self.textEdit_3.setText("等待输入网址")
self.textEdit_4.setFont(QtGui.QFont("华文新魏", 10))
self.textEdit_4.setText("")#app_id
self.textEdit_5.setFont(QtGui.QFont("华文新魏", 10))
self.textEdit_5.setText("")#api_key
self.textEdit_6.setFont(QtGui.QFont("华文新魏", 10))
self.textEdit_6.setText("")#api_secert
self.label.setText("APP_ID")
self.label.setFont(QtGui.QFont("华文新魏",15))#设置字体样式
self.label.setAlignment(QtCore.Qt.AlignCenter)#居中
self.label_2.setText("API_KEY")
self.label_2.setFont(QtGui.QFont("华文新魏", 15))# 设置字体样式
self.label_2.setAlignment(QtCore.Qt.AlignCenter)# 居中
self.label_3.setText("SECRET")
self.label_3.setFont(QtGui.QFont("华文新魏", 15))# 设置字体样式
self.label_3.setAlignment(QtCore.Qt.AlignCenter)# 居中
self.radiobutton.setText("文字识别")
self.radiobutton.setFont(QtGui.QFont("华文新魏", 13))
self.radiobutton_2.setText("文字识别(高精度)")
self.radiobutton_2.setFont(QtGui.QFont("华文新魏", 13))
self.radiobutton_3.setText("表格识别")
self.radiobutton_3.setFont(QtGui.QFont("华文新魏", 13))
self.radiobutton_4.setText("表格识别(高精度)")
self.radiobutton_4.setFont(QtGui.QFont("华文新魏", 13))
self.pushButton.clicked.connect(self.choosefile)
self.pushButton_2.clicked.connect(self.start)
self.pushButton_3.clicked.connect(self.seturl)
self.pushButton_4.clicked.connect(self.clear)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Simple OCR"))
self.pushButton.setText(_translate("MainWindow", "选择文件"))
self.pushButton_2.setText(_translate("MainWindow", "开始识别"))
self.pushButton_3.setText(_translate("MainWindow","输入网址"))
self.pushButton_4.setText(_translate("MainWindow", "清空"))
def choosefile(self):
self.filename,self.filetype = QFileDialog.getOpenFileName(directory="C:\\Users\\Administrator\\Desktop",filter="JPG Files (*.jpg);;PNG Files (*.png);;BMP Files (*.bmp)")
self.textEdit_2.setText(self.filename)
self.filepath = self.textEdit_2.toPlainText()
self.filepath.replace("/","\\")
self.mod = 1
self.textEdit_3.setText("等待输入网址")
def start(self):
messageBox = QMessageBox()
if self.textEdit_4.toPlainText() == "等待输入APP_ID" or self.textEdit_5.toPlainText() == "等待输入API_KEY"or self.textEdit_6.toPlainText() =="等待输入SECRET_KEY":
message('警告','请输入正确的信息')
else:
self.app_id = self.textEdit_4.toPlainText()
self.api_key = self.textEdit_5.toPlainText()
self.secert = self.textEdit_6.toPlainText()
self.client = AipOcr(self.app_id,self.api_key,self.secert)
if self.radiobutton.isChecked():
#文字识别
if self.mod == 1:
self.img = get_file(self.filepath)
self.result = self.client.basicGeneral(self.img)
self.words = self.result["words_result"]
self.textEdit.setText("")
self.content = ""
for self.word in self.words:
self.content = self.content + self.word["words"] + "\r\n"
self.textEdit.insertPlainText(self.content)
elif self.mod == 2:
self.result = self.client.basicGeneralUrl(self.url)
self.words = self.result["words_result"]
self.textEdit.setText("")
self.content = ""
for self.word in self.words:
self.content = self.content + self.word["words"] + "\r\n"
self.textEdit.insertPlainText(self.content)
else:
message('警告','请选择文件或输入网址')
elif self.radiobutton_2.isChecked():
#文字识别(高精度)
if self.mod == 1:
self.img = get_file(self.filepath)
self.result = self.client.basicAccurate(self.img)
self.words = self.result["words_result"]
self.textEdit.setText("")
self.content = ""
for self.word in self.words:
self.content = self.content + self.word["words"] + "\r\n"
self.textEdit.insertPlainText(self.content)
elif self.mod == 2:
message('警告','该模式不支持网络图片\r\n请选择本地图片')
else:
message('警告','请选择文件或输入网址')
elif self.radiobutton_3.isChecked():
if self.mod == 1:
self.img = get_file(self.filepath)
self.result = self.client.tableRecognition(self.img)["result"]
self.result_data = self.result["result_data"]
self.textEdit.setText("网址为: "+ self.result_data + "\r\n请下载")
elif self.mod == 2:
message('警告','该模式不支持网络图片\r\n请选择本地图片')
else:
message('警告','请选择文件或输入网址')
#表格识别
elif self.radiobutton_4.isChecked():
message('警告','该模式不可用')
def seturl(self):
if self.textEdit_3.toPlainText() == "等待输入网址":
message('警告','请输入网址')
else:
self.url = self.textEdit_3.toPlainText()
self.mod = 2
self.textEdit_2.setText("等待选择文件")
def clear(self):
self.textEdit.setText("识别前请填入app_id、api_key和secret_key\r\n"
"填入完成后选择本地文件或者输入网址\r\n"
"文字识别完成后会显示在这里\r\n"
"图片识别完成后是一个链接,打开下载到本地用Excel打开即可\r\n")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
mainwindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(mainwindow)
mainwindow.show()
sys.exit(app.exec_())
Antigen 发表于 2019-10-9 15:27
你们的百度API都是无限次免费的吗?
不是啊,每天有限制的,返回不了信息可能程序就崩溃了吧{:301_1001:} lxy971004 发表于 2020-1-7 19:09
老哥可以拿走去做个毕设嘛
可以,你自己再优化一下吧,写的比较粗超{:1_926:} 厉害,我才刚刚开始学python{:1_918:} 你们的百度API都是无限次免费的吗? 感谢楼主,学习一下! API什么的从哪里获取呢? vanhongyan 发表于 2020-1-6 10:06
API什么的从哪里获取呢?
百度开放API官网去获取 感谢分享 老哥可以拿走去做个毕设嘛{:1_893:}
页:
[1]
2