human_car_parsing.cpp 9.21 KB
#include "human_car_parsing.h"
#include <stdio.h>
#include <string>
#include <string.h>
#include <time.h>
#include "utools.h"//by Junlin 190122

//model 1104 trt
#include "ga_hcp_predict_net.h"
#include "ga_hcp_init_net.h"

//model 1104 caffe2
#include "ga_hcp_init_net_caffe2.h"
#include "ga_hcp_predict_net_caffe2.h"

#include "authority.h"
#include<boost/thread.hpp>

#ifdef _MSC_VER
#define productSN "5A72239196F54B678D311E7FC11849DC" //windows 车型系列产品序列??图片接口
#else
#define productSN "7CF8B4797F9E441686145BED07F662DE" //linux 车型系列产品序列??-图片接口
#endif

//#ifdef _MSC_VER
//#define productSN "BB73728FAB95492F86A90DA34DDB6D6D" //windows 车型系列产品序列??图片接口
//#else
//#define productSN "7CF8B4797F9E441686145BED07F662DE" //linux 车型系列产品序列??-图片接口
//#endif

//#ifdef _MSC_VER
//#define productSN "4ACFCBE67EF645AB8F0B4CFCDD1926F1" //WINDOWS瀹氫箟浜у搧绯诲垪鍙?//#else
//#define productSN "4FD45501D5104F0C8C4BE530FC872F46" //LINUX瀹氫箟浜у搧绯诲垪鍙?//#endif
#define AUTHORIZATION

const char * hcp_get_version()
{
	return "sy_human_car_parsing_version: v0.0.0.190402_timeout20190701";
}

struct hcp_handle
{
	void* handle;

	int licence_status = -1;
	int thrd_status = -1;
	boost::thread thrd;
};

void check_thread(hcp_handle* handle)
{
	int res = -1;
#ifdef __linux__
	char* wtime = new char[15];
	memset(wtime, 0, 15);
#endif
	while (1)
	{
		//printf("xxx check status on process...\n");
#ifdef _WIN32
		res = sy_licence(productSN);
#elif __linux__
		res = sy_licence(productSN, &wtime);
		//printf("--------------wtime in thread: %s, status: %d\n", wtime, licence_status);
#endif
		if (res < 0)
		{
			handle->licence_status = handle->licence_status - 1;
		}
		else
		{
			if (handle->licence_status < 0)
			{
				handle->licence_status = 0;
			}
		}
		boost::this_thread::sleep(boost::posix_time::seconds(600)); //10min
																	//boost::this_thread::sleep(boost::posix_time::milliseconds(10);
	}
#ifdef __linux__
	if (wtime)
	{
		delete[] wtime;
		wtime = NULL;
	}
#endif
}

int hcp_init(void ** handle, hcp_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(2020, 4);
	if (ret == SUCCESS)
#endif
	{
		hcp_handle* tools = new hcp_handle;

		ctools_init_params cparam;//by Junlin 190121
		cparam.device_type_ = param.mode;
		cparam.device_id_ = param.gpuid; //by Junlin 190121
		cparam.log_level_ = 0;
		cparam.engine_type_ = param.engine;
		cparam.model_type_ = MODEL_CLASSFY;

#ifdef _MSC_VER
		cparam.weight_file_ = NULL;// "../../src/human_car_parsing/hcp_20191008/hcp_predict_net";// NULL;
		cparam.net_file_ = NULL;// "../../src/human_car_parsing/hcp_20191008/hcp_init_net";// NULL;
#else
		cparam.weight_file_ = NULL;
		cparam.net_file_ = NULL;
#endif
		cparam.need_im_info_ = 0;
		cparam.data_process_str_ =
			//"CopyData_CPU2GPU_U8;"
			"TypeConvert_U8_F32;"
			"Resize_F32_F32,scale_w,128,scale_h,224,"
			"submean_b,123.68,submean_g,116.78,submean_r,103.94,"
			"variance_rev_b,0.01743,variance_rev_g,0.017507,variance_rev_r,0.017125;"
			"NHWC2NCHW_F32"
			;

		if (cparam.engine_type_ == ENGINE_TENSORRT)
		{
			cparam.weight_array_ = (unsigned char*)ga_hcp_init_net;
			cparam.weight_array_len_ = ga_hcp_init_net_len;
			cparam.net_array_ = (unsigned char*)ga_hcp_predict_net;
			cparam.net_array_len_ = ga_hcp_predict_net_len;
			cparam.trt_serialize_file_ = param.serialize_file;// NULL;// "HCP_ABCDEFGHIJKLMNOPQRSTUVWXYZ";

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

			memset(cparam.tensorrt_param_str_, 0, sizeof(cparam.tensorrt_param_str_));
			sprintf(cparam.tensorrt_param_str_, "max_batchsize %d,"
				"data_mode %s,"
				"is_create_calibrator %d,"
				"is_onnx_model %d,"
				"input_names blob1,"
				"output_names prob1 prob2 prob3 prob4 prob5 prob6 prob7 prob8 prob9 prob10 prob11 prob12 prob13 prob14 fc_blob1",
				batch_size, g_data_mode.c_str(), g_is_create_calibrator, g_is_onnx_model);
		}
		else if (cparam.engine_type_ == ENGINE_MCAFFE2)
		{
			cparam.weight_array_ = (unsigned char*)ga_hcp_init_net_caffe2;
			cparam.weight_array_len_ = ga_hcp_init_net_len_caffe2;
			cparam.net_array_ = (unsigned char*)ga_hcp_predict_net_caffe2;
			cparam.net_array_len_ = ga_hcp_predict_net_len_caffe2;
			cparam.input_data_blob_name_ = "blob1";
		}

		ret = ctools_init(&tools->handle, &cparam); //by Junlin 190121

		if (ret != SUCCESS)
		{
			printf("sy_human_car_parsing_hcp_init(error): init end. (ret = %d)\n", ret);
			ret = HUMANCARPARSING_INIT_ERROR;
		}

		else
		{
			tools->licence_status = 0;
#ifdef AUTHORIZATION
			tools->thrd = boost::thread(check_thread, tools);
#endif
			tools->thrd_status = 0;
			tools->thrd_status = 0;

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


int hcp_process(void * handle, sy_img img_data, hcp_analysis_result * result)
{
	hcp_handle* tools = (hcp_handle*)handle;

	if (tools->licence_status > -3)
	{
		if (tools->handle == NULL)
		{
			printf("sy_human_car_parsing_hcp_process(error): handle == null.\n");
			return HANDLE_NULL_ERROR;
		}
		if (img_data.data_ == NULL || img_data.w_ == 0 || img_data.h_ == 0)
		{
			printf("sy_human_car_parsing_hcp_process(error): imgdata == null or w == 0 or h == 0.\n");
			return PARAMS_NULL_ERROR;
		}
		ctools_result * cresult;
		int res = ctools_process(tools->handle, &img_data, 1, &cresult);//by Junlin 190121

		if (res != SUCCESS)
		{
			printf("sy_human_car_parsing_hcp_process(error): process failed. (ret = %d)\n", res);
		}
		else
		{
			ctools_result &cur_result = cresult[0];
			int big_class_count = cur_result.obj_count_;
			ctools_obj_result &index_score = cur_result.obj_results_[big_class_count];

/*
			if (index_score.data_count_ != HCP_ATTRI_INDEX_SIZE * 2)
			{
				printf("sy_human_car_parsing_hcp_process(error): wrong result count: %d.\n", index_score.data_count_);
				return FAILED;
			}
      */
			for (size_t i = 0; i < HCP_ATTRI_INDEX_SIZE; i++)
			{
        // int real_index = i+1;
				(*result).res_objs[i].res_index = index_score.data_[i * 2];
				(*result).res_objs[i].res_prob = index_score.data_[i * 2 + 1];
			}
      ctools_result * fresult;
			ctools_extract_layer(tools->handle, 1, &fresult, "fc_blob1");
			ctools_obj_result &feature = fresult[0].obj_results_[0];
      memcpy(result->feature, feature.data_, sizeof(float) * feature.data_count_);
		}
		return res;
	}
	else
	{
		return AUTHOR_ERROR;
	}
}


int hcp_batch(void * handle, sy_img * img_data_array, int batch_size, hcp_analysis_result * result)
{
	hcp_handle* tools = (hcp_handle*)handle;

	if (tools->licence_status > -3)
	{
		if (tools->handle == NULL)
		{
			printf("sy_human_car_parsing_hcp_batch(error): handle == null.\n");
			return HANDLE_NULL_ERROR;
		}
		if ((*img_data_array).data_ == NULL || (*img_data_array).w_ == 0 || (*img_data_array).h_ == 0)
		{
			printf("sy_human_car_parsing_hcp_batch(error): imgdata == null or w == 0 or h == 0.\n");
			return PARAMS_NULL_ERROR;
		}
		if (batch_size == 0)
		{
			printf("sy_human_car_parsing_hcp_batch(error): batch size == 0.\n");
			return PARAMS_NULL_ERROR;
		}

		ctools_result * cresult;
		int res = ctools_process(tools->handle, img_data_array, batch_size, &cresult);
		if (res != SUCCESS)
		{
			printf("sy_human_car_parsing_hcp_batch(error): process failed. (ret = %d)\n", res);
		}
		else
		{
			for (int b = 0; b < batch_size; b++)
			{
        ctools_result * fresult;
			  ctools_extract_layer(tools->handle, batch_size, &fresult, "fc_blob1");
				ctools_result &cur_result = cresult[b];
				int big_class_count = cur_result.obj_count_;
				ctools_obj_result &index_score = cur_result.obj_results_[big_class_count];
        /*
				if (index_score.data_count_ != HCP_ATTRI_INDEX_SIZE * 2)
				{
					printf("sy_human_car_parsing_hcp_batch(error): wrong result count: %d in pic[%d].\n", index_score.data_count_, b);
					break;
				}*/
				for (size_t i = 0; i < HCP_ATTRI_INDEX_SIZE; i++)
				{
         // int real_index = i+1;
					result[b].res_objs[i].res_index = index_score.data_[i * 2];
					result[b].res_objs[i].res_prob = index_score.data_[i * 2 + 1];
				}
        ctools_obj_result &feature = fresult[b].obj_results_[0];
        memcpy(result[b].feature, feature.data_, sizeof(float) * feature.data_count_);
			}
		}
		return res;
	}
	else
	{
		return AUTHOR_ERROR;
	}	
}


void hcp_release(void ** handle)
{
	hcp_handle* tools = (hcp_handle*)(*handle);
	if (tools)
	{
		ctools_release(&tools->handle);//by Junlin 190121

		if (tools->thrd_status == 0)
		{
			tools->thrd.interrupt();
			tools->thrd.join();
			tools->thrd_status = -1;
		}

		delete tools;
		tools = NULL;
	}
}

//
////杈呭姪鍑芥暟锛氭暟鎹澶勭悊
//float fruitmeanSub[3] = { 103.94, 116.78, 123.68 };
//float variance = 0.017;
//void datapreprocess(float * blob, int width, int height, int channels, int batchsize)
//{
//	int datasize_ = width * height;
//	for (int c = 0; c < 3; c++)
//	{
//		for (int i = 0; i < width * height; i++)
//		{
//			blob[datasize_ * c + i] = ((blob[datasize_ * c + i]) - fruitmeanSub[c]) * variance;
//		}
//	}
//
//}