vpt.cpp 7.64 KB
#include "vpt.h"
#include "sy_errorinfo.h"
#include "sy_common.h"
#include "utools.h"
#include <time.h>
#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>
#include <cuda.h>

//model caffe2
#include "ga_vpt_predict_net_caffe2.h"
#include "ga_vpt_init_net_caffe2.h"

//model trt
#include "ga_vpt_predict_net.h"
#include "ga_vpt_init_net.h"
#include "vpt_fpn_plugin_factory.h"
#include "ga_trt_fpn_vpt_calibrator.h"

#include "authority.h"//授权文件
#include<boost/thread.hpp>

//视频结构化
/* 
#ifdef _MSC_VER
#define productSN "96BA6C1E806049BBB69CD273B08624B9" //视频结构化系列_图片接口_tensorrt_Windows
#else
#define productSN "8A0BFFECDED64D13BEFBE8C016F30905" //视频结构化系列_图片接口_tensorrt_Linux 
#endif
*/

//统一视频结构化图片接口产品id -- update 1112
//#ifdef _MSC_VER
//#define productSN "96BA6C1E806049BBB69CD273B08624B9" //windows 车型系列产品序列??图片接口
//#else
//#define productSN "8A0BFFECDED64D13BEFBE8C016F30905" //linux 车型系列产品序列??-图片接口
//#endif

#ifdef _MSC_VER
#define productSN "5A72239196F54B678D311E7FC11849DC" //windows 视频结构化-图片接口
#else
#define productSN "7CF8B4797F9E441686145BED07F662DE" //linux 视频结构化--图片接口
#endif
//#define THRESHOLD 0.6
//#define AUTHORIZATION
typedef struct objDetector {

	void* detect_handle;
	float threshold;
	VPT_FPNPluginFactory tensorrt_plugin_factory;

	int licence_status = -1;//授权相关参数
	int thrd_status = -1;	//授权相关参数
	boost::thread thrd;		//授权相关参数

	objDetector()
	{
		detect_handle = NULL;
		threshold = 0.6;
	}
}objDetector;


int vpt_init(void **handle, vpt_param param)
{
	int ret = SUCCESS;
#ifdef AUTHORIZATION
#ifdef _WIN32
	if (SUCCESS == (ret = sy_licence(productSN)))
#elif __linux__
	char* wtime = new char[15];
	memset(wtime, 0, 15);
	if (SUCCESS == (ret = sy_licence(productSN, &wtime)))
#endif
#else
	ret = sy_time_check(2021, 4);
	if (ret == SUCCESS)
#endif
	{
		cuInit(0);
		int device_count = 0;
		cuDeviceGetCount(&device_count);

		if (param.gpuid >= device_count)
		{
			printf("\nGPU_ID PARAM WRONG!\n");
			return GPUID_PARAM_ERROR;
		}

		objDetector * tools = new objDetector;

		ctools_init_params vpt_param;
		vpt_param.thres_ = param.threshold;
		vpt_param.log_level_ = 0;
		vpt_param.device_type_ = param.mode;
		vpt_param.device_id_ = param.gpuid;
		vpt_param.engine_type_ = param.engine;
		vpt_param.model_type_ = MODEL_FPN;

#ifdef _MSC_VER
		vpt_param.weight_file_ = NULL;
		vpt_param.net_file_ = NULL;
#else
		vpt_param.weight_file_ = NULL;
		vpt_param.net_file_ = NULL;
#endif

		
		vpt_param.data_process_str_ = param.preprocess_param;
		vpt_param.need_im_info_ = 1; // true
		
		if (vpt_param.engine_type_ == ENGINE_MCAFFE2)
		{
			//caffe2模型预处理参数
			vpt_param.data_process_str_ =
			"CopyData_CPU2GPU_U8;"
			"TypeConvert_U8_F32;"
			"ResizePad_F32_F32,test_size,720,test_max_size,1280,fpn_coarsest_stride,32,"
			"submean_b,103.94,submean_g,116.78,submean_r,123.68,"
			"variance_rev_b,0.017,variance_rev_g,0.017,variance_rev_r,0.017;"
			"NHWC2NCHW_F32"
			;

			vpt_param.weight_array_ = (unsigned char*)ga_vpt_init_net_caffe2;
			vpt_param.weight_array_len_ = ga_vpt_init_net_len_caffe2;
			vpt_param.net_array_ = (unsigned char*)ga_vpt_predict_net_caffe2;
			vpt_param.net_array_len_ = ga_vpt_predict_net_len_caffe2;
		}
		else if (vpt_param.engine_type_ == ENGINE_TENSORRT)
		{
			vpt_param.weight_array_ = (uint8_t*)ga_vpt_init_net;
			vpt_param.weight_array_len_ = ga_vpt_init_net_len;
			vpt_param.net_array_ = (uint8_t*)ga_vpt_predict_net;
			vpt_param.net_array_len_ = ga_vpt_predict_net_len;

			vpt_param.trt_serialize_file_ = param.serialize_file;// NULL;// "FPN_VPT_ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		
			//trt版本预处理参数
			param.preprocess_param =
			"CopyData_CPU2GPU_U8;"
			"TypeConvert_U8_F32;"
			"ResizeMaxPad_F32_F32,test_size,720,test_max_size,1280,max_height,736,max_width,1280,"
			"submean_b,103.94,submean_g,116.78,submean_r,123.68,"
			"variance_rev_b,0.017,variance_rev_g,0.017,variance_rev_r,0.017;"
			"NHWC2NCHW_F32"
			;

      memset(vpt_param.tensorrt_param_str_, 0, sizeof(vpt_param.tensorrt_param_str_));

			int batch_size = param.max_batch;
			std::string g_data_mode = "FP32";
			bool g_is_create_calibrator = false;

			sprintf(vpt_param.tensorrt_param_str_, "max_batchsize %d,"
				"data_mode %s,"
				"is_create_calibrator %d,"
				"input_names data im_info,"
				"output_names cls_prob bbox_pred_final rois_count_each_img",
				batch_size, g_data_mode.c_str(), g_is_create_calibrator);

			vpt_param.tensorrt_calibrator_file_ = NULL;// "trt_fpn_vpt_calibrator";
			vpt_param.tensorrt_calibrator_array_len_ = ga_trt_fpn_vpt_calibrator_len;// "trt_fpn_vpt_calibrator";
			vpt_param.tensorrt_calibrator_array_ = (unsigned char*)ga_trt_fpn_vpt_calibrator;// "trt_fpn_vpt_calibrator";

			vpt_param.tensorrt_plugin_factory_ptr_ = &(tools->tensorrt_plugin_factory);
		}
		
		tools->threshold = param.threshold;
	
		int flag = ctools_init(&tools->detect_handle, &vpt_param);

		if (SUCCESS != flag)
		{
			printf("VPT_Init(ERROR): Init failed\n");
			*handle = NULL;
		}
		else
			*handle = (void*)tools;

		return flag;
	}
	else
	{
		return AUTHOR_ERROR;
	}
#ifdef AUTHORIZATION
#ifdef __linux__
	if (wtime)
	{
		delete[] wtime;
		wtime = NULL;
}
#endif
#endif
}


int vpt_process(void * handle, sy_img img, vpt_result *result)
{
	if (NULL == handle)
	{
		printf("vpt_det(error): handle == null.\n");
		return HANDLE_NULL_ERROR;
	}
	if (img.data_ == NULL || img.w_ == 0 || img.h_ == 0)
	{
		printf("vpt_det(error): imgdata == null or width == 0 or height == 0.\n");
		return PARAMS_NULL_ERROR;
	}

	return vpt_batch(handle, &img, 1, &result);
}


int vpt_batch(void * handle, sy_img *batch_img, int batchsize, vpt_result **result)
{
	if (NULL == handle)
	{
		printf("vpt_det(error): handle == null.\n");
		return HANDLE_NULL_ERROR;
	}
	if (batchsize == 0)
	{
		printf("vpt_det(error): batch_size == 0.\n");
		return PARAMS_NULL_ERROR;
	}
	for (int b = 0; b < batchsize; b++)
	{
		if (batch_img[b].data_ == NULL || batch_img[b].w_ == 0 || batch_img[b].h_ == 0)
		{
			printf("vpt_det(error): imgdata == null or width == 0 or height == 0.\n");
			return PARAMS_NULL_ERROR;
		}
	}
	

	objDetector * m_handle = (objDetector *)handle;

	ctools_result *detresult;
	int res_status = ctools_process(m_handle->detect_handle, batch_img, batchsize, &detresult);

	for (int b = 0; b < batchsize; b++)
	{
		ctools_result &cur_result = detresult[b];

		//(*result)[b].obj_count_ = cur_result.obj_count_ > MAX_DET_COUNT ? MAX_DET_COUNT : cur_result.obj_count_;

		int res_count = 0;
		for (int c = 0; c < cur_result.obj_count_ && c < MAX_DET_COUNT; c++)
		{
			float score = cur_result.obj_results_[c].data_[1];

			if (score >= m_handle->threshold)  //更改为left top right bottom score index
			{
				(*result)[b].obj_results_[res_count].obj_id = c;
				(*result)[b].obj_results_[res_count].obj_rect.left_ = cur_result.obj_results_[c].data_[2];
				(*result)[b].obj_results_[res_count].obj_rect.top_ = cur_result.obj_results_[c].data_[3];
				(*result)[b].obj_results_[res_count].obj_rect.width_ = cur_result.obj_results_[c].data_[4] - cur_result.obj_results_[c].data_[2];
				(*result)[b].obj_results_[res_count].obj_rect.height_ = cur_result.obj_results_[c].data_[5] - cur_result.obj_results_[c].data_[3];
				(*result)[b].obj_results_[res_count].obj_index = cur_result.obj_results_[c].data_[0] - 1;
				(*result)[b].obj_results_[res_count].obj_score = cur_result.obj_results_[c].data_[1];
				res_count++;
			}
		}

		(*result)[b].obj_count_ = res_count;
	}



	return SUCCESS;
}


void vpt_release(void **handle)
{
	if (*handle)
	{
		objDetector * m_handle = (objDetector *)*handle;
		ctools_release(&m_handle->detect_handle);
		m_handle = NULL;
	}
}



const char * vpt_get_version()
{
	return "vpt_det_v0.0.0.190327";
}