吾爱破解 - LCG - LSG |安卓破解|病毒分析|破解软件|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 657|回复: 4
上一主题 下一主题

[Python] 利用Python和百度开放API接口,写一个文字或表格识别软件

[复制链接]
跳转到指定楼层
楼主
990708 发表于 2019-10-9 14:40 回帖奖励
本帖最后由 990708 于 2019-10-9 14:42 编辑

在论坛看到了有人分享截图识别软件的制作,就想到把自己以前做的一个表格和文字识别的软件分享出来
但由于本人菜鸟编程水平,没有做后续的优化,有能力的大佬可以优化代码逻辑

代码中GUI绘制用的是PYQT5及相关的模块,代码测试好后用pyinstaller打包成exe使用

打开后输入app_id,api_key,api_secret后选择文件,然后选择图片开始识别,推荐使用高精度的,准确率较高,但是每日免费次数较少
成品自带我自己的免费接口,等哪天我想起来打开百度开放API的时候就关了吧
表格识别因为百度接口原因只能在识别结果网址中下载打开,本人水平有限

下面是效果图
原图

文字识别(普通)                                                文字识别高精度
                                                

成品和源代码打包分享
蓝奏云:https://www.lanzous.com/b00t337ha/  密码:4pv7
百度云:链接: https://pan.baidu.com/s/1nTDoLryaOuAqWfTP25jVFQ 提取码: xmej

以下为源代码


[Python] 纯文本查看 复制代码
# -*- 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_())

QQ截图20191009141847.png (40.85 KB, 下载次数: )

效果图

效果图

免费评分

参与人数 3吾爱币 +4 热心值 +3 收起 理由
苏紫方璇 + 2 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
waMoYa + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
ai474427793 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

沙发
waMoYa 发表于 2019-10-9 15:16
厉害,我才刚刚开始学python
3#
Antigen 发表于 2019-10-9 15:27
4#
 楼主| 990708 发表于 2019-10-9 16:33 <
Antigen 发表于 2019-10-9 15:27
你们的百度API都是无限次免费的吗?

不是啊,每天有限制的,返回不了信息可能程序就崩溃了吧
5#
Kaiter_Plus 发表于 2019-10-9 16:34
感谢楼主,学习一下!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:禁止回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2019-10-16 10:01

Powered by Discuz!

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表