python基于PaddleOCR的文本识别GUI程序
1.PaddleOCR 的组成
PaddleOCR 包含多个模块,主要分为两部分:
文本检测:检测图像中的文本区域。
文本识别:识别文本区域中的具体文字。
此外,PaddleOCR 还支持多语言识别、表格识别、方向分类等功能。
2. 模型规模
PaddleOCR 提供了多种预训练模型,模型规模从小到大都有:
2.1 轻量级模型:如 PP-OCRv3,模型较小,适合移动端或嵌入式设备。
2.2 高精度模型:如 PP-OCRv3-server,模型较大,适合对精度要求较高的场景。
这些模型的规模远小于典型的“大模型”(如 GPT、BERT 等),但它们针对 OCR 任务进行了优化,能够在较小的模型规模下实现较高的精度。
3. 本程序GUI界面

功能 1:
可以整个图片进行文字识别。

功能 2 :
对图片所选区域进行识别文字

4. 核心代码
(https://download.youkuaiyun.com/download/java_marvel/90291417)[数据包]
class ImageRecognition(QtWidgets.QMainWindow):
def __init__(self):
super(ImageRecognition, self).__init__()
# imagePath
self.filePath = ''
self.all_screens = []
self.shot_screens = []
self.resize(1200, 800)
self.setWindowTitle("A+ KPD Image-recognition Tool")
self.setWindowIcon(QtGui.QIcon("🍳"))
# central widget
centralwidget = QtWidgets.QWidget(self)
self.setCentralWidget(centralwidget)
# central widget 里面的 主 layout
mainLayout = QtWidgets.QVBoxLayout(centralwidget)
# UI Up page
topLayout = QtWidgets.QHBoxLayout()
self.label_ori_image = QtWidgets.QLabel(self)
self.label_treated_image = QtWidgets.QLabel(self)
self.label_ori_image.setMinimumSize(520, 400)
self.label_treated_image.setMinimumSize(520, 400)
self.label_ori_image.setStyleSheet("border: 1px solid #D7E2F9;")
self.label_treated_image.setStyleSheet("border: 1px solid #D7E2F9;")
topLayout.addWidget(self.label_ori_image)
topLayout.addWidget(self.label_treated_image)
mainLayout.addLayout(topLayout)
# UI Down Page
groupBox = QtWidgets.QGroupBox(self)
bottomLayout = QtWidgets.QHBoxLayout(groupBox)
self.textLog = QtWidgets.QTextBrowser()
self.textLog.setStyleSheet("border: 1px solid #D7E2F9;")
bottomLayout.addWidget(self.textLog)
mainLayout.addWidget(groupBox)
# right button
btnLayout = QtWidgets.QVBoxLayout()
self.imageBtn = QtWidgets.QPushButton("🎞 图片")
self.startBtn = QtWidgets.QPushButton("◀ 开始")
btnLayout.addWidget(self.imageBtn)
btnLayout.addWidget(self.startBtn)
bottomLayout.addLayout(btnLayout)
self.imageBtn.clicked.connect(self.openImage)
self.startBtn.clicked.connect(self.recognition)
# 获取剪贴板
self.clipboard = QApplication.clipboard()
def recognition_txt(self, data):
import DafaIt
# 文字识别
# 识别范围 (起始x, 起始y, 识别宽, 识别高)
a1 = DafaIt.zi((data[0], data[1], data[2], data[3]))
print(self.all_screens)
print(a1)
self.textLog.append(a1)
def keyPressEvent(self, event):
""" 处理键盘事件,当按下 Ctrl+V 时粘贴图片 """
if event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_V:
self.paste_image_from_clipboard()
def paste_image_from_clipboard(self):
""" 从剪贴板获取图片并显示到 QLabel 上 """
mime = self.clipboard.mimeData()
# 确保剪贴板内容是图片
if mime.hasImage():
image = self.clipboard.image()
pixmap = QPixmap.fromImage(image)
self.label_treated_image.setPixmap(pixmap.scaled(self.label_treated_image.size(), Qt.KeepAspectRatio))
else:
self.label_treated_image.setText("No image found in clipboard.")
def openImage(self):
# 打开文件对话框让用户选择图片
filePath, _ = QtWidgets.QFileDialog.getOpenFileName(self, "选择图片", "", "图片文件 (*.png *.jpg *.bmp *.jpeg)")
if filePath:
# 加载并显示图片
self.pixmap = QPixmap(filePath)
self.label_ori_image.setPixmap(self.pixmap.scaled(self.label_ori_image.size(), QtCore.Qt.KeepAspectRatio))
label_pos = self.label_ori_image.mapToGlobal(QtCore.QPoint(0, 0)) # 获取 label 左上角的屏幕坐标
label_x = label_pos.x()
label_y = label_pos.y()
# 获取 QLabel 的大小
label_width = self.label_ori_image.width()
label_height = self.label_ori_image.height()
# 输出坐标和尺寸
# self.textLog.append(f"Label 位置:({label_x}, {label_y})")
# self.textLog.append(f"Label 尺寸:{label_width} x {label_height}")
# 也可以将图片路径输出到日志
self.textLog.append(f"选择的图片路径: {filePath}")
self.filePath = filePath
self.all_screens.append(label_x)
self.all_screens.append(label_y)
self.all_screens.append(label_width)
self.all_screens.append(label_height)
else:
QMessageBox.warning(self, "Notice", "请先选择图片!")
def recognition(self):
if self.filePath != "":
self.textLog.clear()
if self.label_treated_image.pixmap() is not None:
label_tre = self.label_treated_image.mapToGlobal(QtCore.QPoint(0, 0)) # 获取 label 左上角的屏幕坐标
label_x = label_tre.x()
label_y = label_tre.y()
# 获取 QLabel 的大小
label_width = self.label_treated_image.width()
label_height = self.label_treated_image.height()
# 输出坐标和尺寸
self.textLog.append(f"Label 位置:({label_x}, {label_y})")
self.textLog.append(f"Label 尺寸:{label_width} x {label_height}")
self.shot_screens.append(label_x)
self.shot_screens.append(label_y)
self.shot_screens.append(label_width)
self.shot_screens.append(label_height)
# recognition txt
self.recognition_txt(self.shot_screens)
else:
# recognition txt
self.recognition_txt(self.all_screens)
else:
QMessageBox.warning(self, "Notice", "请先选择图片!")
app = QtWidgets.QApplication([])
window = ImageRecognition()
window.show()
app.exec_()
5. 总结
PaddleOCR 提供了多个预训练模型,例如:
PP-OCRv3:轻量级模型,适合移动端和嵌入式设备。
PP-OCRv3-server:高精度模型,适合服务器端使用。
PP-Structure:用于表格识别和文档结构分析。
这些模型的规模通常较小,但通过优化和蒸馏技术,能够在保持较高精度的同时减少计算资源需求。
PaddleOCR 专注于 OCR 任务的工具库。它提供了多种规模的模型,适合从移动端到服务器端的各种应用场景。虽然模型规模较小,但通过针对性的优化,PaddleOCR 在 OCR 任务中表现出色,是文本检测与识别领域的优秀工具。

1358

被折叠的 条评论
为什么被折叠?



