OCR Check Loading png/jpg

.

import sys

from PyQt5.QtWidgets import QApplication, QMainWindow, QDockWidget, QTextEdit, QAction, QFileDialog, QMessageBox, QVBoxLayout, QWidget, QPushButton, QLabel, QHBoxLayout

from PyQt5.QtCore import Qt, QRect, QPoint, pyqtSignal

from PyQt5.QtGui import QPixmap, QPainter, QColor, QPen, QCursor

from PIL import Image

class NotePad(QMainWindow):

def init(self):

super().init()

self.image = None

self.pixmap = None

self.start_point = None

self.end_point = None

self.rect = None

self.scale_factor = 1.0

self.crosshair_enabled = False

self.last_cursor_pos = None

self.dragging = False

self.drag_offset = QPoint()

self.selecting = False

初始化日志标签

self.log_label = QLabel(self)

self.log_label.setFixedSize(300, 300) # 调整日志区域大小

self.log_label.setStyleSheet("border: 1px solid black; background-color: white;")

self.log_label.setAlignment(Qt.AlignTop)

self.log_label.setVisible(True)

self.initUI()

def initUI(self):

self.setWindowTitle("NotePad")

self.setGeometry(100, 100, 1000, 700)

self.textEdit = QTextEdit()

self.setCentralWidget(self.textEdit)

dock = QDockWidget("File Actions", self)

dock.setAllowedAreas(Qt.LeftDockWidgetArea)

#openButton = QPushButton("Open File")

#openButton.clicked.connect(self.openFile)

#saveButton = QPushButton("Save File")

#saveButton.clicked.connect(self.saveFile)

ocrButton = QPushButton("OCR Check")

ocrButton.clicked.connect(self.ocrCheck)

layout = QVBoxLayout()

#layout.addWidget(openButton)

#layout.addWidget(saveButton)

layout.addWidget(ocrButton)

widget = QWidget()

widget.setLayout(layout)

dock.setWidget(widget)

self.addDockWidget(Qt.LeftDockWidgetArea, dock)

添加日志显示区域

logDock = QDockWidget("Log", self)

logDock.setAllowedAreas(Qt.RightDockWidgetArea)

logDock.setWidget(self.log_label) # 确保此处使用了正确的属性

self.addDockWidget(Qt.RightDockWidgetArea, logDock)

图片显示区域

self.image_label = QLabel(self)

self.image_label.setGeometry(100, 100, 800, 600)

self.image_label.setAlignment(Qt.AlignCenter)

self.image_label.mousePressEvent = self.mouse_press

self.image_label.mouseMoveEvent = self.mouse_move

self.image_label.mouseReleaseEvent = self.mouse_release

self.image_label.setMouseTracking(True) # 允许鼠标跟踪

按钮布局

button_layout = QHBoxLayout()

fit_button = QPushButton("Fit to Window")

fit_button.clicked.connect(self.fit_to_window)

zoom_in_button = QPushButton("Zoom In")

zoom_in_button.clicked.connect(self.zoom_in)

zoom_out_button = QPushButton("Zoom Out")

zoom_out_button.clicked.connect(self.zoom_out)

crosshair_button = QPushButton("Crosshair")

crosshair_button.clicked.connect(self.toggle_crosshair)

drag_button = QPushButton("Drag Image")

drag_button.clicked.connect(self.enable_dragging)

select_button = QPushButton("Select Area")

select_button.clicked.connect(self.enable_selecting)

button_layout.addWidget(fit_button)

button_layout.addWidget(zoom_in_button)

button_layout.addWidget(zoom_out_button)

button_layout.addWidget(crosshair_button)

button_layout.addWidget(drag_button)

button_layout.addWidget(select_button)

将按钮布局添加到主窗口

main_layout = QVBoxLayout()

main_layout.addLayout(button_layout)

main_layout.addWidget(self.image_label)

central_widget = QWidget()

central_widget.setLayout(main_layout)

self.setCentralWidget(central_widget)

self.createActions()

self.createMenus()

self.show()

def createActions(self):

self.openAction = QAction("Open File", self)

self.openAction.triggered.connect(self.openFile)

self.saveAction = QAction("Save File", self)

self.saveAction.triggered.connect(self.saveFile)

def createMenus(self):

self.fileMenu = self.menuBar().addMenu("File")

self.fileMenu.addAction(self.openAction)

self.fileMenu.addAction(self.saveAction)

def openFile(self):

fileName, _ = QFileDialog.getOpenFileName(self, "Open File", "", "Text Files (*.txt);;All Files (*)")

if fileName:

try:

with open(fileName, 'r', encoding='utf-8') as file:

content = file.read()

self.textEdit.setPlainText(content)

except Exception as e:

QMessageBox.critical(self, "Error", str(e))

def saveFile(self):

fileName, _ = QFileDialog.getSaveFileName(self, "Save File", "", "Text Files (*.txt);;All Files (*)")

if fileName:

try:

with open(fileName, 'w', encoding='utf-8') as file:

content = self.textEdit.toPlainText()

file.write(content)

QMessageBox.information(self, "Success", "File saved successfully.")

except Exception as e:

QMessageBox.critical(self, "Error", str(e))

def ocrCheck(self):

fileName, _ = QFileDialog.getOpenFileName(self, "Open Image", "", "Image Files (*.jpg *.jpeg *.png)")

if fileName:

try:

self.image = Image.open(fileName)

self.pixmap = QPixmap(fileName)

self.fit_to_window() # 初始加载时就适应窗口

self.scale_factor = 1.0

except Exception as e:

QMessageBox.critical(self, "Error", str(e))

def mouse_press(self, event):

if self.dragging and event.button() == Qt.LeftButton:

self.drag_offset = event.pos() - self.image_label.pos()

elif self.selecting and event.button() == Qt.LeftButton:

self.start_point = event.pos()

self.end_point = event.pos()

self.update()

def mouse_move(self, event):

if self.dragging and event.buttons() & Qt.LeftButton:

self.image_label.move(event.pos() - self.drag_offset)

elif self.selecting and self.start_point and event.buttons() & Qt.LeftButton:

self.end_point = event.pos()

if self.crosshair_enabled:

self.last_cursor_pos = event.pos()

self.update()

def mouse_release(self, event):

if self.selecting and self.start_point and self.end_point:

rect = QRect(self.start_point, self.end_point).normalized()

self.rect = (rect.x(), rect.y(), rect.width(), rect.height())

self.check_red_color()

self.update()

def check_red_color(self):

if self.image and self.rect:

cropped_image = self.image.crop(self.rect)

rgb_image = cropped_image.convert('RGB')

pixels = list(rgb_image.getdata())

for pixel in pixels:

r, g, b = pixel

if r > 200 and g < 100 and b < 100: # 红色的标准可以调整

self.show_check_ng()

return

self.show_check_ok()

def show_check_ng(self):

self.log_label.setText("Check NG\n" + self.log_label.text())

QMessageBox.warning(self, "Result", "Check NG")

def show_check_ok(self):

self.log_label.setText("Check OK\n" + self.log_label.text())

QMessageBox.information(self, "Result", "Check OK")

def paintEvent(self, event):

painter = QPainter(self.image_label)

if self.selecting and self.start_point and self.end_point:

pen = QPen(Qt.red, 2, Qt.SolidLine)

painter.setPen(pen)

painter.drawRect(QRect(self.start_point, self.end_point).normalized())

if self.crosshair_enabled and self.last_cursor_pos:

pen = QPen(Qt.red, 1, Qt.SolidLine)

painter.setPen(pen)

x, y = self.last_cursor_pos.x(), self.last_cursor_pos.y()

painter.drawLine(x, 0, x, self.image_label.height())

painter.drawLine(0, y, self.image_label.width(), y)

def fit_to_window(self):

if self.pixmap:

self.scale_factor = 1.0

self.image_label.setPixmap(self.pixmap.scaled(self.image_label.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))

self.image_label.adjustSize()

def zoom_in(self):

if self.pixmap:

self.scale_factor *= 1.25

scaled_pixmap = self.pixmap.scaled(self.pixmap.size() * self.scale_factor, Qt.KeepAspectRatio, Qt.SmoothTransformation)

self.image_label.setPixmap(scaled_pixmap)

self.image_label.adjustSize() # 适应新的尺寸

def zoom_out(self):

if self.pixmap:

self.scale_factor /= 1.25

scaled_pixmap = self.pixmap.scaled(self.pixmap.size() * self.scale_factor, Qt.KeepAspectRatio, Qt.SmoothTransformation)

self.image_label.setPixmap(scaled_pixmap)

self.image_label.adjustSize() # 适应新的尺寸

def toggle_crosshair(self):

self.crosshair_enabled = not self.crosshair_enabled

if self.crosshair_enabled:

self.setCursor(Qt.CrossCursor)

else:

self.setCursor(Qt.ArrowCursor)

def enable_dragging(self):

self.dragging = True

self.selecting = False

self.setCursor(Qt.OpenHandCursor) # 改变光标为拖动状态

def enable_selecting(self):

self.dragging = False

self.selecting = True

self.setCursor(Qt.ArrowCursor) # 改变光标为默认状态

if name == 'main':

app = QApplication(sys.argv)

notepad = NotePad()

sys.exit(app.exec_())

相关推荐
Chunyyyen12 小时前
【第二十六周】OCR学习01
学习·计算机视觉·ocr
恶猫1 天前
STranslate 翻译 工具 v2.0.0 绿色便携版 翻译、OCR工具
ocr·文字识别·自动翻译·翻译·划词翻译·截图翻译
垦***耪1 天前
台达 DVP ES2 与 3 台英威腾 GD 通讯程序(TDES - 12)开发实战
ocr
MonkeyKing_sunyuhua2 天前
使用ARQ做PDF OCR和 图片OCR的任务的方案
pdf·ocr
kevin 12 天前
合同盖章前,如何比对差异,确保纸质版与电子版100%一致?
人工智能·自动化·ocr
AI人工智能+3 天前
授权委托书识别技术:利用深度学习和NLP实现纸质文档的智能解析
ocr·文档抽取·授权委托书识别
大强同学3 天前
ShareX - 错误:英语 language is not available in this system for OCR.
ocr
不惑_3 天前
在 Rokid 眼镜上实现工业巡检与 OCR,识别、理解与指导的现场智能
ocr
翔云 OCR API3 天前
赋能文档的数字化智能处理:通用文字/文档/合同识别接口
开发语言·人工智能·python·计算机视觉·ocr
番石榴AI4 天前
java版的ocr推荐引擎——JiaJiaOCR 2.0重磅升级!纯Java CPU推理,新增手写OCR与表格识别
java·python·ocr