VehiclePlate.cpp 2.76 KB
#include "VehiclePlate.h"
#include <cuda_runtime.h>

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


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

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

int VehiclePlate::init(int gpuid, char* auth_license)
{
	vp_detect_param param;
	param.gpuid = gpuid;
	param.thresld = 0.4;
	param.max_batch = 20;
	param.serialize_file = "./serialize_file/VPDetect";
	param.auth_license = "sy_va_sub_sdk_2023";

	vpr_param rparam;
	rparam.gpuid = gpuid;
	rparam.max_batch = 20;
	rparam.serialize_file = "./serialize_file/VPRecg";
	param.auth_license = "sy_va_sub_sdk_2023";

    int ret = vpdr_init(&handle, param, rparam);
	if (ret != 0){
		LOG_ERROR("vpdr_init Failed!");
		return FAILED;
	}

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

int VehiclePlate::process(sy_img * batch_img, int batchsize, vehicle_plate_result *&result)
{
    sy_img cpu_batch_img[batchsize];
	for (int i = 0; i < batchsize; i++)
	{
		if (batch_img[i].data_ == NULL) {
			LOG_ERROR("data null ");
			return FAILED;
		}
		// cpu_batch_img[i].data_ = (unsigned char *)malloc(batch_img[i].w_ *batch_img[i].h_ * batch_img[i].c_ * sizeof(unsigned char));
		// cudaMemcpy(cpu_batch_img[i].data_, batch_img[i].data_, batch_img[i].w_ *batch_img[i].h_ * batch_img[i].c_ * sizeof(unsigned char), cudaMemcpyDeviceToHost);
		// cpu_batch_img[i].w_ = batch_img[i].w_;
		// cpu_batch_img[i].h_ = batch_img[i].h_; 
		// cpu_batch_img[i].c_ = batch_img[i].c_;
	}

	vehicle_plate_results * all_result = new vehicle_plate_results[batchsize] {};
	for (int i = 0; i<batchsize; i++) {
		for (int j = 0; j < MAXPLATECOUNT; j++) {
			memset(&all_result[i].vehicle_plate_infos[j].rect, -1, sizeof(sy_rect));
		}
	}

	int index_count = vpdr_batch(handle, batch_img, batchsize, all_result);
	cudaDeviceSynchronize();

	for (int b = 0; b < batchsize; b++)
	{ 
		double max_score = 0.0;
		int best_index = 0;
		for (int j = 0; j < MAXPLATECOUNT; j++)
		{
			if (all_result[b].vehicle_plate_infos[j].rect.left_ == -1)
			{
				continue;
			}

			if (all_result[b].vehicle_plate_infos[j].detect_score > max_score)
			{
				max_score = all_result[b].vehicle_plate_infos[j].detect_score;
				best_index = j;
			}
		}

		memcpy(&result[b], &all_result[b].vehicle_plate_infos[best_index], sizeof(vehicle_plate_result));
	}

	// for (int i = 0; i < batchsize; i++) {
	// 	if(cpu_batch_img[i].data_ != nullptr) {
	// 		free(cpu_batch_img[i].data_);
	// 		cpu_batch_img[i].data_ = nullptr;
	// 	}
	// }

	if (all_result != NULL)
		delete [] all_result;

	return SUCCESS;
}

int VehiclePlate::release()
{
	if (handle) {
		vpdr_release(&handle);
		handle = nullptr;
	}
	
	LOG_INFO("release");
	return SUCCESS;
}