feat: Refactor coverage management and enhance logging functionality
- Removed the `.coverage` file from the repository to streamline coverage management. - Updated `.gitignore` to exclude coverage-related files and directories, ensuring a cleaner project structure. - Introduced new scripts for checking GPU availability and detecting GUI parameter conflicts, enhancing the overall functionality of the application. - Improved logging mechanisms across various modules to support better debugging and monitoring. - Added comprehensive documentation for new features and updated existing markdown files for clarity.
This commit is contained in:
1 parent
539e580147
commit
e68f1dd3de
70 files changed
+62590
-10791
No files matched your search
@@ -208,6 +208,10 @@ class underlying_operations:
|
||||
# 支持延迟初始化(测试流程/只做点击时不需要 OCR)。
|
||||
self.ocr: Any = None
|
||||
self.ocr_type = None # 'easyocr' 或 'paddleocr'
|
||||
|
||||
# 检测GPU是否可用(自动检测)
|
||||
self._gpu_available = self._detect_gpu_available()
|
||||
|
||||
if not str(os.environ.get("SKIP_OCR_INIT", "")).strip():
|
||||
# 优先使用EasyOCR(更快),如果失败则回退到PaddleOCR
|
||||
# 首先尝试EasyOCR
|
||||
@@ -220,11 +224,13 @@ class underlying_operations:
|
||||
import easyocr # type: ignore[import-untyped]
|
||||
|
||||
# 只加载中文和英文,加快初始化速度
|
||||
# 自动检测GPU可用性,如果可用则启用GPU加速
|
||||
self.ocr = easyocr.Reader(
|
||||
["ch_sim", "en"], gpu=False, verbose=False
|
||||
["ch_sim", "en"], gpu=self._gpu_available, verbose=False
|
||||
)
|
||||
self.ocr_type = "easyocr"
|
||||
print("[OCR] 使用 EasyOCR(快速模式)")
|
||||
gpu_status = "GPU加速" if self._gpu_available else "CPU模式"
|
||||
print(f"[OCR] 使用 EasyOCR({gpu_status})")
|
||||
except Exception:
|
||||
# EasyOCR初始化失败,回退到PaddleOCR
|
||||
try:
|
||||
@@ -242,15 +248,27 @@ class underlying_operations:
|
||||
), contextlib.redirect_stderr(devnull):
|
||||
from paddleocr import PaddleOCR
|
||||
|
||||
# PaddleOCR的GPU支持需要paddlepaddle-gpu版本
|
||||
use_gpu = False
|
||||
try:
|
||||
import paddle
|
||||
if paddle.device.is_compiled_with_cuda() and paddle.device.get_device():
|
||||
use_gpu = self._gpu_available
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
self.ocr = PaddleOCR(
|
||||
lang="ch",
|
||||
ocr_version="PP-OCRv4",
|
||||
use_angle_cls=False,
|
||||
use_gpu=use_gpu,
|
||||
use_doc_orientation_classify=False,
|
||||
use_doc_unwarping=False,
|
||||
use_textline_orientation=False,
|
||||
)
|
||||
self.ocr_type = "paddleocr"
|
||||
print("[OCR] EasyOCR不可用,使用 PaddleOCR(备选方案)")
|
||||
gpu_status = "GPU加速" if use_gpu else "CPU模式"
|
||||
print(f"[OCR] EasyOCR不可用,使用 PaddleOCR({gpu_status})")
|
||||
print(
|
||||
f"[OCR] 提示:安装EasyOCR可提升速度,运行: pip install easyocr"
|
||||
)
|
||||
@@ -291,9 +309,45 @@ class underlying_operations:
|
||||
adb = adb[1:-1]
|
||||
return adb
|
||||
|
||||
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):
|
||||
if self.ocr is not None:
|
||||
return
|
||||
|
||||
# 重新检测GPU(延迟初始化时)
|
||||
self._gpu_available = self._detect_gpu_available()
|
||||
|
||||
# 优先尝试EasyOCR
|
||||
devnull = None
|
||||
try:
|
||||
@@ -303,9 +357,10 @@ class underlying_operations:
|
||||
):
|
||||
import easyocr # type: ignore[import-untyped]
|
||||
|
||||
self.ocr = easyocr.Reader(["ch_sim", "en"], gpu=False, verbose=False)
|
||||
self.ocr = easyocr.Reader(["ch_sim", "en"], gpu=self._gpu_available, verbose=False)
|
||||
self.ocr_type = "easyocr"
|
||||
print("[OCR] 延迟初始化:使用 EasyOCR(快速模式)")
|
||||
gpu_status = "GPU加速" if self._gpu_available else "CPU模式"
|
||||
print(f"[OCR] 延迟初始化:使用 EasyOCR({gpu_status})")
|
||||
except Exception:
|
||||
# EasyOCR失败,回退到PaddleOCR
|
||||
try:
|
||||
@@ -323,15 +378,27 @@ class underlying_operations:
|
||||
):
|
||||
from paddleocr import PaddleOCR
|
||||
|
||||
# PaddleOCR的GPU支持需要paddlepaddle-gpu版本
|
||||
use_gpu = False
|
||||
try:
|
||||
import paddle
|
||||
if paddle.device.is_compiled_with_cuda() and paddle.device.get_device():
|
||||
use_gpu = self._gpu_available
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
self.ocr = PaddleOCR(
|
||||
lang="ch",
|
||||
ocr_version="PP-OCRv4",
|
||||
use_angle_cls=False,
|
||||
use_gpu=use_gpu,
|
||||
use_doc_orientation_classify=False,
|
||||
use_doc_unwarping=False,
|
||||
use_textline_orientation=False,
|
||||
)
|
||||
self.ocr_type = "paddleocr"
|
||||
print("[OCR] 延迟初始化:使用 PaddleOCR(备选方案)")
|
||||
gpu_status = "GPU加速" if use_gpu else "CPU模式"
|
||||
print(f"[OCR] 延迟初始化:使用 PaddleOCR({gpu_status})")
|
||||
except Exception:
|
||||
try:
|
||||
if devnull is not None:
|
||||
|
||||
Reference in new issue
Block a user