Python + paddleocr 进行文字识别 并使用PyQt制作简单识别界面(2)

本文介绍了使用PaddleOCR库在Python环境中创建一个带有图像显示、识别和结果展示功能的文字识别界面,用户可以上传图片,进行OCR识别,并显示识别结果。
摘要由CSDN通过智能技术生成

接上一篇文章。

4. 简单文字识别界面

5. 代码 ocr_recongition_Ui.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ocr_recongition.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from paddleocr import PaddleOCR, draw_ocr
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtGui import QMessageBox
from PIL import Image
import sip
import sys
import os

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(QtGui.QMainWindow):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(764, 555)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.groupBox = QtGui.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(20, 20, 721, 501))
        self.groupBox.setTitle(_fromUtf8(""))
        self.groupBox.setObjectName(_fromUtf8("groupBox"))
        self.label = QtGui.QLabel(self.groupBox)
        self.label.setGeometry(QtCore.QRect(300, 20, 131, 51))
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setObjectName(_fromUtf8("label"))
        self.groupBox_2 = QtGui.QGroupBox(self.groupBox)
        self.groupBox_2.setGeometry(QtCore.QRect(20, 60, 401, 381))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.groupBox_2.setFont(font)
        self.groupBox_2.setObjectName(_fromUtf8("groupBox_2"))
        self.pushButton = QtGui.QPushButton(self.groupBox_2)
        self.pushButton.setGeometry(QtCore.QRect(130, 332, 131, 41))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.label_2 = QtGui.QLabel(self.groupBox_2)
        self.label_2.setGeometry(QtCore.QRect(30, 30, 341, 271))
        self.label_2.setText(_fromUtf8(""))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.groupBox_3 = QtGui.QGroupBox(self.groupBox)
        self.groupBox_3.setGeometry(QtCore.QRect(440, 60, 261, 381))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.groupBox_3.setFont(font)
        self.groupBox_3.setObjectName(_fromUtf8("groupBox_3"))
        self.textEdit = QtGui.QTextEdit(self.groupBox_3)
        self.textEdit.setGeometry(QtCore.QRect(20, 30, 221, 301))
        self.textEdit.setObjectName(_fromUtf8("textEdit"))
        self.pushButton_2 = QtGui.QPushButton(self.groupBox)
        self.pushButton_2.setGeometry(QtCore.QRect(370, 460, 101, 31))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_2.setFont(font)
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.pushButton.clicked.connect(self.pic_show_ocr)
        self.pushButton_2.clicked.connect(self.close)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.label.setText(_translate("MainWindow", "文字识别软件", None))
        self.groupBox_2.setTitle(_translate("MainWindow", "图像显示区域", None))
        self.pushButton.setText(_translate("MainWindow", "图像显示并识别", None))
        self.groupBox_3.setTitle(_translate("MainWindow", "结果显示", None))
        self.pushButton_2.setText(_translate("MainWindow", "关闭", None))

    def pic_show_ocr(self):
        img_path = os.getcwd() + '/pic/test.jpeg'
        ocr_out_path = os.getcwd() + '/result/test_out.jpg'
        pixmap = QtGui.QPixmap(img_path)  # 指定图片路径
        self.label_2.setPixmap(pixmap)
        self.label_2.setScaledContents(True)
        ocr = PaddleOCR(use_angle_cls=True,  # 是否加载分类模型
                        use_gpu=False)  # 是否使用gpu
        result = ocr.ocr(img_path, cls=True)
        image = Image.open(img_path).convert('RGB')
        txts = [line[1][0] for line in result[0]]
        boxes = [line[0] for line in result[0]]
        scores = [line[1][1] for line in result[0]]
        im_show = draw_ocr(image, boxes, txts, scores, font_path='./fonts/simfang.ttf')
        im_show = Image.fromarray(im_show)
        im_show.save(ocr_out_path)

        data = ''
        for txt in txts:
            data += txt + '\n'
        self.textEdit.setText(data)

    def close(self):
        sys.exit()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.setWindowTitle("文字识别软件")
    MainWindow.show()
    app.exec_()

 5. 界面效果

    以上篇文章测试图片为例,界面效果如下        

6. Pyinstaller 打包

打开命令窗口,cmd该界面代码路径下,执行:

pyinstaller -F -w ocr_recongition_Ui.py

出现上述提示,打包成功,在路径下会生成.exe文件,该文件在没有python环境下可成功执行;若执行出现以下错误:

在ocr_recongition_Ui.spec中添加paddleocr及paddle相关库的路径,如下图所示:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值