QT调用Mxnet C++ 接口

#include
#include "bufferfile.h"
#include
#include
#include
#include
void getFeature(PredictorHandle &pred_hnd,
                std::vector &image_data,
                mx_uint image_size,
                float *feature)
{
    // // Set Input Image
    MXPredSetInput(pred_hnd, "data", image_data.data(), image_size);
    // // Do Predict Forward
    MXPredForward(pred_hnd);
    mx_uint output_index = 0;
    mx_uint *shape = 0;
    mx_uint shape_len;
    mx_uint size = 1;
    // Get Output Result

    MXPredGetOutputShape(pred_hnd, output_index, &shape, &shape_len);
    for (mx_uint i = 0; i < shape_len; ++i) { size *= shape[i]; }
    int64 start = cv::getTickCount();
    MXPredGetOutput(pred_hnd, output_index, feature, size);
    int64 end = cv::getTickCount();

    double secs = (end-start)/cv::getTickFrequency();
//    qDebug()<<"time: " < }

void GetImageFile(const std::string image_file,
                  mx_float* image_data,
                  const uint channels,
                  const cv::Size resize_size,
                  const mx_float* mean_data = nullptr) {
    // Read all kinds of file into a BGR color 3 channels image
    cv::Mat im_ori = cv::imread(image_file, cv::IMREAD_COLOR);

    if (im_ori.empty()) {
        std::cerr << "Can't open the image. Please check " << image_file << ". \n";
        assert(false);
    }

    cv::Mat im;

    cv::resize(im_ori, im, resize_size);//缩放图像
//    cv::imshow(image_file,im);
//    cv::waitKey(1);
//    float mean[3]={104.0,117.0,123.0};
    float mean[3]={0};
//        float mean[3]={128.0};
    int b = 0,g = 1, r = 2;
    int w = im.cols;
    int h = im.rows;
    for(int i=0; i < h; ++i){
        uchar* data = im.ptr(i);
        for(int j=0; j< w; ++j){
            image_data[2*w*h+i*w+j] = static_cast(*data++);//- mean[b];
            image_data[w*h+i*w+j]   = static_cast(*data++);//- mean[g];
            image_data[i*w+j]       = static_cast(*data++);//- mean[r];
        }
    }

}

int test()
{
    // model file path
    std::string json_file = "E:/GitHub/Python2/InsightFace/insightface/models/model-r100-ii/model-symbol.json";
    std::string param_file = "E:/GitHub/Python2/InsightFace/insightface/models/model-r100-ii/model-0000.params";

    // read model file
    BufferFile json_data(json_file);
    BufferFile param_data(param_file);

    if (json_data.GetLength() == 0 || param_data.GetLength() == 0)
    {
        return EXIT_FAILURE;
    }
    // mxnet parameters
    int dev_type = 2;  // 1: cpu, 2: gpu, we can change
    int dev_id = 0;   // arbitrary.
    mx_uint num_input_nodes = 1;  // 1 for feedforward
    const char *input_key[1] = { "data" };
    const char **input_keys = input_key;

    uint width = 112;
    uint height = 112;
    uint channels = 3;
    const mx_uint input_shape_indptr[2] = { 0, 4}; // column dim is 2
    const mx_uint input_shape_data[4] = { 1,channels,height,width};

    // global predicator handler
    PredictorHandle pred_hnd = nullptr;

    // create predictor
    MXPredCreate(static_cast(json_data.GetBuffer()),
                 static_cast(param_data.GetBuffer()),
                 static_cast(param_data.GetLength()),
                 dev_type,
                 dev_id,
                 num_input_nodes,
                 input_keys,
                 input_shape_indptr,
                 input_shape_data,
                 &pred_hnd);
    if (!pred_hnd)
    {
        std::cerr << "Failed to create predict handler" << std::endl;
        return EXIT_FAILURE;
    }

    mx_uint image_size = width * height * channels;

    std::string test_file1 = "D:/IMG/video/0.jpg";
    // // Read Image Data
    std::vector image_data1 = std::vector(image_size);


    GetImageFile(test_file1, image_data1.data(), channels, cv::Size(112, 112));

    float f1[512] ={0};
    getFeature(pred_hnd,image_data1,image_size,&f1[0]);
    std::string test_file2 = "D:/IMG/video/";
    for(int i =0;i<=7;i++)
    {
     std::string path;
     path.append(test_file2).append(std::to_string(i)).append(".jpg");
    float f2[512] ={0};

    std::vector image_data2 = std::vector(image_size);

    GetImageFile(path, image_data2.data(), channels, cv::Size(112, 112));

    getFeature(pred_hnd,image_data2,image_size,&f2[0]);

    float sim = cblas_sdot(512,&f1[0],1,&f2[0],1)/
            (sqrt(cblas_sdot(512,&f1[0],1,&f1[0],1))*sqrt(cblas_sdot(512,&f2[0],1,&f2[0],1)));

//    sim = 0.5+0.5*sim;//归一化
    qDebug()<


    }
    return EXIT_SUCCESS;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    test();
    return a.exec();
}
 

你可能感兴趣的:(机器学习,C++)