MotorPhoneAnalysis.cpp 2.38 KB
#include "MotorPhoneAnalysis.h"
#include "motor_phone_det.h"

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

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

int MotorPhoneAnalysis::init(int devId, std::string sdk_root){
    ACL_CALL(aclrtCreateContext(&ctx, devId), SY_SUCCESS, SY_FAILED);

    std::string model_path = sdk_root + "/models/motor_phone/motor_phone1127_310p.om";

    motor_phone_param param;
    param.modelNames = (char*)model_path.data();
    param.thresld = 0.25;
    param.devId = devId;

    LOG_INFO("motor_phone_init start");
    int ret = motor_phone_init(&m_handle, param);
    if (ret != 0) {
        return -1;
	}

    LOG_INFO("motor_phone_init success");

    return SY_SUCCESS;
}

vector<MotorPhoneResult> MotorPhoneAnalysis::detect(vector<sy_img> vec_img){

    vector<MotorPhoneResult> vec_result;

    const int batchsize = vec_img.size();

    vector<motor_phone_result> results;
    results.resize(batchsize);

    int ret = SY_FAILED;

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

        ret = motor_phone_process_batch(m_handle, vec_img.data(), batchsize, results.data());
        if (SY_SUCCESS != ret) {
            printf("motor_phone_process_batch failed!");
            break;
        }
        
        for(int batchIdx = 0; batchIdx < batchsize; batchIdx ++){
            MotorPhoneResult one_result;
            one_result.phoning = -1;
            one_result.prob = 0;
            for (int i = 0; i < results[batchIdx].objcount; i++) {
                printf("  %d:%.2f \n", results[batchIdx].objinfo[i].index,results[batchIdx].objinfo[i].confidence);
                if (results[batchIdx].objinfo[i].index == 1) {
                    // 骑车打电话
                    one_result.phoning = results[batchIdx].objinfo[i].index;
                    one_result.prob = results[batchIdx].objinfo[i].confidence;
                    break;
                }
            }

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

    return vec_result;
}

int MotorPhoneAnalysis::release() {

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

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

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

    return SY_SUCCESS;
}