68 lines
2.8 KiB
C++

#include "bmp_loader.h"
#include "result_json.h"
#include "yolo_runtime.h"
#include <cstdlib>
#include <iostream>
#include <memory>
#include <string_view>
static const char* value_after(int argc, char** argv, std::string_view option) {
for (int index = 1; index + 1 < argc; ++index)
if (argv[index] == option) return argv[index + 1];
return nullptr;
}
struct RuntimeDeleter {
void operator()(yolo_runtime_t* runtime) const { yolo_runtime_destroy(runtime); }
};
int main(int argc, char** argv) {
const char* config = value_after(argc, argv, "--config");
const char* image_path = value_after(argc, argv, "--image");
const char* json_path = value_after(argc, argv, "--json");
if (!config || !image_path || !json_path) {
std::cerr << "Usage: yolo_cpp_example --config model.yaml --image sample.bmp --json result.json\n";
return 2;
}
if (yolo_runtime_get_abi_version() != YOLO_RUNTIME_ABI_VERSION) return 3;
unsigned char* raw_pixels = nullptr;
int width = 0, height = 0;
if (load_bmp_rgb(image_path, &raw_pixels, &width, &height) != 0) return 4;
std::unique_ptr<unsigned char, decltype(&free_bmp_rgb)> pixels(raw_pixels, free_bmp_rgb);
yolo_runtime_t* raw_runtime = nullptr;
if (yolo_runtime_create(config, &raw_runtime) != YOLO_STATUS_OK || !raw_runtime) return 5;
std::unique_ptr<yolo_runtime_t, RuntimeDeleter> runtime(raw_runtime);
if ((yolo_runtime_get_capabilities(runtime.get()) & YOLO_CAPABILITY_INFER_IMAGE) == 0) return 6;
yolo_image_view_t image{};
image.struct_size = sizeof(image);
image.abi_version = YOLO_IMAGE_VIEW_ABI_VERSION;
image.data = pixels.get();
image.width = width;
image.height = height;
image.row_stride_bytes = width * 3;
image.pixel_format = YOLO_PIXEL_FORMAT_RGB888;
yolo_result_list_t results{};
const int status = yolo_runtime_infer_image(runtime.get(), &image, &results);
if (status != YOLO_STATUS_OK) {
std::cerr << "Inference failed: " << yolo_runtime_last_error(runtime.get()) << '\n';
return 7;
}
std::cout << "results=" << results.count
<< " preprocess=" << results.performance.preprocess_ms
<< "ms inference=" << results.performance.inference_ms
<< "ms postprocess=" << results.performance.postprocess_ms
<< "ms total=" << results.performance.total_ms << "ms\n";
for (int index = 0; index < results.count; ++index) {
const auto& result = results.results[index];
std::cout << '[' << index << "] class=" << result.class_id
<< " label=" << result.label << " score=" << result.score
<< " angle=" << result.bbox.angle << '\n';
}
return write_result_json(json_path, config, &results) == 0 ? 0 : 8;
}