Refactor import statements for douyin_screenshot_manager to use dynamic loading
- Updated multiple files to replace static imports of `douyin_screenshot_manager` with dynamic imports using `importlib`. This change enhances flexibility and prevents import errors when the module is unavailable. - Added fallback mechanisms in `fudai_core.py` to ensure the application continues to function even if certain dependencies are missing, improving robustness. - Adjusted error handling to provide clearer feedback during initialization failures, particularly for OCR-related components.
This commit is contained in:
1 parent
c7b5d6be0f
commit
148cdf56c2
13 files changed
+192
-275
No files matched your search
+34
-18
@@ -233,7 +233,12 @@ class underlying_operations:
|
||||
try:
|
||||
# 在Windows上,避免使用stderr重定向,可能会导致"lost sys.stderr"问题
|
||||
# 改为直接静默导入和初始化
|
||||
import easyocr # type: ignore[import-untyped] # pyright: ignore[reportMissingImports]
|
||||
# 尝试按需导入 easyocr,避免静态检查器在缺少依赖时报错
|
||||
import importlib
|
||||
try:
|
||||
easyocr = importlib.import_module("easyocr")
|
||||
except Exception:
|
||||
easyocr = None
|
||||
|
||||
# 只加载中文和英文,加快初始化速度
|
||||
# 自动检测GPU可用性,如果可用则启用GPU加速
|
||||
@@ -244,14 +249,18 @@ class underlying_operations:
|
||||
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=False, verbose=False
|
||||
)
|
||||
if easyocr is not None:
|
||||
with redirect_stdout(fake_stdout), redirect_stderr(fake_stderr):
|
||||
self.ocr = easyocr.Reader(
|
||||
["ch_sim", "en"], gpu=False, verbose=False
|
||||
)
|
||||
|
||||
self.ocr_type = "easyocr"
|
||||
gpu_status = "CPU模式"
|
||||
print(f"[OCR] 使用 EasyOCR({gpu_status})")
|
||||
self.ocr_type = "easyocr"
|
||||
gpu_status = "CPU模式"
|
||||
print(f"[OCR] 使用 EasyOCR({gpu_status})")
|
||||
else:
|
||||
# 强制抛出异常以触发回退逻辑
|
||||
raise ImportError("easyocr not available")
|
||||
except Exception as e:
|
||||
# EasyOCR初始化失败,回退到PaddleOCR
|
||||
print(f"[OCR] EasyOCR初始化失败: {e},回退到PaddleOCR")
|
||||
@@ -333,7 +342,11 @@ class underlying_operations:
|
||||
# 优先尝试EasyOCR
|
||||
try:
|
||||
# 在Windows上,避免使用stderr重定向,可能会导致"lost sys.stderr"问题
|
||||
import easyocr # type: ignore[import-untyped]
|
||||
import importlib
|
||||
try:
|
||||
easyocr = importlib.import_module("easyocr")
|
||||
except Exception:
|
||||
easyocr = None
|
||||
|
||||
# 临时重定向输出以减少初始化时的噪音
|
||||
import io
|
||||
@@ -342,14 +355,17 @@ class underlying_operations:
|
||||
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=False, verbose=False
|
||||
)
|
||||
if easyocr is not None:
|
||||
with redirect_stdout(fake_stdout), redirect_stderr(fake_stderr):
|
||||
self.ocr = easyocr.Reader(
|
||||
["ch_sim", "en"], gpu=False, verbose=False
|
||||
)
|
||||
|
||||
self.ocr_type = "easyocr"
|
||||
gpu_status = "GPU加速" if False else "CPU模式"
|
||||
print(f"[OCR] 延迟初始化:使用 EasyOCR({gpu_status})")
|
||||
self.ocr_type = "easyocr"
|
||||
gpu_status = "GPU加速" if False else "CPU模式"
|
||||
print(f"[OCR] 延迟初始化:使用 EasyOCR({gpu_status})")
|
||||
else:
|
||||
raise ImportError("easyocr not available")
|
||||
except Exception as e:
|
||||
# EasyOCR失败,回退到PaddleOCR
|
||||
print(f"[OCR] 延迟初始化EasyOCR失败: {e},回退到PaddleOCR")
|
||||
@@ -450,8 +466,8 @@ class underlying_operations:
|
||||
|
||||
# OCR图片文件:按分类组织到子目录
|
||||
try:
|
||||
from douyin.handlers import douyin_screenshot_manager
|
||||
|
||||
import importlib
|
||||
douyin_screenshot_manager = importlib.import_module("douyin.handlers.douyin_screenshot_manager")
|
||||
category = douyin_screenshot_manager.get_category_for_picname(picname)
|
||||
except Exception:
|
||||
category = "others"
|
||||
|
||||
Reference in new issue
Block a user