VehicleAnalysis.cpp 5.25 KB
#include "VehicleAnalysis.h"

#include "VehicleAnalysisDevice.h"


VehicleAnalysis::VehicleAnalysis() {
    cout << va_get_version() << endl;
}

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

int VehicleAnalysis::init(VehicleAnalysisParam tParam) {

    va_param param;
	param.vehicle_detect_config= SY_CONFIG_OPEN;  //1.开启车检测 SY_CONFIG_CLOSE  SY_CONFIG_OPEN
    param.vehicle_recg_config= SY_CONFIG_OPEN;     //4.开启车型识别
	param.vehicle_recg_supplement_config= SY_CONFIG_OPEN;     //4.开启车型识别补充识别
    param.vehicle_plate_det_recg_config= SY_CONFIG_OPEN; //5.开启车牌检测识别	
    param.vehicle_pendant_det_config= SY_CONFIG_OPEN; //6.开启车属性检测识别
    param.vehicle_motor_tricycle_analysis_config= SY_CONFIG_OPEN;  //8.摩托车分析	
    param.vehicle_manned_config= SY_CONFIG_OPEN;  //8.开启载人分析	
    param.vehicle_illegal_config= SY_CONFIG_OPEN;  //7.开启车违规
	param.vehicle_color_config= SY_CONFIG_OPEN;  //3.开启车颜色识别
	param.vehicle_feature_config= SY_CONFIG_OPEN;  //8.开启车辆特征提取
	
	param.gpuid=tParam.devId;
		
	//车检测参数	
	//param.vehicle_det_param.process_min_l = 720;// 720;
	//param.vehicle_det_param.process_max_l = 1280;//1280;	
	//param.vehicle_det_param.thresld=0.3;
	//param.min_obj_size=200;  //最小目标	
	param.vehicle_det_thresld=0.4;
	
	//车牌检测参数
	//param.vehicle_plate_det_param.process_min_l=320;
	//param.vehicle_plate_det_param.process_max_l=320;	
	//param.vehicle_plate_det_param.thresld=0.4;
	param.vehicle_plate_det_thresld=0.5;
	
	//车属性检测参数
	//param.vehicle_attribute_det_param.process_min_l=360;
	//param.vehicle_attribute_det_param.process_max_l=640;	
	//param.vehicle_attribute_det_param.thresld=0.3;
	param.vehicle_attribute_det_thresld=0.5;
	
	//车logo检测参数
	//param.vehicle_logo_det_param.process_min_l=512;
	//param.vehicle_logo_det_param.process_max_l=512;	
	//param.vehicle_logo_det_param.thresld=0.1;
	param.vehicle_logo_det_thresld=0.1;	
	
	//车颜色阈值
	param.vc_thresld = 0.5;
	
    string dbPath = tParam.sdk_root + "/models/vehicle_analysis/db/vr_h0725x210605_r191230.db";
    string models_path = tParam.sdk_root + "/models/vehicle_analysis/";
	//车型参数
	param.dbPath= (char*)dbPath.data();	
	param.models_Path= (char*)models_path.data();	//所有模型的地址

    LOG_INFO("va_init start");
    // 内部有 ctx
    int ret = va_init(&m_handle, param);
    if (ret != 0) {
        return -1;
	}

    LOG_INFO("va_init success");
    return 0;
}

vector<AnalysisResult> VehicleAnalysis::detect(vector<sy_img> vec_img) {
			
    int batch_size = vec_img.size();
    
    va_result *result=new va_result[batch_size];

    LOG_INFO("va_batch start");
    
    int ret = va_batch(m_handle, vec_img.data(), batch_size, result);

    auto vec_result = va_result2AnalysisResult(result, batch_size);

    delete[] result;
    result = nullptr;

    LOG_INFO("va_batch end:{}", ret);

    return vec_result;  
}

void VehicleAnalysis::release(){
    if (m_handle) {
        va_release(&m_handle);
    }
}

vector<AnalysisResult> VehicleAnalysis::va_result2AnalysisResult(va_result* result, int batchsize) {

    vector<AnalysisResult> vec_result;

    for (int b = 0; b < batchsize; b++)
    {
        vector<VehicleInfo> vec_info;
        for(int c=0;c<result[b].count;c++)
        {
            vehicle_info result_info = result[b].info[c];

            VehicleInfo info;
            info.vehicle_detect_res         = result_info.vehicle_detect_res        ;	 
            info.vehicle_win_detect_res     = result_info.vehicle_win_detect_res    ; 	
            info.vehicle_body_detect_res    = result_info.vehicle_body_detect_res   ;	
            info.vehicle_color_res          = result_info.vehicle_color_res         ;
            info.vehicle_recg_res           = result_info.vehicle_recg_res          ;  
            info.vehicle_plate_det_recg_res = result_info.vehicle_plate_det_recg_res;	 
            info.vehicle_illegal_det_res    = result_info.vehicle_illegal_det_res   ; 	
            info.vehicle_fea_res            = result_info.vehicle_fea_res           ;   	
            info.mta_res                    = result_info.mta_res                   ;   
            info.manned_res	 	            = result_info.manned_res	 	        ;  
            info.type                       = result_info.type                      ;         
            info.vpt_type                   = result_info.vpt_type                  ;


            auto pendant_result = result_info.vehicle_pendant_det_res;
            for (size_t j = 0; j < pendant_result.count; j++)
            {
                auto pendant_det_info = pendant_result.vpd_res[j];

                pendant_info one_pendant_info;
                one_pendant_info.rect = pendant_det_info.rect;
                one_pendant_info.index = pendant_det_info.index;
                one_pendant_info.confidence = pendant_det_info.confidence;
                one_pendant_info.driver_copilot_info = pendant_det_info.driver_copilot_info;
                info.vehicle_pendant_det_res.push_back(one_pendant_info);
            }

            vec_info.push_back(info);
        }

        AnalysisResult one_result;
        one_result.info = vec_info;
        vec_result.push_back(one_result);
    }

    return vec_result;
}