facelandmarks.cpp 3.17 KB
#include "facelandmarks.h"


// FaceLandmarks::~FaceLandmarks(){
//     pfld_interpreter->releaseModel();
//     pfld_interpreter->releaseSession(session);
// }

bool FaceLandmarks::init_model(string model_path){
    pfld_interpreter = unique_ptr<MNN::Interpreter>(MNN::Interpreter::createFromFile(model_path.c_str()));
    if(nullptr==pfld_interpreter){
        return false;
    }
    //创建session
    MNN::ScheduleConfig schedule_config;
    schedule_config.type = forward_type;
    schedule_config.numThread = num_thread;
    MNN::BackendConfig backend_config;
    backend_config.memory    = MNN::BackendConfig::Memory_Normal;
    backend_config.power     = MNN::BackendConfig::Power_Normal;
    backend_config.precision = MNN::BackendConfig::Precision_Normal;
    schedule_config.backendConfig = &backend_config;
    session = pfld_interpreter->createSession(schedule_config);
    input_tensor = pfld_interpreter->getSessionInput(session,NULL);
    pfld_interpreter->resizeTensor(input_tensor,{1,3,112,112});
    pfld_interpreter->resizeSession(session);

    //数据预处理
    MNN::CV::ImageProcess::Config image_config;
    ::memcpy(image_config.normal,normal,sizeof(normal));
    image_config.sourceFormat = MNN::CV::BGR;
    image_config.destFormat = MNN::CV::BGR;
    
    pretreat = shared_ptr<MNN::CV::ImageProcess>(MNN::CV::ImageProcess::create(image_config));
    // pretreat->setMatrix(transforms);

    return true;
}

vector<vector<float>> FaceLandmarks::inference(string image_path){
    Mat image = cv::imread(image_path);
    vector<vector<float>> landmarks;
    int width = image.cols;
    int height = image.rows;
    Mat resize_image;
    cv::resize(image,resize_image,Size(112,112));
    float ws = float(width)/float(112.0);
    float hs = float(height)/float(112.0);

    pretreat->convert(resize_image.data,112,112,0,input_tensor);

    pfld_interpreter->runSession(session);

    auto output_landmark = pfld_interpreter->getSessionOutput(session, NULL);
    MNN::Tensor landmark_tensor(output_landmark, output_landmark->getDimensionType());
    output_landmark->copyToHostTensor(&landmark_tensor);
    float* result = landmark_tensor.host<float>();
    for (int i = 0; i < 106; ++i) {
        vector<float> curr_pt={result[2 * i + 0] * ws,result[2 * i + 1] * hs};
        landmarks.push_back(curr_pt);
    }
    return landmarks;
}

vector<vector<float>> FaceLandmarks::inference(Mat image){
    vector<vector<float>> landmarks;
    int width = image.cols;
    int height = image.rows;
    Mat resize_image;
    cv::resize(image,resize_image,Size(112,112));
    float ws = float(width)/float(112.0);
    float hs = float(height)/float(112.0);

    pretreat->convert(resize_image.data,112,112,0,input_tensor);

    pfld_interpreter->runSession(session);

    auto output_landmark = pfld_interpreter->getSessionOutput(session, NULL);
    MNN::Tensor landmark_tensor(output_landmark, output_landmark->getDimensionType());
    output_landmark->copyToHostTensor(&landmark_tensor);
    float* result = landmark_tensor.host<float>();
    for (int i = 0; i < 106; ++i) {
        vector<float> curr_pt={result[2 * i + 0] * ws,result[2 * i + 1] * hs};
        landmarks.push_back(curr_pt);
    }
    return landmarks;
}