feat: Add logging and error handling enhancements
- Introduced new logging modules for action tracking, error logging, and performance tracking to improve observability. - Added functions for retrieving debug and error log paths in `path_config.py`. - Updated various components to utilize the new logging system, enhancing error handling and performance diagnostics. - Implemented a GUI component for viewing error logs, improving user experience in monitoring application issues. - Cleaned up unnecessary blank lines and improved code readability across multiple files.
This commit is contained in:
1 parent
bdf9312bc9
commit
efe6e2252f
48 files changed
+2165
-320
No files matched your search
@@ -26,8 +26,8 @@ class OCRProcessor:
|
||||
self.ocr: Any = None
|
||||
self.ocr_type = None # 'easyocr' 或 'paddleocr'
|
||||
|
||||
# 检测GPU是否可用(自动检测)
|
||||
self._gpu_available = self._detect_gpu_available()
|
||||
# 默认使用CPU模式(删除GPU检测)
|
||||
self._gpu_available = False
|
||||
|
||||
if not str(os.environ.get("SKIP_OCR_INIT", "")).strip():
|
||||
self._init_ocr()
|
||||
@@ -36,32 +36,30 @@ class OCRProcessor:
|
||||
"""初始化OCR引擎"""
|
||||
# 优先使用EasyOCR(更快),如果失败则回退到PaddleOCR
|
||||
# 首先尝试EasyOCR
|
||||
devnull = None
|
||||
try:
|
||||
devnull = open(os.devnull, "w")
|
||||
with contextlib.redirect_stdout(devnull), contextlib.redirect_stderr(
|
||||
devnull
|
||||
):
|
||||
import easyocr # type: ignore[import-untyped] # pyright: ignore[reportMissingImports]
|
||||
# 在Windows上,避免使用stderr重定向,可能会导致"lost sys.stderr"问题
|
||||
import easyocr # type: ignore[import-untyped] # pyright: ignore[reportMissingImports]
|
||||
|
||||
# 只加载中文和英文,加快初始化速度
|
||||
# 自动检测GPU可用性,如果可用则启用GPU加速
|
||||
# 只加载中文和英文,加快初始化速度
|
||||
# 自动检测GPU可用性,如果可用则启用GPU加速
|
||||
# 临时重定向输出以减少初始化时的噪音
|
||||
import io
|
||||
from contextlib import redirect_stdout, redirect_stderr
|
||||
|
||||
fake_stdout = io.StringIO()
|
||||
fake_stderr = io.StringIO()
|
||||
|
||||
with redirect_stdout(fake_stdout), redirect_stderr(fake_stderr):
|
||||
self.ocr = easyocr.Reader(
|
||||
["ch_sim", "en"], gpu=self._gpu_available, verbose=False
|
||||
["ch_sim", "en"], gpu=False, verbose=False
|
||||
)
|
||||
self.ocr_type = "easyocr"
|
||||
gpu_status = "GPU加速" if self._gpu_available else "CPU模式"
|
||||
print(f"[OCR] 使用 EasyOCR({gpu_status})")
|
||||
except Exception:
|
||||
|
||||
self.ocr_type = "easyocr"
|
||||
print(f"[OCR] 使用 EasyOCR(CPU模式)")
|
||||
except Exception as e:
|
||||
# EasyOCR初始化失败,回退到PaddleOCR
|
||||
try:
|
||||
if devnull is not None:
|
||||
try:
|
||||
devnull.close()
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
print(f"[OCR] EasyOCR初始化失败: {e},回退到PaddleOCR")
|
||||
devnull = None
|
||||
try:
|
||||
devnull = open(os.devnull, "w")
|
||||
with contextlib.redirect_stdout(
|
||||
@@ -78,7 +76,7 @@ class OCRProcessor:
|
||||
paddle.device.is_compiled_with_cuda()
|
||||
and paddle.device.get_device()
|
||||
):
|
||||
use_gpu = self._gpu_available
|
||||
use_gpu = False
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@@ -99,67 +97,22 @@ class OCRProcessor:
|
||||
)
|
||||
except Exception:
|
||||
# 若静默初始化失败,保留 None 并允许后续按需初始化,避免阻塞启动。
|
||||
try:
|
||||
if devnull is not None:
|
||||
try:
|
||||
devnull.close()
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
self.ocr = None
|
||||
self.ocr_type = None
|
||||
finally:
|
||||
try:
|
||||
if devnull is not None:
|
||||
devnull.close()
|
||||
except Exception:
|
||||
pass
|
||||
finally:
|
||||
try:
|
||||
if devnull is not None:
|
||||
devnull.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _detect_gpu_available(self) -> bool:
|
||||
"""检测GPU是否可用
|
||||
|
||||
默认返回False(CPU模式),除非通过环境变量明确启用GPU
|
||||
|
||||
返回:
|
||||
True: GPU可用且已启用
|
||||
False: 使用CPU模式(默认)
|
||||
"""
|
||||
# 默认使用CPU模式,除非环境变量明确要求使用GPU
|
||||
# 检查环境变量是否强制启用GPU
|
||||
if str(os.environ.get("FORCE_GPU_OCR", "")).strip().lower() in (
|
||||
"1",
|
||||
"true",
|
||||
"yes",
|
||||
):
|
||||
# 强制启用时,验证GPU是否真的可用
|
||||
try:
|
||||
import torch # type: ignore[import-untyped]
|
||||
|
||||
if torch.cuda.is_available():
|
||||
gpu_name = torch.cuda.get_device_name(0)
|
||||
print(f"[OCR] 环境变量强制启用GPU: {gpu_name},将使用GPU加速")
|
||||
return True
|
||||
else:
|
||||
print("[OCR] 环境变量要求启用GPU,但GPU不可用,使用CPU模式")
|
||||
return False
|
||||
except ImportError:
|
||||
print("[OCR] 环境变量要求启用GPU,但PyTorch未安装,使用CPU模式")
|
||||
return False
|
||||
except Exception as e:
|
||||
print(f"[OCR] GPU检测失败: {e},使用CPU模式")
|
||||
return False
|
||||
|
||||
# 默认使用CPU模式
|
||||
return False
|
||||
|
||||
def _ensure_ocr(self):
|
||||
"""确保OCR引擎已初始化"""
|
||||
if self.ocr is not None:
|
||||
return
|
||||
|
||||
# 重新检测GPU(延迟初始化时)
|
||||
self._gpu_available = self._detect_gpu_available()
|
||||
# 默认使用CPU模式(延迟初始化时)
|
||||
|
||||
# 优先尝试EasyOCR
|
||||
devnull = None
|
||||
@@ -171,10 +124,10 @@ class OCRProcessor:
|
||||
import easyocr # type: ignore[import-untyped]
|
||||
|
||||
self.ocr = easyocr.Reader(
|
||||
["ch_sim", "en"], gpu=self._gpu_available, verbose=False
|
||||
["ch_sim", "en"], gpu=False, verbose=False
|
||||
)
|
||||
self.ocr_type = "easyocr"
|
||||
gpu_status = "GPU加速" if self._gpu_available else "CPU模式"
|
||||
gpu_status = "CPU模式"
|
||||
print(f"[OCR] 延迟初始化:使用 EasyOCR({gpu_status})")
|
||||
except Exception:
|
||||
# EasyOCR失败,回退到PaddleOCR
|
||||
@@ -202,7 +155,7 @@ class OCRProcessor:
|
||||
paddle.device.is_compiled_with_cuda()
|
||||
and paddle.device.get_device()
|
||||
):
|
||||
use_gpu = self._gpu_available
|
||||
use_gpu = False
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@@ -234,9 +187,6 @@ class OCRProcessor:
|
||||
_ocr_processor = OCRProcessor()
|
||||
|
||||
|
||||
def _detect_gpu_available() -> bool:
|
||||
"""检测GPU是否可用(向后兼容接口)"""
|
||||
return _ocr_processor._detect_gpu_available()
|
||||
|
||||
|
||||
def _ensure_ocr():
|
||||
@@ -277,6 +227,5 @@ __all__ = [
|
||||
"analyse_pic_digits",
|
||||
"extract_ocr_content",
|
||||
"_ensure_ocr",
|
||||
"_detect_gpu_available",
|
||||
]
|
||||
|
||||
Reference in new issue
Block a user