MSRegionSurveilanceVpt.cpp 2.6 KB
#include "MSRegionSurveilanceVpt.h"
#include "vpt.h"
#include "sy_errorinfo.h"
#include <cuda.h>
#include <cuda_runtime.h>


int IRegionSurveillanceVpt::init(const rs_param & param)
{
	vpt_param vptparam;
	vptparam.mode = DEVICE_GPU;
	vptparam.gpuid = 0;
	vptparam.threshold = 0.6;
	vptparam.engine = ENGINE_TENSORRT;
	vptparam.preprocess_param =
		"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"
		;
	vptparam.serialize_file = "FPN_VPT";
	vptparam.max_batch = 20;
	
	if (vpt_init(&vpt_handle, vptparam) != 0)
	{
		printf("vpt_init failed!\n");
		return VPT_DET_INIT_ERROR;
	}
	printf("finish init vpt\n");
	AddTaskTracker(0, 1, 1);
	return 0;
}






int IRegionSurveillanceVpt::detect(const sy_img &img_data, VPT_Result & vptResult)
{
	vector<int> deleteObjectID;
	vpt_result * vptresult = new vpt_result{};
	vptresult->obj_count_ = 0;
	vptresult->obj_results_ = new vpt_obj_result[MAX_DET_COUNT];
	sy_img batch_img_gpu;
	unsigned char * data = NULL;
	cudaMalloc(&data, img_data.w_ *img_data.h_*img_data.c_ * sizeof(unsigned char));
	cudaMemcpy(data, img_data.data_, img_data.w_ *img_data.h_ * img_data.c_ * sizeof(unsigned char), cudaMemcpyHostToDevice);
	batch_img_gpu.set_data(img_data.w_, img_data.h_, img_data.c_, data);
	auto ret = vpt_batch(vpt_handle, &batch_img_gpu, 1, &vptresult);
	cudaFree(data);
	std::vector<std::vector<float>> detectResult;
	for (int c = 0; c < vptresult->obj_count_&& c < MAX_OBJ_COUNT; c++)
	{
		vector <float> obj;
		if (vptresult->obj_results_[c].obj_index == 0)
		{
			obj.push_back(vptresult->obj_results_[c].obj_rect.left_);
			obj.push_back(vptresult->obj_results_[c].obj_rect.top_);
			obj.push_back(vptresult->obj_results_[c].obj_rect.left_ + vptresult->obj_results_[c].obj_rect.width_);
			obj.push_back(vptresult->obj_results_[c].obj_rect.top_ + vptresult->obj_results_[c].obj_rect.height_);
			obj.push_back(vptresult->obj_results_[c].obj_score);
			obj.push_back(vptresult->obj_results_[c].obj_index);
			detectResult.push_back(obj);
		}
	
	}

	int objCount = taskTrackers.tracker.update(/*tools->param.w*/img_data.w_* taskTrackers.ratioWidth, /*tools->param.h*/img_data.h_* taskTrackers.ratioHeight, true, detectResult, vptResult.obj, deleteObjectID);
	vptResult.objCount = objCount;
	delete[] vptresult->obj_results_;
	delete vptresult;
	return ret;
}




void IRegionSurveillanceVpt::release()
{

	//1.VPT RELEASE
	if (vpt_handle)
	{
		vpt_release(&vpt_handle);
		vpt_handle = NULL;
	}
	
}