Compare commits

...

1 Commits

@ -7,6 +7,7 @@
#include <QDir>
#include <QFile>
#include <QDateTime>
#include <QCoreApplication>
/**
*
@ -263,7 +264,27 @@ void VisionModelPage::runYOLOv5Detection(const QString& imagePath) {
if (isRunning_) {
return;
}
// 构建Python脚本路径优先使用相对路径保证跨环境可用
QDir appDir(QCoreApplication::applicationDirPath());
const QStringList candidateScripts = {
QDir::cleanPath(appDir.filePath("../Src/vision_models/services/yolov5_detection_service_simple.py")),
QDir::cleanPath(appDir.filePath("Src/vision_models/services/yolov5_detection_service_simple.py")),
QDir::cleanPath(appDir.filePath("vision_models/services/yolov5_detection_service_simple.py"))
};
QString pythonScript;
for (const QString& candidate : candidateScripts) {
if (QFile::exists(candidate)) {
pythonScript = candidate;
break;
}
}
if (pythonScript.isEmpty()) {
showError("找不到YOLOv5检测脚本请确认项目目录结构完整。");
return;
}
isRunning_ = true;
runBtn_->setText("检测中...");
runBtn_->setEnabled(false);
@ -272,7 +293,6 @@ void VisionModelPage::runYOLOv5Detection(const QString& imagePath) {
progressTimer_->start(100);
// 构建Python命令
QString pythonScript = QDir::currentPath() + "/vision_models/services/yolov5_detection_service_simple.py";
QStringList arguments;
arguments << pythonScript << "--image" << imagePath;

@ -20,6 +20,12 @@ except ImportError as e:
print(f"YOLOv5导入失败: {e}")
YOLOV5_AVAILABLE = False
SCRIPT_DIR = Path(__file__).resolve().parent
PROJECT_ROOT = SCRIPT_DIR.parents[2] # /Src/vision_models/services -> root
MODELS_DIR = SCRIPT_DIR.parent / "models"
YOLOROOT_DIR = SCRIPT_DIR.parent / "yolov5"
class YOLOv5Detector:
"""YOLOv5目标检测器 - 简化版本"""
@ -33,17 +39,32 @@ class YOLOv5Detector:
# 加载模型
try:
if weights_path and os.path.exists(weights_path):
self.model = YOLO(weights_path)
else:
# 优先使用现有的yolov5su.pt模型
if os.path.exists('yolov5su.pt'):
self.model = YOLO('yolov5su.pt')
elif os.path.exists('../models/yolov5su.pt'):
self.model = YOLO('../models/yolov5su.pt')
else:
# 使用默认的YOLOv5s模型
self.model = YOLO('yolov5s.pt')
candidate_paths = []
if weights_path:
candidate_paths.append(Path(weights_path))
# 优先选择项目内模型
candidate_paths.extend([
MODELS_DIR / "yolov5su.pt",
SCRIPT_DIR / "yolov5su.pt",
Path.cwd() / "yolov5su.pt",
MODELS_DIR / "yolov5s.pt",
SCRIPT_DIR / "yolov5s.pt",
Path.cwd() / "yolov5s.pt",
])
model_path = None
for path in candidate_paths:
if path.exists():
model_path = path
break
if model_path is None:
raise FileNotFoundError(
"未找到YOLO模型权重请将 yolov5su.pt 或 yolov5s.pt 放在 Src/vision_models/models 目录。"
)
self.model = YOLO(str(model_path))
print(f"✅ 模型加载成功: {self.model.model_name if hasattr(self.model, 'model_name') else 'YOLOv5'}")
except Exception as e:
raise RuntimeError(f"模型加载失败: {e}")

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save