- Removed trailing whitespace and ensured files end with a single newline in cleanup_unused_files.py. - Added fix_trailing_whitespace.py to batch remove trailing whitespace from Python files. - Updated install_dependencies.py to improve dependency installation prompts. - Cleaned up pick_coords.py by removing unnecessary whitespace and improving code readability. - Enhanced region_selector.py by disabling specific pylint checks for better compatibility with third-party libraries. - Improved WPS流程图生成工具.py by cleaning up whitespace and enhancing readability. - Updated __init__.py in typings to remove unnecessary lines. - Modified app.js to adjust default configuration values and clean up unused code.
209 lines
6.7 KiB
Python
209 lines
6.7 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
检查GPU是否可用的脚本
|
||
"""
|
||
|
||
import sys
|
||
|
||
# 修复Windows控制台编码问题
|
||
if sys.platform == "win32":
|
||
try:
|
||
reconfigure_func = getattr(sys.stdout, 'reconfigure', None)
|
||
if reconfigure_func:
|
||
reconfigure_func(encoding="utf-8")
|
||
except Exception:
|
||
pass
|
||
|
||
|
||
def check_torch_cuda():
|
||
"""检查PyTorch是否支持CUDA"""
|
||
try:
|
||
try:
|
||
import torch # type: ignore # pylint: disable=import-error
|
||
except ImportError:
|
||
print("[GPU检测] PyTorch未安装")
|
||
return False
|
||
|
||
if torch.cuda.is_available():
|
||
print(f"[GPU检测] PyTorch CUDA可用")
|
||
print(f" CUDA版本: {torch.version.cuda}")
|
||
print(f" GPU数量: {torch.cuda.device_count()}")
|
||
for i in range(torch.cuda.device_count()):
|
||
print(f" GPU {i}: {torch.cuda.get_device_name(i)}")
|
||
return True
|
||
else:
|
||
print("[GPU检测] PyTorch CUDA不可用")
|
||
return False
|
||
except Exception as e:
|
||
print(f"[GPU检测] PyTorch CUDA检测失败: {e}")
|
||
return False
|
||
|
||
|
||
def check_easyocr_gpu():
|
||
"""检查EasyOCR是否可以使用GPU"""
|
||
try:
|
||
try:
|
||
import easyocr # type: ignore # pylint: disable=import-error
|
||
except ImportError:
|
||
print("[GPU检测] EasyOCR未安装")
|
||
return False
|
||
|
||
print("[GPU检测] EasyOCR已安装")
|
||
# EasyOCR使用PyTorch,所以需要PyTorch支持CUDA
|
||
try:
|
||
import torch # type: ignore
|
||
|
||
if torch.cuda.is_available():
|
||
print("[GPU检测] EasyOCR可以使用GPU(通过PyTorch CUDA)")
|
||
return True
|
||
else:
|
||
print("[GPU检测] EasyOCR无法使用GPU(PyTorch CUDA不可用)")
|
||
return False
|
||
except ImportError:
|
||
print("[GPU检测] EasyOCR需要PyTorch才能使用GPU")
|
||
return False
|
||
except ImportError:
|
||
print("[GPU检测] EasyOCR未安装")
|
||
return False
|
||
except Exception as e:
|
||
print(f"[GPU检测] EasyOCR GPU检测失败: {e}")
|
||
return False
|
||
|
||
|
||
def check_paddleocr_gpu():
|
||
"""检查PaddleOCR是否可以使用GPU"""
|
||
try:
|
||
print("[GPU检测] PaddleOCR已安装")
|
||
# PaddleOCR需要paddlepaddle-gpu版本
|
||
try:
|
||
try:
|
||
import paddle # type: ignore # pylint: disable=import-error
|
||
except ImportError:
|
||
print("[GPU检测] PaddleOCR未安装(paddle不可用)")
|
||
return False
|
||
|
||
if paddle.device.is_compiled_with_cuda():
|
||
print("[GPU检测] PaddleOCR可以使用GPU(paddlepaddle-gpu已安装)")
|
||
if paddle.device.get_device():
|
||
print(f" Paddle设备: {paddle.device.get_device()}")
|
||
return True
|
||
else:
|
||
print("[GPU检测] PaddleOCR无法使用GPU(需要安装paddlepaddle-gpu)")
|
||
return False
|
||
except ImportError:
|
||
print("[GPU检测] PaddleOCR需要paddlepaddle才能使用GPU")
|
||
return False
|
||
except ImportError:
|
||
print("[GPU检测] PaddleOCR未安装")
|
||
return False
|
||
except Exception as e:
|
||
print(f"[GPU检测] PaddleOCR GPU检测失败: {e}")
|
||
return False
|
||
|
||
|
||
def check_integrated_graphics():
|
||
"""检查是否是集成显卡(核显)"""
|
||
try:
|
||
try:
|
||
import torch # type: ignore # pylint: disable=import-error
|
||
except ImportError:
|
||
return False
|
||
|
||
if torch.cuda.is_available():
|
||
for i in range(torch.cuda.device_count()):
|
||
gpu_name = torch.cuda.get_device_name(i).lower()
|
||
# 常见的核显标识
|
||
integrated_keywords = [
|
||
"intel",
|
||
"iris",
|
||
"uhd",
|
||
"hd graphics",
|
||
"amd",
|
||
"radeon vega",
|
||
"radeon graphics",
|
||
]
|
||
is_integrated = any(
|
||
keyword in gpu_name for keyword in integrated_keywords
|
||
)
|
||
if is_integrated:
|
||
print(
|
||
f"[GPU检测] 检测到可能是集成显卡: {torch.cuda.get_device_name(i)}"
|
||
)
|
||
print("[GPU检测] 警告:集成显卡的GPU加速效果可能不明显")
|
||
return True
|
||
except Exception:
|
||
pass
|
||
return False
|
||
|
||
|
||
def main():
|
||
"""主函数"""
|
||
print("=" * 60)
|
||
print("GPU可用性检测")
|
||
print("=" * 60)
|
||
print()
|
||
|
||
# 检查PyTorch CUDA
|
||
print("[1] 检查PyTorch CUDA支持")
|
||
torch_cuda_available = check_torch_cuda()
|
||
print()
|
||
|
||
# 检查是否是核显
|
||
is_integrated = False # 初始化,避免未绑定错误
|
||
if torch_cuda_available:
|
||
print("[2] 检查显卡类型")
|
||
is_integrated = check_integrated_graphics()
|
||
print()
|
||
|
||
# 检查EasyOCR GPU支持
|
||
print("[3] 检查EasyOCR GPU支持")
|
||
easyocr_gpu_available = check_easyocr_gpu()
|
||
print()
|
||
|
||
# 检查PaddleOCR GPU支持
|
||
print("[4] 检查PaddleOCR GPU支持")
|
||
paddleocr_gpu_available = check_paddleocr_gpu()
|
||
print()
|
||
|
||
# 总结
|
||
print("=" * 60)
|
||
print("检测结果总结")
|
||
print("=" * 60)
|
||
|
||
if torch_cuda_available:
|
||
print("[OK] PyTorch CUDA可用")
|
||
if is_integrated:
|
||
print("[WARN] 检测到集成显卡,GPU加速效果可能有限")
|
||
else:
|
||
print("[OK] 检测到独立显卡,GPU加速效果较好")
|
||
else:
|
||
print("[NO] PyTorch CUDA不可用(将使用CPU模式)")
|
||
|
||
if easyocr_gpu_available:
|
||
print("[OK] EasyOCR可以使用GPU")
|
||
else:
|
||
print("[NO] EasyOCR无法使用GPU(将使用CPU模式)")
|
||
|
||
if paddleocr_gpu_available:
|
||
print("[OK] PaddleOCR可以使用GPU")
|
||
else:
|
||
print("[NO] PaddleOCR无法使用GPU(将使用CPU模式)")
|
||
|
||
print()
|
||
print("建议:")
|
||
if not torch_cuda_available:
|
||
print(" - 如果使用NVIDIA独立显卡,请安装CUDA和PyTorch GPU版本")
|
||
print(" - 如果使用集成显卡,GPU加速效果可能不明显,建议继续使用CPU模式")
|
||
elif is_integrated:
|
||
print(" - 集成显卡的GPU加速效果可能不明显")
|
||
print(" - 可以尝试启用GPU,但性能提升可能有限")
|
||
print(" - 如果遇到问题,建议继续使用CPU模式")
|
||
else:
|
||
print(" - 可以启用GPU加速以提升OCR性能")
|
||
print(" - 修改 Underlying_Operations.py 中的 gpu=False 为 gpu=True")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|