VehicleFeature.cpp 3.63 KB
#include "VehicleFeature.h"
#include <iostream>
#include "sy_errorinfo.h"

#include "../../FFNvDecoder/logger.hpp"

using namespace std;


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

VehicleFeature::~VehicleFeature()
{
    LOG_INFO("~VehicleFeature");
}

int VehicleFeature::init_vpd(char*dbpath, int gpuid, char* auth_license)
{
	vehicle_pendant_det_param params;
	params.gpuid = gpuid;
	params.max_batch = 20;
	params.serialize_file = "./serialize_file/VPD";
	params.thresld = 0.6;
	params.auth_license = auth_license;
	if (vpd2_init(&vpd_handle, params) !=0) {
		LOG_ERROR("vpd2_init failed!");
        return FAILED;
    }

	LOG_INFO("vpd2_init success! gpu_id: {}", gpuid);
    return SUCCESS;
}

int VehicleFeature::init_vf(char*dbpath, int gpuid, char* auth_license)
{
    vf_param params;
    params.gpuid = gpuid;
    params.mode = DEVICE_GPU;
	params.engine = ENGINE_TENSORRT;
	params.max_batch = 20;
	params.serialize_file = "./serialize_file/VF";
	params.auth_license = auth_license;
    if (vf_init(&vf_handle, params) !=0) {
        LOG_ERROR("vf_init failed!");
        return FAILED;
    }

	LOG_INFO("vf_init success! gpu_id: {}", gpuid);
    return SUCCESS;
}

int VehicleFeature::process_vpd(sy_img * batch_img, int batch_size, vpd2_result*& result)
{
    for (int i = 0; i < batch_size; i++)
    {
        if (batch_img[i].data_ == NULL) {
			LOG_ERROR("data null!");
			return FAILED;
		}
    }
	vpd2_process(vpd_handle, batch_img, batch_size, result);
 
 	for (int b = 0; b < batch_size; b++)
	{
		for (int c = 0; c < result[b].count; c++)
		{
			sy_rect rect = result[b].vpd_res[c].rect;
			RectboundCheck(batch_img[b].w_, batch_img[b].h_, rect.left_, rect.top_, rect.width_, rect.height_);
		}
	}
 
    return SUCCESS;
}

int VehicleFeature::process_vf( sy_img * batch_img, int batch_size, int8**& result)
{
    for (int i = 0; i < batch_size; i++)
    {
        if (batch_img[i].data_ == NULL){
			LOG_ERROR("data null!");
		}
    }

	int **fea = new int*[batch_size];
	for (int i = 0; i < batch_size; i++)
	{
		fea[i] = new int[FEATURESIZE];
	}

    vf_feature_batch(vf_handle, batch_img, batch_size, fea);

	for (int i = 0; i < batch_size; i++)
	{
		for (int f = 0; f < FEATURESIZE; f++)
		{
			result[i][f] = (unsigned char)fea[i][f];
		}
	}
	
	if (fea)
	{
		for (int b = 0; b < batch_size; b++)
		{
			if(fea[b])
			{
				delete[] fea[b];
				fea[b] = NULL;
			}
		}

		delete[] fea;
		fea = NULL;
	}

    return SUCCESS;
}

int VehicleFeature::release_vpd()
{
	if (vpd_handle){
		vpd2_release(&vpd_handle);
		vpd_handle = nullptr;
	}
	
	LOG_INFO("release_vpd");
	return SUCCESS;
}


int VehicleFeature::release_vf()
{
	if (vf_handle) {
		vf_release(&vf_handle);
		vf_handle = nullptr;
	}
    
	LOG_INFO("release_vf");
    return SUCCESS;
}

void VehicleFeature::RectboundCheck(int Width, int Height, int &left, int &top, int &width, int &height)
{
#ifdef _DEBUG
	printf("img: Width = %d Height = %d\n", Width, Height);
	printf("Src: width = %d height = %d, x = %d, y =%d\n", width, height, left, top);
#endif
	if (left <= 0)
		left = 0;

	if (left >= (Width - 1))
		left = Width - 2;

	if (top < 0)
		top = 0;
	if (top >= (Height - 1))
		top = Height - 2;

	if ((left + width) >= Width)
		width = Width - left - 1;
	if (width <= 0)
		width = 0;

	if ((top + height) >= Height)
		height = Height - 1 - top;
	if (height <= 0)
		height = 0;

#ifdef _DEBUG
	printf("img: Width = %d Height = %d\n", Width, Height);
	printf("Src: width = %d height = %d, x = %d, y =%d\n", width, height, left, top);
#endif
}