Files
ModelTrainEval/deploy/cpp/main.cpp
T

50 lines
2.1 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.
// 演示与对拍工具:读一张原始像素转储,推理并打印框。
//
// ./film_predict <model.xml> <image.bin> <宽> <高> <通道> [conf] [tile]
//
// 输入是连续的 HWC uint8 缓冲(用 scripts/dump_raw.py 从图片生成)。产线里通常直接
// 拿相机帧的指针,不需要这一步。tile 传 0 表示图本身就是网络输入尺寸。
#include "film_detector.hpp"
#include <cstdio>
#include <fstream>
#include <iostream>
#include <vector>
int main(int argc, char** argv) {
if (argc < 6) {
std::cerr << "用法: " << argv[0]
<< " <model.xml> <image.bin> <宽> <高> <通道> [conf] [tile-overlap]\n";
return 1;
}
const std::string model_path = argv[1], image_path = argv[2];
const int width = std::stoi(argv[3]), height = std::stoi(argv[4]);
const int channels = std::stoi(argv[5]);
const float conf = argc > 6 ? std::stof(argv[6]) : 0.25f;
const int overlap = argc > 7 ? std::stoi(argv[7]) : 32;
std::ifstream file(image_path, std::ios::binary);
if (!file) { std::cerr << "打不开 " << image_path << "\n"; return 1; }
std::vector<unsigned char> pixels((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
const size_t expected = static_cast<size_t>(width) * height * channels;
if (pixels.size() != expected) {
std::cerr << "缓冲大小 " << pixels.size() << " 与 " << width << "x" << height
<< "x" << channels << " 不符(应为 " << expected << ")\n";
return 1;
}
film::Detector detector(model_path);
film::ImageView image{pixels.data(), width, height, channels};
const auto boxes = (width == detector.input_size() && height == detector.input_size())
? detector.infer_tile(image, conf)
: detector.infer(image, conf, 0.5f, overlap);
std::printf("%zu\n", boxes.size());
for (const auto& b : boxes) {
std::printf("%d %.6f %.6f %.6f %.6f %.4f\n",
b.class_id, b.x1, b.y1, b.x2, b.y2, b.score);
}
return 0;
}