HumanCarAnalysis.cpp 3.99 KB
#include "HumanCarAnalysis.h"

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

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

int HumanCarAnalysis::init(int devId){
    ACL_CALL(aclrtCreateContext(&ctx, devId), SY_SUCCESS, SY_FAILED);
    ACL_CALL(aclrtSetCurrentContext(ctx), SY_SUCCESS, SY_FAILED);

    hcp_param param;
    param.modelNames = "./models/hcp/hcp211008_310p.om";
    param.devId = devId;

    cout << "hcp_init start " << endl;
    int ret = hcp_init(&m_handle, param);
    if (ret != 0) {
        return -1;
	}

    cout << "hcp_init success " << endl;

    return SY_SUCCESS;
}

std::vector<HumanCarResult> HumanCarAnalysis::detect(vector<sy_img> vec_img){

    std::vector<HumanCarResult> vec_result;

    const int batchsize = vec_img.size();
    hcp_analysis_result * results = new hcp_analysis_result[batchsize];

    int ret = SY_FAILED;

    do
    {
        ret = aclrtSetCurrentContext(ctx);
        if (SY_SUCCESS != ret) {
            printf("aclrtSetCurrentContext failed!");
            break;
        }

        ret = hcp_batch(m_handle, vec_img.data(), batchsize, results);
        if (SY_SUCCESS != ret) {
            printf("hcp_batch failed!");
            break;
        }
        
        for(int batchIdx = 0;batchIdx<batchsize;batchIdx++){ 
            HumanCarResult one_result;
            one_result.head = results[batchIdx].res_objs[0].res_index;                   
            one_result.head_prob = results[batchIdx].res_objs[0].res_prob;
            one_result.eye = results[batchIdx].res_objs[1].res_index;                    
            one_result.eye_prob = results[batchIdx].res_objs[1].res_prob;
            one_result.mouth = results[batchIdx].res_objs[2].res_index;                  
            one_result.mouth_prob = results[batchIdx].res_objs[2].res_prob;
            one_result.weibo = results[batchIdx].res_objs[3].res_index;                  
            one_result.weibo_prob = results[batchIdx].res_objs[3].res_prob;
            one_result.up_wear = results[batchIdx].res_objs[4].res_index;                
            one_result.up_wear_prob = results[batchIdx].res_objs[4].res_prob;
            one_result.up_color = results[batchIdx].res_objs[5].res_index;               
            one_result.up_color_prob = results[batchIdx].res_objs[5].res_prob;
            one_result.up_tex = results[batchIdx].res_objs[6].res_index;                 
            one_result.up_tex_prob = results[batchIdx].res_objs[6].res_prob;
            one_result.bag = results[batchIdx].res_objs[7].res_index;                    
            one_result.bag_prob = results[batchIdx].res_objs[7].res_prob;
            one_result.sex = results[batchIdx].res_objs[8].res_index;                    
            one_result.sex_prob = results[batchIdx].res_objs[8].res_prob;
            one_result.age = results[batchIdx].res_objs[9].res_index;                    
            one_result.age_prob = results[batchIdx].res_objs[9].res_prob;
            one_result.carColor = results[batchIdx].res_objs[10].res_index;               
            one_result.carColor_prob = results[batchIdx].res_objs[10].res_prob;
            one_result.orient = results[batchIdx].res_objs[11].res_index;                 
            one_result.orient_prob = results[batchIdx].res_objs[11].res_prob;
            one_result.dasan = results[batchIdx].res_objs[12].res_index;                  
            one_result.dasan_prob = results[batchIdx].res_objs[12].res_prob;
            one_result.take = results[batchIdx].res_objs[13].res_index;                   
            one_result.take_prob = results[batchIdx].res_objs[13].res_prob;

            vec_result.push_back(one_result);
        }
    } while (0);

    if (results) {
        delete [] results;
    }

    return vec_result;
}

int HumanCarAnalysis::release() {

    ACL_CALL(aclrtSetCurrentContext(ctx), SY_SUCCESS, SY_FAILED);	

    if (m_handle) {
        hcp_release(&m_handle);
    }

    if(ctx){
        aclrtDestroyContext(ctx);
        ctx = nullptr;
    }

    return SY_SUCCESS;
}