Files
douyin_guaji-main/check_gpu_available.py
T
Your Name f92933641f feat: Enhance documentation and clean up code structure
- Added comprehensive documentation for the unified screenshot and score recording process in the new markdown file.
- Improved existing markdown files by adding spacing for better readability.
- Cleaned up code by removing unnecessary blank lines across various scripts, enhancing overall code clarity.
- Updated configuration settings to reflect recent changes and ensure consistency across the project.
- Enhanced logging mechanisms to support better debugging and monitoring.
2025-12-25 16:11:25 +08:00

196 lines
6.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
检查GPU是否可用的脚本
"""
import sys
# 修复Windows控制台编码问题
if sys.platform == "win32":
try:
sys.stdout.reconfigure(encoding="utf-8") # type: ignore[attr-defined]
except Exception:
pass
def check_torch_cuda():
"""检查PyTorch是否支持CUDA"""
try:
import torch # type: ignore
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 ImportError:
print("[GPU检测] PyTorch未安装")
return False
except Exception as e:
print(f"[GPU检测] PyTorch CUDA检测失败: {e}")
return False
def check_easyocr_gpu():
"""检查EasyOCR是否可以使用GPU"""
try:
import easyocr # type: ignore
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:
from paddleocr import PaddleOCR
print("[GPU检测] PaddleOCR已安装")
# PaddleOCR需要paddlepaddle-gpu版本
try:
import paddle
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:
import torch # type: ignore
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()
# 检查是否是核显
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()