人脸3D关键点的python版本和C++版本区别

1.onnxruntime推理

        推理部分C++版本麻烦很多,使用的onnxruntime1.9.0-gpu版本

1.1 python版本

image = cv2_imread(image_dir + file)
image = image[:, :, ::-1] #BGR2RGB
bbox = [35.75518  , 220.21573  , 227.99582  , 463.20563  ,   0.8576229]
#bbox = [111.28296, 98.003784, 288.46533, 342.31738, 0.8576229]
crop_img, crop_M = _crop_image(image, bbox, 1.5)
providers = ['CUDAExecutionProvider']
# providers = ['CPUExecutionProvider']
img_size = (height, width)

modelname = r"1001.onnx"
ort_session = onnxruntime.InferenceSession(modelname, providers=providers)  #
crop_img = crop_img.transpose((2, 0, 1))[np.newaxis, ...]
image = np.asarray(crop_img, dtype=np.float32)
ort_inputs = {ort_session.get_inputs()[0].name: image}
output = ort_session.run(None, ort_inputs)
print(output)

1.2 C++版本

#include <iostream>
#include <opencv2/opencv.hpp>
#include <onnxruntime_cxx_api.h>
#include <vector>
#include <cmath>
#include <filesystem>
 

      string modelpath = "./models/1001.onnx";
    std::wstring widestr = std::wstring(modelpath.begin(), modelpath.end());
    Ort::Env env_regress = Env(ORT_LOGGING_LEVEL_ERROR, "face_3dkps");
    Ort::Session* ort_session_regress = nullptr;
    SessionOptions sessionOptions_regress = SessionOptions();
    vector<char*> input_names;
    vector<char*> output_names;
    vector<vector<int64_t>> input_node_dims; // >=1 outputs
    vector<vector<int64_t>> output_node_dims; // >=1 outputs
    
    Mat image = cv2_imread("1.jpg");
    cv::cvtColor(image, image, cv::COLOR_BGR2RGB);
    vector<float> bbox;
    //1.jpg
    bbox.push_back(35.75518);
    bbox.push_back(220.21573);
    bbox.push_back(227.99582);
    bbox.push_back(463.20563);
    //111111.jpg
    //111.28296, 98.003784, 288.46533, 342.31738, 0.8576229
   /* bbox.push_back(111.28296);
    bbox.push_back(98.003784);
    bbox.push_back(288.46533);
    bbox.push_back(342.31738);*/
   
    // Example bbox, adjust as needed
    cv::Mat M;
    Mat crop_img = crop_image(image, bbox, 1.5f, M);
   
    sessionOptions_regress.SetGraphOptimizationLevel(ORT_ENABLE_BASIC);
    ort_session_regress = new Session(env_regress, widestr.c_str(), sessionOptions_regress);
    size_t numInputNodes = ort_session_regress->GetInputCount();
    size_t numOutputNodes = ort_session_regress->GetOutputCount();
    AllocatorWithDefaultOptions allocator;
    for (int i = 0; i < numInputNodes; i++)
    {
        input_names.push_back(ort_session_regress->GetInputName(i, allocator));
        //input_names.push_back(ort_session->GetInputName(i, allocator));
        Ort::TypeInfo input_type_info = ort_session_regress->GetInputTypeInfo(i);
        auto input_tensor_info = input_type_info.GetTensorTypeAndShapeInfo();
        auto input_dims = input_tensor_info.GetShape();
        input_node_dims.push_back(input_dims);
    }
    for (int i = 0; i < numOutputNodes; i++)
    {
        output_names.push_back(ort_session_regress->GetOutputName(i, allocator));

        //output_names.push_back(ort_session->GetOutputName(i, allocator));
        Ort::TypeInfo output_type_info = ort_session_regress->GetOutputTypeInfo(i);
        auto output_tensor_info = output_type_info.GetTensorTypeAndShapeInfo();
        auto output_dims = output_tensor_info.GetShape();
        output_node_dims.push_back(output_dims);
    }
    int inpHeight = input_node_dims[0][2];
    int inpWidth = input_node_dims[0][3];
    int nout = output_node_dims[0][2];
    array<int64_t, 4> input_shape_{ 1, 3,inpHeight, inpWidth };
    std::vector<float> input_image_;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值