HumanFeature.cpp 2.4 KB
#include "HumanFeature.h"
#include "humanreid.h"


HumanFeature::HumanFeature(/* args */)
{
}

HumanFeature::~HumanFeature()
{
    release();
}

int HumanFeature::init(int dev_id, string model_dir){

    string model_path = model_dir + "/models/human_reid2/humanreid_310p.om" ;

    LOG_INFO("hp 版本:{}  模型路径:{}", humanreid_get_version(), model_path);

    humanreid_param param;
    char modelNames[100];
    strcpy(modelNames, model_path.c_str());
    param.modelNames = modelNames;
    param.devId = dev_id;

    m_devId = param.devId;
    ACL_CALL(aclrtSetDevice(m_devId), ACL_SUCCESS, -1);
    ACL_CALL(aclrtCreateContext(&m_algorthim_ctx, m_devId), ACL_SUCCESS, -1);

    int ret = humanreid_init(&m_handle, param);
    if(ret != 0){
        LOG_ERROR("humanreid_init error.");
        return -1;
    }

    return 0;
}

vector<HFResult> HumanFeature::process(vector<DeviceMemory*> vec_gpuMem) {

    int batchsize = vec_gpuMem.size();

    vector<sy_img> batch_img;
    for (int i = 0; i < vec_gpuMem.size(); i++) {
        DeviceMemory* mem = vec_gpuMem[i];

        sy_img img;
        img.w_ = mem->getWidth();     
        img.h_ = mem->getHeight();
        img.c_ = mem->getChannel();   
        img.data_ = mem->getMem();
        batch_img.push_back(img);
    }

    float ** feature=new float*[batchsize];
    for(int b=0;b<batchsize;b++)
    {
        feature[b]=new float[HUMANREID_FEATURESIZE];
    }

    int ret = SY_SUCCESS;
    do
    {
        ret = aclrtSetCurrentContext(m_algorthim_ctx);

        ret = humanreid_batch(m_handle, batch_img.data(), batchsize, feature);
        if (SY_SUCCESS != ret) {
            printf("humanFeature process failed!");
            ret = SY_FAILED;
            break;
        }
    } while (0);

    vector<HFResult> vec_hf_res;
    
    for(int batchIdx = 0;batchIdx<batchsize;batchIdx++){ 
        //debug
        HFResult res;
        memcpy(res.feature, feature[batchIdx], sizeof(float) * HUMANREID_FEATURESIZE);
        vec_hf_res.push_back(res);
    
        if (feature[batchIdx]!=NULL)
            delete[] feature[batchIdx];
    }

    if (feature!=NULL) {
        delete feature;
        feature = nullptr;
    }

    return vec_hf_res;
}

void HumanFeature::release(){
    if (m_handle){
        humanreid_release(&m_handle);
        m_handle = NULL;
    }
    if(m_algorthim_ctx){
        aclrtDestroyContext(m_algorthim_ctx);
        m_algorthim_ctx = nullptr;
    }
}