207 lines
8.6 KiB
C++
207 lines
8.6 KiB
C++
#include "GroovePoseDetector.h"
|
|
|
|
#include <onnxruntime_cxx_api.h>
|
|
#include <QtGlobal>
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <vector>
|
|
#include <QPainter>
|
|
|
|
struct GroovePoseDetector::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;
|
|
};
|
|
|
|
GroovePoseDetector::GroovePoseDetector() = default;
|
|
GroovePoseDetector::~GroovePoseDetector() = default;
|
|
|
|
void GroovePoseDetector::setError(const QString& error) const
|
|
{
|
|
m_lastError = error;
|
|
}
|
|
|
|
bool GroovePoseDetector::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,
|
|
"GroovePoseDetector");
|
|
impl->options.SetIntraOpNumThreads(4);
|
|
impl->options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_ALL);
|
|
#ifdef _WIN32
|
|
impl->session = std::make_unique<Ort::Session>(*impl->env,
|
|
onnxPath.toStdWString().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 inName = impl->session->GetInputNameAllocated(0, impl->allocator);
|
|
auto outName = impl->session->GetOutputNameAllocated(0, impl->allocator);
|
|
impl->inputName = inName.get();
|
|
impl->outputName = outName.get();
|
|
const auto inShape = impl->session->GetInputTypeInfo(0)
|
|
.GetTensorTypeAndShapeInfo().GetShape();
|
|
const auto outShape = impl->session->GetOutputTypeInfo(0)
|
|
.GetTensorTypeAndShapeInfo().GetShape();
|
|
if (inShape.size() != 4 || inShape[1] != 3 || inShape[2] <= 0 || inShape[3] <= 0 ||
|
|
outShape.size() != 3 || outShape[0] != 1 || outShape[1] != 17 || outShape[2] <= 0) {
|
|
setError(QStringLiteral("凹槽位姿模型输入/输出形状异常,要求输入[1,3,H,W]、输出[1,17,N]。"));
|
|
return false;
|
|
}
|
|
m_inputH = static_cast<int>(inShape[2]);
|
|
m_inputW = static_cast<int>(inShape[3]);
|
|
m_ort = std::move(impl);
|
|
m_modelPath = onnxPath;
|
|
m_loaded = true;
|
|
return true;
|
|
} catch (const Ort::Exception& e) {
|
|
setError(QStringLiteral("凹槽位姿 ONNX 加载失败:%1").arg(e.what()));
|
|
} catch (const std::exception& e) {
|
|
setError(QStringLiteral("凹槽位姿模型加载失败:%1").arg(e.what()));
|
|
}
|
|
return false;
|
|
}
|
|
|
|
std::vector<float> GroovePoseDetector::preprocess(const QImage& image,
|
|
float& scale,
|
|
int& padX,
|
|
int& padY) const
|
|
{
|
|
const QImage rgb = image.convertToFormat(QImage::Format_RGB888);
|
|
scale = qMin(static_cast<float>(m_inputW) / qMax(1, rgb.width()),
|
|
static_cast<float>(m_inputH) / qMax(1, rgb.height()));
|
|
const int resizedW = qMax(1, qRound(rgb.width() * scale));
|
|
const int resizedH = qMax(1, qRound(rgb.height() * scale));
|
|
padX = (m_inputW - resizedW) / 2;
|
|
padY = (m_inputH - resizedH) / 2;
|
|
|
|
const QImage resized = rgb.scaled(resizedW, resizedH,
|
|
Qt::IgnoreAspectRatio, Qt::SmoothTransformation)
|
|
.convertToFormat(QImage::Format_RGB888);
|
|
QImage letterboxed(m_inputW, m_inputH, QImage::Format_RGB888);
|
|
letterboxed.fill(QColor(114, 114, 114));
|
|
{
|
|
QPainter painter(&letterboxed);
|
|
painter.drawImage(padX, padY, resized);
|
|
}
|
|
const size_t plane = static_cast<size_t>(m_inputW) * m_inputH;
|
|
std::vector<float> tensor(plane * 3);
|
|
for (int y = 0; y < m_inputH; ++y) {
|
|
const uchar* line = letterboxed.constScanLine(y);
|
|
for (int x = 0; x < m_inputW; ++x) {
|
|
const size_t i = static_cast<size_t>(y) * m_inputW + x;
|
|
tensor[i] = line[x * 3] / 255.0f;
|
|
tensor[plane + i] = line[x * 3 + 1] / 255.0f;
|
|
tensor[plane * 2 + i] = line[x * 3 + 2] / 255.0f;
|
|
}
|
|
}
|
|
return tensor;
|
|
}
|
|
|
|
QVector<GroovePosePrediction> GroovePoseDetector::nms(
|
|
QVector<GroovePosePrediction> candidates, float iouThreshold) const
|
|
{
|
|
std::sort(candidates.begin(), candidates.end(), [](const auto& a, const auto& b) {
|
|
return a.confidence > b.confidence;
|
|
});
|
|
QVector<GroovePosePrediction> 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]) continue;
|
|
const QRectF b = candidates[j].box;
|
|
const qreal x1 = qMax(a.left(), b.left());
|
|
const qreal y1 = qMax(a.top(), b.top());
|
|
const qreal x2 = qMin(a.right(), b.right());
|
|
const qreal y2 = qMin(a.bottom(), b.bottom());
|
|
const qreal inter = qMax<qreal>(0, x2 - x1) * qMax<qreal>(0, y2 - y1);
|
|
const qreal uni = a.width() * a.height() + b.width() * b.height() - inter;
|
|
if (uni > 0 && inter / uni > iouThreshold) suppressed[j] = true;
|
|
}
|
|
}
|
|
return kept;
|
|
}
|
|
|
|
QVector<GroovePosePrediction> GroovePoseDetector::postprocess(
|
|
const float* output, int n, int fields, int ow, int oh,
|
|
float scale, int padX, int padY,
|
|
float confThreshold, float nmsThreshold) const
|
|
{
|
|
QVector<GroovePosePrediction> candidates;
|
|
if (!output || fields != 17 || scale <= 0.0f) return candidates;
|
|
const auto mapX = [scale, padX, ow](float x) {
|
|
return qBound<qreal>(0.0, (x - padX) / scale, static_cast<qreal>(ow));
|
|
};
|
|
const auto mapY = [scale, padY, oh](float y) {
|
|
return qBound<qreal>(0.0, (y - padY) / scale, static_cast<qreal>(oh));
|
|
};
|
|
for (int i = 0; i < n; ++i) {
|
|
const float conf = output[4 * n + i];
|
|
if (!std::isfinite(conf) || conf < confThreshold) continue;
|
|
const float cx = output[i], cy = output[n + i];
|
|
const float w = output[2 * n + i], h = output[3 * n + i];
|
|
if (!(w > 0 && h > 0)) continue;
|
|
const qreal x1 = mapX(cx - w * .5f);
|
|
const qreal y1 = mapY(cy - h * .5f);
|
|
const qreal x2 = mapX(cx + w * .5f);
|
|
const qreal y2 = mapY(cy + h * .5f);
|
|
if (x2 <= x1 || y2 <= y1) continue;
|
|
GroovePosePrediction p;
|
|
p.box = QRectF(x1, y1, x2 - x1, y2 - y1);
|
|
p.confidence = conf;
|
|
for (int k = 0; k < 4; ++k) {
|
|
const int base = (5 + k * 3) * n + i;
|
|
p.keypoints.append(QPointF(mapX(output[base]), mapY(output[base + n])));
|
|
p.keypointConfidence.append(output[base + 2 * n]);
|
|
}
|
|
candidates.append(p);
|
|
}
|
|
return nms(std::move(candidates), nmsThreshold);
|
|
}
|
|
|
|
QVector<GroovePosePrediction> GroovePoseDetector::detect(
|
|
const QImage& image, float confidenceThreshold, float nmsThreshold) const
|
|
{
|
|
if (!m_loaded || !m_ort || !m_ort->session || image.isNull()) return {};
|
|
try {
|
|
float scale = 1.0f;
|
|
int padX = 0, padY = 0;
|
|
std::vector<float> input = preprocess(image, scale, padX, padY);
|
|
const std::vector<int64_t> shape = {1, 3, m_inputH, m_inputW};
|
|
const auto memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
|
|
auto tensor = Ort::Value::CreateTensor<float>(memory, input.data(), input.size(),
|
|
shape.data(), shape.size());
|
|
const char* in = m_ort->inputName.c_str();
|
|
const char* out = m_ort->outputName.c_str();
|
|
auto outputs = m_ort->session->Run(Ort::RunOptions{nullptr}, &in, &tensor, 1, &out, 1);
|
|
if (outputs.empty()) return {};
|
|
const auto outShape = outputs[0].GetTensorTypeAndShapeInfo().GetShape();
|
|
if (outShape.size() != 3 || outShape[1] != 17) return {};
|
|
return postprocess(outputs[0].GetTensorData<float>(), static_cast<int>(outShape[2]),
|
|
static_cast<int>(outShape[1]), image.width(), image.height(),
|
|
scale, padX, padY,
|
|
confidenceThreshold, nmsThreshold);
|
|
} catch (const Ort::Exception& e) {
|
|
setError(QStringLiteral("凹槽位姿推理失败:%1").arg(e.what()));
|
|
} catch (const std::exception& e) {
|
|
setError(QStringLiteral("凹槽位姿推理异常:%1").arg(e.what()));
|
|
}
|
|
return {};
|
|
}
|