Skip to content

Commit

Permalink
Merge pull request #6 from formero009/v2.0.0
Browse files Browse the repository at this point in the history
bugfix 模型路径不存在时闪退问题
  • Loading branch information
formero009 authored Jan 14, 2025
2 parents e344de6 + dfb6915 commit 546baea
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
4 changes: 2 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
snipaste:
modelpath: C:/Users/formero/Desktop/projects/SnipasteOCR/models
path: C:/Users/formero/Pictures/snipaste
modelpath:
path:
preview_enabled: true
translation:
from_lang: auto
Expand Down
27 changes: 24 additions & 3 deletions src/core/ocr_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import yaml
import logging
from PyQt6.QtCore import QThread, pyqtSignal
from PyQt6.QtWidgets import QMessageBox
from src.core.folder_monitor import FolderMonitor
from src.utils.logging_config import setup_logging

Expand All @@ -10,6 +11,7 @@

class OCRThread(QThread):
preview_signal = pyqtSignal(str, object)
error_signal = pyqtSignal(str)

def __init__(self):
super().__init__()
Expand All @@ -25,22 +27,41 @@ def __init__(self):
config = yaml.safe_load(f)
self.path = config['snipaste']['path']
self.modelpath = config['snipaste'].get('modelpath', os.path.join(current_path, 'models'))

# Validate model path
if not self.modelpath or not os.path.exists(self.modelpath):
error_msg = "模型路径无效或为空,请在设置中指定正确的模型路径。"
logger.error(error_msg)
self.error_signal.emit(error_msg)
return

except Exception as e:
logger.error(f"Failed to load OCR configuration: {str(e)}")
raise

def run(self):
logger.info("Starting OCR thread")
self.FolderMonitor = FolderMonitor(self.path, self.modelpath)
self.FolderMonitor.result_signal.connect(self.handle_result)
try:
self.FolderMonitor = FolderMonitor(self.path, self.modelpath)
self.FolderMonitor.result_signal.connect(self.handle_result)
except Exception as e:
error_msg = f"启动OCR服务失败: {str(e)}"
logger.error(error_msg)
self.error_signal.emit(error_msg)

def handle_result(self, image_path, result):
if self.running:
logging.info(f"Received result: \n\n{result}\n\n")
self.preview_signal.emit(image_path, result)

def stop(self):
"""停止OCR线程并清理资源"""
logger.info("Stopping OCR thread")
self.running = False
if hasattr(self, 'FolderMonitor'):
self.FolderMonitor.stop()
try:
self.FolderMonitor.result_signal.disconnect()
except Exception:
pass
self.FolderMonitor.stop()
delattr(self, 'FolderMonitor')
35 changes: 33 additions & 2 deletions src/ui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(self):

self.ocrThread = OCRThread()
self.ocrThread.preview_signal.connect(self.show_preview)
self.ocrThread.error_signal.connect(self.show_ocr_error)
self.preview_window = None
self.preview_enabled = True

Expand Down Expand Up @@ -134,6 +135,9 @@ def setupTrayIcon(self):
self.trayIcon.show()

def initData(self):
self.ocrThread = OCRThread()
self.ocrThread.preview_signal.connect(self.show_preview)
self.ocrThread.error_signal.connect(self.show_ocr_error)
self.ocrThread.start()

def initStyle(self):
Expand Down Expand Up @@ -532,7 +536,27 @@ def saveConfig(self):
with open(config_path, 'w', encoding='utf-8') as f:
yaml.dump(existing_config, f, allow_unicode=True)

QMessageBox.information(self, '成功', '设置已保存')
# 重新启动OCR线程
if self.ocrThread is not None:
self.ocrThread.stop()
self.ocrThread.quit()
if not self.ocrThread.wait(1000):
logger.warning("OCR thread did not exit in time, forcing termination")
self.ocrThread.terminate()
self.ocrThread.wait()

# 创建新的OCR线程
self.ocrThread = OCRThread()
self.ocrThread.preview_signal.connect(self.show_preview)
self.ocrThread.error_signal.connect(self.show_ocr_error)
self.ocrThread.start()

QMessageBox.information(self, '成功', '设置已保存并重新加载OCR服务')

# 重新启用预览按钮(如果之前被禁用)
self.preview_button.setEnabled(True)
self.preview_button.setToolTip('')

except Exception as e:
logger.error(f"Failed to save configuration: {str(e)}")
QMessageBox.warning(self, '错误', f'保存配置文件失败: {str(e)}')
Expand Down Expand Up @@ -670,4 +694,11 @@ def update_autostart_button(self):
self.autostart_button.style().polish(self.autostart_button)

except Exception as e:
logger.error(f"Failed to update autostart button: {str(e)}")
logger.error(f"Failed to update autostart button: {str(e)}")

def show_ocr_error(self, error_msg):
"""显示OCR错误消息"""
QMessageBox.warning(self, '错误', error_msg)
# 禁用预览按钮,因为OCR服务未正常启动
self.preview_button.setEnabled(False)
self.preview_button.setToolTip('OCR服务未正常启动')

0 comments on commit 546baea

Please sign in to comment.