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

241 lines
9.5 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.
// GrooveOccupancyDetector.cpp
#include "GrooveOccupancyDetector.h"
#include <onnxruntime_cxx_api.h>
#include <QDebug>
#include <QtGlobal>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
struct GrooveOccupancyDetector::OrtImpl
{
std::unique_ptr<Ort::Env> env;
Ort::SessionOptions options;
std::unique_ptr<Ort::Session> session;
Ort::AllocatorWithDefaultOptions allocator;
std::string inputName;
std::string outputName;
};
GrooveOccupancyDetector::GrooveOccupancyDetector() = default;
GrooveOccupancyDetector::~GrooveOccupancyDetector() = default;
void GrooveOccupancyDetector::setError(const QString& error)
{
m_lastError = error;
qWarning() << "[GrooveOccupancyDetector]" << error;
}
bool GrooveOccupancyDetector::loadModel(const QString& onnxPath)
{
m_loaded = false;
m_modelPath.clear();
m_lastError.clear();
m_ort.reset();
try {
auto impl = std::make_unique<OrtImpl>();
impl->env = std::make_unique<Ort::Env>(ORT_LOGGING_LEVEL_WARNING,
"GrooveOccupancyDetector");
impl->options.SetIntraOpNumThreads(4);
impl->options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_ALL);
#ifdef _WIN32
const std::wstring widePath = onnxPath.toStdWString();
impl->session = std::make_unique<Ort::Session>(*impl->env,
widePath.c_str(), impl->options);
#else
impl->session = std::make_unique<Ort::Session>(*impl->env,
onnxPath.toUtf8().constData(), impl->options);
#endif
if (impl->session->GetInputCount() != 1 || impl->session->GetOutputCount() != 1) {
setError(QStringLiteral("凹槽模型必须只有一个输入和一个输出。"));
return false;
}
auto inputName = impl->session->GetInputNameAllocated(0, impl->allocator);
auto outputName = impl->session->GetOutputNameAllocated(0, impl->allocator);
impl->inputName = inputName.get();
impl->outputName = outputName.get();
const auto inputShape = impl->session->GetInputTypeInfo(0)
.GetTensorTypeAndShapeInfo().GetShape();
if (inputShape.size() != 4 || inputShape[1] != 3 ||
inputShape[2] <= 0 || inputShape[3] <= 0) {
setError(QStringLiteral("凹槽模型输入不是固定的 [1,3,H,W] RGB 张量。"));
return false;
}
m_inputH = static_cast<int>(inputShape[2]);
m_inputW = static_cast<int>(inputShape[3]);
const auto outputShape = impl->session->GetOutputTypeInfo(0)
.GetTensorTypeAndShapeInfo().GetShape();
if (outputShape.size() != 3 || outputShape[0] != 1 ||
outputShape[1] != 6 || outputShape[2] <= 0) {
setError(QStringLiteral(
"凹槽模型输出必须是 [1,6,N](xywh + empty/occupied 两类)。"));
return false;
}
m_ort = std::move(impl);
m_modelPath = onnxPath;
m_loaded = true;
qDebug() << "[GrooveOccupancyDetector] model loaded:" << onnxPath
<< "input:" << m_inputW << "x" << m_inputH;
return true;
}
catch (const Ort::Exception& exception) {
setError(QStringLiteral("ONNX Runtime 加载失败:%1").arg(exception.what()));
}
catch (const std::exception& exception) {
setError(QStringLiteral("凹槽模型加载失败:%1").arg(exception.what()));
}
return false;
}
std::vector<float> GrooveOccupancyDetector::preprocess(const QImage& image) const
{
const QImage rgb = image.convertToFormat(QImage::Format_RGB888);
// scaled() can promote the result to RGB32. Convert back before reading
// three bytes per pixel, so the CHW tensor is never byte-shifted.
const QImage resized = rgb.scaled(m_inputW, m_inputH,
Qt::IgnoreAspectRatio, Qt::SmoothTransformation)
.convertToFormat(QImage::Format_RGB888);
const size_t planeSize = static_cast<size_t>(m_inputW) * m_inputH;
std::vector<float> tensor(planeSize * 3);
for (int y = 0; y < m_inputH; ++y) {
const uchar* line = resized.constScanLine(y);
for (int x = 0; x < m_inputW; ++x) {
const size_t index = static_cast<size_t>(y) * m_inputW + x;
tensor[index] = line[x * 3] / 255.f;
tensor[planeSize + index] = line[x * 3 + 1] / 255.f;
tensor[planeSize * 2 + index] = line[x * 3 + 2] / 255.f;
}
}
return tensor;
}
QVector<GrooveDetectionResult> GrooveOccupancyDetector::nms(
QVector<GrooveDetectionResult> candidates, float iouThreshold) const
{
std::sort(candidates.begin(), candidates.end(),
[](const GrooveDetectionResult& left, const GrooveDetectionResult& right) {
return left.confidence > right.confidence;
});
QVector<GrooveDetectionResult> kept;
QVector<bool> suppressed(candidates.size(), false);
for (int i = 0; i < candidates.size(); ++i) {
if (suppressed[i]) continue;
kept.append(candidates[i]);
const QRectF& a = candidates[i].box;
for (int j = i + 1; j < candidates.size(); ++j) {
if (suppressed[j] || candidates[i].classId != candidates[j].classId)
continue;
const QRectF& b = candidates[j].box;
const float x1 = std::max(a.left(), b.left());
const float y1 = std::max(a.top(), b.top());
const float x2 = std::min(a.right(), b.right());
const float y2 = std::min(a.bottom(), b.bottom());
const float intersection = std::max(0.f, x2 - x1) *
std::max(0.f, y2 - y1);
const float unionArea = a.width() * a.height() +
b.width() * b.height() - intersection;
if (unionArea > 0.f && intersection / unionArea > iouThreshold)
suppressed[j] = true;
}
}
return kept;
}
QVector<GrooveDetectionResult> GrooveOccupancyDetector::postprocess(
const float* output, int numAnchors, int numFields, int originalWidth,
int originalHeight, float confidenceThreshold, float nmsThreshold) const
{
QVector<GrooveDetectionResult> candidates;
candidates.reserve(128);
const float scaleX = static_cast<float>(originalWidth) / m_inputW;
const float scaleY = static_cast<float>(originalHeight) / m_inputH;
for (int anchor = 0; anchor < numAnchors; ++anchor) {
const float emptyScore = output[4 * numAnchors + anchor];
const float occupiedScore = output[5 * numAnchors + anchor];
const int classId = occupiedScore > emptyScore ? 1 : 0;
const float score = classId == 0 ? emptyScore : occupiedScore;
if (!std::isfinite(score) || score < confidenceThreshold)
continue;
const float centerX = output[anchor];
const float centerY = output[numAnchors + anchor];
const float width = output[2 * numAnchors + anchor];
const float height = output[3 * numAnchors + anchor];
if (!(width > 0.f && height > 0.f))
continue;
const float x1 = qBound(0.f, (centerX - width * .5f) * scaleX,
static_cast<float>(originalWidth));
const float y1 = qBound(0.f, (centerY - height * .5f) * scaleY,
static_cast<float>(originalHeight));
const float x2 = qBound(0.f, (centerX + width * .5f) * scaleX,
static_cast<float>(originalWidth));
const float y2 = qBound(0.f, (centerY + height * .5f) * scaleY,
static_cast<float>(originalHeight));
if (x2 <= x1 || y2 <= y1)
continue;
candidates.append({ QRectF(x1, y1, x2 - x1, y2 - y1), classId, score });
}
return nms(std::move(candidates), nmsThreshold);
}
QVector<GrooveDetectionResult> GrooveOccupancyDetector::detect(const QImage& image,
float confidenceThreshold, float nmsThreshold)
{
if (!m_loaded || !m_ort || !m_ort->session) {
setError(QStringLiteral("凹槽模型尚未加载。"));
return {};
}
if (image.isNull()) {
setError(QStringLiteral("凹槽检测输入图像为空。"));
return {};
}
try {
std::vector<float> input = preprocess(image);
const std::vector<int64_t> inputShape = { 1, 3, m_inputH, m_inputW };
const Ort::MemoryInfo memory = Ort::MemoryInfo::CreateCpu(
OrtArenaAllocator, OrtMemTypeDefault);
Ort::Value inputTensor = Ort::Value::CreateTensor<float>(memory,
input.data(), input.size(), inputShape.data(), inputShape.size());
const char* inputName = m_ort->inputName.c_str();
const char* outputName = m_ort->outputName.c_str();
auto outputs = m_ort->session->Run(Ort::RunOptions{ nullptr },
&inputName, &inputTensor, 1, &outputName, 1);
if (outputs.empty()) {
setError(QStringLiteral("凹槽模型没有返回输出张量。"));
return {};
}
const auto shape = outputs[0].GetTensorTypeAndShapeInfo().GetShape();
if (shape.size() != 3 || shape[0] != 1 || shape[1] != 6 || shape[2] <= 0) {
setError(QStringLiteral("凹槽模型推理输出形状异常。"));
return {};
}
return postprocess(outputs[0].GetTensorData<float>(),
static_cast<int>(shape[2]), static_cast<int>(shape[1]),
image.width(), image.height(), confidenceThreshold, nmsThreshold);
}
catch (const Ort::Exception& exception) {
setError(QStringLiteral("凹槽 ONNX 推理失败:%1").arg(exception.what()));
}
catch (const std::exception& exception) {
setError(QStringLiteral("凹槽检测失败:%1").arg(exception.what()));
}
return {};
}