motor_rainshed_cls.cpp 4.21 KB
#include "motor_rainshed_cls.h"
#include "sy_errorinfo.h"
#include "cnn_cls.h"
#include "dvpp_processx.h"
#include <time.h>
#include <sys/time.h>
#include "stream_data.h"


using namespace atlas_utils;
using namespace std;

struct Resource {
    aclrtContext ctx;
    aclrtStream stream;
};

typedef struct Tools {
    Resource src;
    CnnCls*  mRainCls;
    DvppProcessx* dvpp;
}Tools;

int mrc_init(void** handle, mrc_param param) {

    int ret = SY_SUCCESS;
    Tools* tools = new Tools;
    // init resource
    // ACL_CALL(aclInit(nullptr), ACL_SUCCESS, SY_FAILED);
    // ACL_CALL(aclrtSetDevice(param.devId), ACL_SUCCESS, SY_FAILED);
    // ACL_CALL(aclrtCreateContext(&tools->src.ctx, param.devId), ACL_SUCCESS, SY_FAILED);
    ACL_CALL(aclrtCreateStream(&tools->src.stream), ACL_SUCCESS, SY_FAILED);

    // motor rainshed classfication init
    tools->mRainCls = new CnnCls();
    tools->mRainCls->config.confThr = param.thresld;
    ret = tools->mRainCls->Init(param.modelNames);
    if (ret != SY_SUCCESS) {
        delete tools->mRainCls;
        tools->mRainCls = nullptr;
        return SY_FAILED;
    }

    tools->dvpp = new DvppProcessx();
    tools->dvpp->InitResource(tools->src.stream);

    *handle = tools;
    
    return SY_SUCCESS;
}


// double msecond() {
//     struct timeval tv;
//     gettimeofday(&tv, 0);
//     return (tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0);
// }

int mrc_batch(void * handle, sy_img *img_data_array, int batch_size, mrc_result *result) {
    Tools* tools = (Tools*) handle;

    int inputW = tools->mRainCls->GetInputWidth();
    int inputH = tools->mRainCls->GetInputHeight();
    
    // printf("debug inputw:%d,inputh:%d\n",inputW,inputH);

    for (int i = 0; i < batch_size; i++) {
        if (img_data_array[i].data_ == NULL || img_data_array[i].w_ == 0 || img_data_array[i].h_ == 0) {
            ERROR_LOG("mRainCls get null input ptr!");
	        return SY_FAILED;
        }
        ImageData resizeImg, src;
        //debug========================================================================
        // src.width = img_data_array[i].w_;
        // src.height = img_data_array[i].h_;
        // src.alignWidth = ((src.width + 127) & ~(127));
        // src.alignHeight = ((src.height + 15) & ~(15));
        // src.size = src.alignWidth * src.alignHeight * 1.5;
        // src.data.reset((uint8_t*)img_data_array[i].data_, [](uint8_t* p) { acldvppFree((void *)p); });
        //debug end====================================================================
        // Utils::CopysyImageDataToDvpp(src, img_data_array[i]);
        ACL_CALL(Utils::CopysyImageDataToDvppV2(src, img_data_array[i]), SY_SUCCESS, SY_FAILED);
        ACL_CALL(tools->dvpp->CropAndPaste(resizeImg, src, inputW, inputH), SY_SUCCESS, SY_FAILED);
        // forward
        //double t1, t2;
        //t1 = msecond();
        int ret = tools->mRainCls->Inference(resizeImg);
        if (ret != SY_SUCCESS) {
            return SY_MODEL_FORWARD_ERROR;
        }
        //t2 = msecond();
        //printf("debug infer time: %.2f\n", t2 - t1);

        vector<float> mRainClsRes;
        ret = tools->mRainCls->PostProcess(mRainClsRes);
        if (ret != SY_SUCCESS) {
            return SY_MODEL_GETRESULT_ERROR;
        }
        result[i].index = mRainClsRes[0];
        result[i].score = mRainClsRes[1];
        // printf("debug index:%d,confidence:%f\n",result[i].index,result[i].score);
        // for (auto res : mRainClsRes) {
        //     printf("debug info: %f\n",res);
        // }
        
    }

    return SY_SUCCESS;
}

void mrc_release(void **handle) {
    Tools* tools = (Tools*) handle;
    // printf("debug line:%d\n",__LINE__);
    if (tools) {
        if (tools->mRainCls) {
            // delete tools->mRainCls;
            tools->mRainCls = nullptr;
        }
        // printf("debug line:%d\n",__LINE__);
        if (tools->dvpp) {
            // delete tools->dvpp;
            tools->dvpp = nullptr;
        }
        //printf("debug line:%d\n",__LINE__);
        // aclFinalize();
        aclrtDestroyStream(&tools->src.stream);
        // delete tools;
        tools = NULL;
        //printf("debug line:%d\n",__LINE__);
    }
}

const char * mrc_get_version() {
    return "motorrc_arm_vdec_310p_v0.0.1.20230921_without_timelimit";
}