Files
VisionGuide/source/YoloDetector.cpp
T
2026-10-26 10:56:56 +08:00

371 lines
14 KiB
C++
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.
// YoloDetector.cpp
// Build notes (VS2017 / MSVC 19.1x):
// - Add onnxruntime include path and link against onnxruntime.lib
// - Copy the matching onnxruntime.dll next to your .exe so Windows does NOT
// pick up C:\Windows\System32\onnxruntime.dll (version mismatch causes the
// "Ort::GetApi() returned nullptr" crash).
// - Requires ONNX Runtime >= 1.13 for GetInputNameAllocated().
#include "YoloDetector.h"
// ORT headers are ONLY included in this translation unit (pimpl).
#include <onnxruntime_cxx_api.h>
#include <QPainter>
#include <QDebug>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
// ── OrtImpl (pimpl body) ─────────────────────────────────────────────────────
struct YoloDetector::OrtImpl
{
// Env must outlive Session. Use unique_ptr so construction is deferred.
std::unique_ptr<Ort::Env> env;
Ort::SessionOptions opts;
std::unique_ptr<Ort::Session> session;
Ort::AllocatorWithDefaultOptions alloc;
// We store strings to keep them alive, then build pointer arrays for Run().
std::vector<std::string> inputNamesBuf;
std::vector<std::string> outputNamesBuf;
std::vector<const char*> inputNamePtrs;
std::vector<const char*> outputNamePtrs;
};
// ── ctor / dtor ──────────────────────────────────────────────────────────────
YoloDetector::YoloDetector(QObject* parent) : QObject(parent) {}
YoloDetector::~YoloDetector() = default;
// ── loadModel ────────────────────────────────────────────────────────────────
bool YoloDetector::loadModel(const QString& onnxPath)
{
m_loaded = false;
m_ort.reset();
try {
auto impl = std::make_unique<OrtImpl>();
// ── Create ORT environment ──────────────────────────────────────────
impl->env = std::make_unique<Ort::Env>(
ORT_LOGGING_LEVEL_WARNING, "YoloDetector");
// ── Session options ─────────────────────────────────────────────────
impl->opts.SetIntraOpNumThreads(4);
impl->opts.SetGraphOptimizationLevel(
GraphOptimizationLevel::ORT_ENABLE_ALL);
// ── Load model ──────────────────────────────────────────────────────
#ifdef _WIN32
std::wstring wPath = onnxPath.toStdWString();
impl->session = std::make_unique<Ort::Session>(
*impl->env, wPath.c_str(), impl->opts);
#else
impl->session = std::make_unique<Ort::Session>(
*impl->env, onnxPath.toUtf8().constData(), impl->opts);
#endif
// ── Input metadata ──────────────────────────────────────────────────
size_t numInputs = impl->session->GetInputCount();
for (size_t i = 0; i < numInputs; ++i) {
auto ptr = impl->session->GetInputNameAllocated(i, impl->alloc);
impl->inputNamesBuf.emplace_back(ptr.get());
}
for (auto& s : impl->inputNamesBuf)
impl->inputNamePtrs.push_back(s.c_str());
// Read input shape [1, 3, H, W] – dynamic dims may be -1
auto inInfo = impl->session->GetInputTypeInfo(0)
.GetTensorTypeAndShapeInfo();
auto inShape = inInfo.GetShape();
if (inShape.size() == 4) {
if (inShape[2] > 0) m_inputH = static_cast<int>(inShape[2]);
if (inShape[3] > 0) m_inputW = static_cast<int>(inShape[3]);
}
// ── Output metadata ─────────────────────────────────────────────────
size_t numOutputs = impl->session->GetOutputCount();
for (size_t i = 0; i < numOutputs; ++i) {
auto ptr = impl->session->GetOutputNameAllocated(i, impl->alloc);
impl->outputNamesBuf.emplace_back(ptr.get());
}
for (auto& s : impl->outputNamesBuf)
impl->outputNamePtrs.push_back(s.c_str());
// YOLOv8 default output: [1, 4+nc, num_anchors]
auto outInfo = impl->session->GetOutputTypeInfo(0)
.GetTensorTypeAndShapeInfo();
auto outShape = outInfo.GetShape();
if (outShape.size() >= 2 && outShape[1] > 4)
m_numClasses = static_cast<int>(outShape[1]) - 4;
m_ort = std::move(impl);
m_loaded = true;
m_modelPath = onnxPath;
// Fill default class names if the caller hasn't set them yet
if (m_classNames.isEmpty() || m_classNames.size() != m_numClasses) {
m_classNames.clear();
// 修正:匹配模型的3类命名
if (m_numClasses == 3) {
m_classNames << QStringLiteral("干扰物")
<< QStringLiteral("镀金零件")
<< QStringLiteral("非镀金零件");
}
else if (m_numClasses == 2) { // 兼容旧2类模型
m_classNames << QStringLiteral("镀金零件")
<< QStringLiteral("非镀金零件");
}
else {
for (int i = 0; i < m_numClasses; ++i)
m_classNames << QString("class%1").arg(i);
}
}
qDebug() << "[YoloDetector] model loaded:" << onnxPath
<< " input:" << m_inputW << "x" << m_inputH
<< " classes:" << m_numClasses;
emit modelLoaded(onnxPath);
return true;
}
catch (const Ort::Exception& e) {
QString msg = QString("[YoloDetector] ORT exception: %1").arg(e.what());
qWarning() << msg;
emit errorOccurred(msg);
}
catch (const std::exception& e) {
QString msg = QString("[YoloDetector] std::exception: %1").arg(e.what());
qWarning() << msg;
emit errorOccurred(msg);
}
return false;
}
// ── setClassNames ────────────────────────────────────────────────────────────
void YoloDetector::setClassNames(const QStringList& names)
{
m_classNames = names;
m_numClasses = names.size();
}
// ── preprocess ───────────────────────────────────────────────────────────────
std::vector<float> YoloDetector::preprocess(
const QImage& img, int targetW, int targetH)
{
// Smooth scaling may change RGB888 to RGB32. Convert AFTER scaling so
// the tensor loop always reads three-byte RGB pixels, including live frames.
QImage resized = img.scaled(targetW, targetH,
Qt::IgnoreAspectRatio,
Qt::SmoothTransformation).convertToFormat(QImage::Format_RGB888);
std::vector<float> data(static_cast<size_t>(3 * targetH * targetW));
const size_t planeSize = static_cast<size_t>(targetH * targetW);
for (int y = 0; y < targetH; ++y) {
const uchar* line = resized.constScanLine(y);
for (int x = 0; x < targetW; ++x) {
size_t idx = static_cast<size_t>(y * targetW + x);
data[0 * planeSize + idx] = line[x * 3 + 0] / 255.f; // R
data[1 * planeSize + idx] = line[x * 3 + 1] / 255.f; // G
data[2 * planeSize + idx] = line[x * 3 + 2] / 255.f; // B
}
}
return data;
}
// ── NMS (Non-Maximum Suppression) ────────────────────────────────────────────
QVector<YoloResult> YoloDetector::nms(QVector<YoloResult>& candidates, float iouThresh)
{
QVector<YoloResult> keep;
if (candidates.isEmpty()) return keep;
// Sort by confidence descending
std::sort(candidates.begin(), candidates.end(),
[](const YoloResult& a, const YoloResult& b) {
return a.conf > b.conf;
});
QVector<bool> suppressed(candidates.size(), false);
for (int i = 0; i < candidates.size(); ++i) {
if (suppressed[i]) continue;
keep.append(candidates[i]);
for (int j = i + 1; j < candidates.size(); ++j) {
if (suppressed[j] || candidates[i].classId != candidates[j].classId)
continue;
// Calculate IoU
const QRectF& a = candidates[i].box;
const QRectF& b = candidates[j].box;
float interX1 = std::max(a.left(), b.left());
float interY1 = std::max(a.top(), b.top());
float interX2 = std::min(a.right(), b.right());
float interY2 = std::min(a.bottom(), b.bottom());
float interArea = std::max(0.f, interX2 - interX1) * std::max(0.f, interY2 - interY1);
float aArea = a.width() * a.height();
float bArea = b.width() * b.height();
float iou = interArea / (aArea + bArea - interArea);
if (iou > iouThresh)
suppressed[j] = true;
}
}
return keep;
}
// ── postprocess ──────────────────────────────────────────────────────────────
QVector<YoloResult> YoloDetector::postprocess(
const float* data, int numAnchors, int numFields,
int origW, int origH, float confThresh, float nmsThresh, int filterClass)
{
int nc = numFields - 4;
if (nc <= 0) return {};
const float scaleW = static_cast<float>(origW) / m_inputW;
const float scaleH = static_cast<float>(origH) / m_inputH;
// ── Decode candidates ────────────────────────────────────────────────────
QVector<YoloResult> candidates;
candidates.reserve(256);
for (int a = 0; a < numAnchors; ++a) {
// Find the highest-scoring class
int bestClass = -1;
float bestScore = confThresh;
for (int c = 0; c < nc; ++c) {
float score = data[(4 + c) * numAnchors + a];
if (score > bestScore) { bestScore = score; bestClass = c; }
}
if (bestClass < 0) continue;
if (filterClass >= 0 && bestClass != filterClass) continue;
float cx = data[0 * numAnchors + a];
float cy = data[1 * numAnchors + a];
float bw = data[2 * numAnchors + a];
float bh = data[3 * numAnchors + a];
float x1 = qBound(0.f, (cx - bw * 0.5f) * scaleW, (float)origW);
float y1 = qBound(0.f, (cy - bh * 0.5f) * scaleH, (float)origH);
float x2 = qBound(0.f, (cx + bw * 0.5f) * scaleW, (float)origW);
float y2 = qBound(0.f, (cy + bh * 0.5f) * scaleH, (float)origH);
YoloResult res;
res.box = QRectF(x1, y1, x2 - x1, y2 - y1);
res.classId = bestClass;
res.conf = bestScore;
res.label = m_classNames.value(bestClass, QString("class%1").arg(bestClass));
candidates.append(res);
}
// ── Apply NMS ────────────────────────────────────────────────────────────
return nms(candidates, nmsThresh);
}
// ── detect ───────────────────────────────────────────────────────────────────
QVector<YoloResult> YoloDetector::detect(
const QImage& img,
float confThresh, float nmsThresh, int filterClass)
{
if (!m_loaded || !m_ort || !m_ort->session) {
emit errorOccurred("YOLO model not loaded");
return {};
}
if (img.isNull()) return {};
const int origW = img.width();
const int origH = img.height();
try {
// ── Pre-process ─────────────────────────────────────────────────────
auto inputData = preprocess(img, m_inputW, m_inputH);
std::vector<int64_t> inputShape = { 1, 3,
static_cast<int64_t>(m_inputH),
static_cast<int64_t>(m_inputW) };
Ort::MemoryInfo memInfo =
Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
Ort::Value inputTensor = Ort::Value::CreateTensor<float>(
memInfo,
inputData.data(), inputData.size(),
inputShape.data(), inputShape.size());
// ── Inference ───────────────────────────────────────────────────────
auto outputs = m_ort->session->Run(
Ort::RunOptions{ nullptr },
m_ort->inputNamePtrs.data(), &inputTensor, 1,
m_ort->outputNamePtrs.data(), m_ort->outputNamePtrs.size());
// ── Post-process ────────────────────────────────────────────────────
const float* outData = outputs[0].GetTensorData<float>();
auto outShape = outputs[0].GetTensorTypeAndShapeInfo().GetShape();
// Expected layout: [1, 4+nc, num_anchors]
if (outShape.size() < 3) {
emit errorOccurred("Unexpected output shape");
return {};
}
int numFields = static_cast<int>(outShape[1]); // 4 + nc
int numAnchors = static_cast<int>(outShape[2]);
return postprocess(outData, numAnchors, numFields,
origW, origH, confThresh, nmsThresh, filterClass);
}
catch (const Ort::Exception& e) {
emit errorOccurred(QString("ORT inference error: %1").arg(e.what()));
}
catch (const std::exception& e) {
emit errorOccurred(QString("Detect error: %1").arg(e.what()));
}
return {};
}
// ── detectAndDraw ────────────────────────────────────────────────────────────
QImage YoloDetector::detectAndDraw(const QImage& img,
float confThresh, float nmsThresh, int filterClass)
{
QImage result = img.copy();
auto detections = detect(img, confThresh, nmsThresh, filterClass);
QPainter painter(&result);
painter.setRenderHint(QPainter::Antialiasing);
// Define colors for different classes (extend as needed)
QVector<QColor> colors = {
QColor(255, 0, 0), // Class 0: Red (镀金零件)
QColor(0, 255, 0), // Class 1: Green (非镀金零件)
QColor(0, 0, 255), // Class 2: Blue
QColor(255, 255, 0), // Class 3: Yellow
QColor(255, 0, 255) // Class 4: Magenta
};
for (const auto& res : detections) {
QColor color = colors[res.classId % colors.size()];
painter.setPen(QPen(color, 2));
painter.drawRect(res.box);
// Draw label + confidence
QString label = QString("%1 (%.2f)").arg(res.label).arg(res.conf);
QFont font = painter.font();
font.setBold(true);
painter.setFont(font);
QRectF textRect = painter.boundingRect(
QRectF(res.box.topLeft(), QSizeF(200, 30)),
Qt::AlignLeft | Qt::AlignTop, label);
textRect.adjust(-2, -2, 2, 2);
painter.fillRect(textRect, color);
painter.setPen(Qt::white);
painter.drawText(res.box.topLeft() + QPointF(2, 12), label);
}
return result;
}