Код: Выделить всё
#include "gtest/gtest.h"
#include "onnx_helper.h"
#include "vector_helper.h"
#include "opencv_helper.h"
#include
#include
class OnnxTest : public testing::Test {
protected:
// static void SetUpTestCase() {
// }
};
Ndarray i2t2(const std::string &img_path) {// Load the image using OpenCV
cv::Mat input_mat = cv::imread(img_path);
// Convert to float
cv::cvtColor(input_mat, input_mat, cv::COLOR_BGR2RGB);
input_mat.convertTo(input_mat, CV_32F);
// Normalize
input_mat /= 255.0;
// Convert the preprocessed image to a vector of floats
Ndarray input_img = mat_to_ndarray(input_mat);
// Transpose and expand dimensions
input_img.transpose({2, 0, 1});
input_img.expand_dims(0);
return input_img;
}
// Assuming you have already defined the necessary functions for image preprocessing
Ort::Value i2t(const std::string &img_path) {
Ndarray input_img = i2t2(img_path);
// Create the input tensor
auto input_vector = input_img.data;
auto input_shape = input_img.shape;
return Ort::Value::CreateTensor(
mem_info,
input_vector.data(),
input_vector.size(),
input_shape.data(),
input_shape.size());
}
TEST_F(OnnxTest, run_onnx) {
// Define paths
auto d = "/home/roroco/Dropbox/cpp/cpp_lib/test/fix/onnx";
std::string onnx_model_path = std::format("{}/RealESRGAN_x4plus_anime_6B.onnx", d);
std::string input_img_path = std::format("{}/small.jpg", d);
const std::string &out_img_path = std::format("/tmp/t.jpg", d);
// Run inference
auto s = new_session(onnx_model_path);
auto tensor = i2t(input_img_path);
// Ndarray input_img = i2t2(input_img_path);
// // Create the input tensor
//// auto mem_info = Ort::MemoryInfo::CreateCpu(OrtAllocatorType::OrtArenaAllocator,
//// OrtMemType::OrtMemTypeDefault);
// auto input_vector = input_img.data;
// auto input_shape = input_img.shape;
// auto tensor = Ort::Value::CreateTensor(
// mem_info,
// input_vector.data(),
// input_vector.size(),
// input_shape.data(),
// input_shape.size());
Ort::Value out_tensor = infer(s, tensor);
tensor_to_img(out_tensor, out_img_path);
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/78511513/in-cpp-return-cause-stdvector-value-changed[/url]
Мобильная версия