HumanCarParsing.cpp 7.87 KB
#include "HumanCarParsing.h"
#include <cuda.h>
#include <cuda_runtime.h>
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"

//string up[12] = { "T恤", "马甲/吊带/背心", "衬衫", "西服", "毛衣", "皮衣/夹克", "羽绒服", "大衣/风衣", "外套", "连衣裙", "无上衣", "其他" };
//string up_color[12] = { "黑", "白", "红", "黄", "蓝", "绿", "紫", "棕", "灰", "橙", "多色", "其他" };
//string down[6] = { "长裤", "短裤", "长裙", "短裙", "连衣裙", "其他" };
//string down_color[12] = { "黑", "白", "红", "黄", "蓝", "绿", "紫", "棕", "灰", "橙", "多色", "其他" };
//string bao[5] = { "无包", "单肩包", "双肩包", "其他", "钱包" };
//string bag_color[12] = { "黑", "白", "红", "黄", "蓝", "绿", "紫", "棕", "灰", "橙", "多色", "其他" };
//string head[6] = { "长发", "短发", "光头", "帽子", "头盔", "其他" };
//string clothing_text[5] = { "纯色", "碎花", "条纹", "格子", "其他" };
//string sex[3] = { "男", "女", "不明" };
//string figure[3] = { "胖", "瘦", "中" };
//string nationality[5] = { "汉族", "维族", "黑人", "白人", "其他" };
//string age[6] = { "幼儿", "儿童", "青年", "中年", "老年", "不明" };
//string eye[4] = { "正常眼睛", "眼镜", "墨镜", "其他" };
//string mouth[3] = { "正常嘴", "戴口罩", "其他" };
//string weibo[3] = { "无围巾", "普通围巾", "包头围巾" };
//
//string carColor[13] = { "黑", "白", "红", "黄", "蓝", "绿", "紫", "棕", "灰", "橙", "多色", "其他", "银" };
//string orient[3] = { "正面", "背面", "侧面" };
//string drivenum[4] = { "0人", "1人", "2人", "更多人" };
//string dasan[2] = { "无", "有" };
//string take[2] = { "无", "物品" };

//static std::string hcp_head[] = { "长发", "短发", "头盔", "其他" };
//static std::string hcp_eye[] = { "未戴眼镜", "戴眼镜" };
//static std::string hcp_mouth[] = { "未戴口罩", "戴口罩" };
//static std::string hcp_weibo[] = { "未带围巾", "带围巾" };
//static std::string hcp_up[] = { "T恤/背心", "衬衫", "毛衣", "外套", "连衣裙", "其他" };
//static std::string hcp_up_color[] = { "黑", "白", "灰", "红", "蓝", "黄", "绿", "多色", "不明" };
//static std::string hcp_clothing_text[] = { "纯色", "碎花", "条纹格子", "其他" };
//static std::string hcp_bao[] = { "无包", "背包" };
//static std::string hcp_sex[] = { "男", "女", "不明" };
//static std::string hcp_age[] = { "小孩", "成人", "不明" };
//static std::string hcp_carColor[] = { "黑", "白", "红", "其他" };
//static std::string hcp_orient[] = { "正面", "背面", "侧面" };
//static std::string hcp_dasan[] = { "无", "有" };
//static std::string hcp_take[] = { "无", "有" };

const int FIR_INDEX_SIZE = 14;
const int SEC_INDEX_SIZE[FIR_INDEX_SIZE] = {4,2,2,2,6,9,4,2,3,3,4,3,2,2 };
int HumanCarParsing_Init(void *&handle, int gpuid, char* auth_license)
{
	hcp_param param;
    param.mode = DEVICE_GPU;
    param.gpuid = gpuid;
	param.engine = ENGINE_TENSORRT;
	param.max_batch = 20;
	param.serialize_file = "./serialize_file/HCP";
	param.auth_license = auth_license;
	if (hcp_init(&handle, param) != 0)
	{
		cout << "Init HCP Failed!" << endl;
		return FAILED;
	}
       
	return SUCCESS;
}

#include <fstream>
int HumanCarParsing_Process(void * handle,  sy_img * batch_img, int batch_size, hcp_analysis_result *&result)
{
    //ctools_result *result=NULL;
	for (int i = 0; i < batch_size; i++)
	{
		if (batch_img[i].data_ == NULL)
			cout << i << " data null" << endl;
	}

	//cout << "begin hcp ProcessBatch" << endl;
	hcp_batch(handle, batch_img, batch_size, result);

	for (int b = 0; b < batch_size; b++)
	{
		hcp_analysis_result &cur_result = result[b];
		
		for (int i = 0; i < FIR_INDEX_SIZE; i++)
		{
			if (cur_result.res_objs[i].res_index >= SEC_INDEX_SIZE[i])
			{
				cur_result.res_objs[i].res_index = 0;
				//printf("HumanCarParsing_Process ERROR!!!!!!!!!!!! %d %d %d\n", i, cur_result.res_objs[i].res_index, SEC_INDEX_SIZE[i]);
				/*int data_size = batch_img[i].w_ * batch_img[i].h_ * batch_img[i].c_;
				unsigned char *snapshotDataHost = new unsigned char[data_size] {};
				cudaMemcpy(snapshotDataHost, batch_img[i].data_, data_size * sizeof(unsigned char), cudaMemcpyDeviceToHost);
				cv::Mat newVideoImg(batch_img[i].h_, batch_img[i].w_, CV_8UC3, snapshotDataHost);
				cv::imwrite("hcp.jpg", newVideoImg);
				delete[] snapshotDataHost;*/
				//printf("HumanCarParsing_Process ERROR!!!!!!!!!!!! %d %d %d\n", i, cur_result.res_objs[i].res_index, SEC_INDEX_SIZE[i]);
			}
		}
	}

	//cout << "end hcp ProcessBatch" << endl;
   // for (int b = 0; b < batch_size; b++)
   // {
   //     ctools_result &cur_result = result[b];
   //     int big_class_count = cur_result.obj_count_;
   //     ctools_obj_result &index_score = cur_result.obj_results_[big_class_count];


   //     //for (int i = 0; i < cur_result.obj_count_ + 1; i++)
   //     //	{
   //     //		ctools_obj_result &index_score_1 = cur_result.obj_results_[i];
   //     //	
   //     //		for (int j = 0; j < index_score_1.data_count_; j++)
   //     //			printf("%.2f ", index_score_1.data_[j]);
   //     //		printf("\n");
   //     //	}


   //     if (index_score.data_count_ != FIR_INDEX_SIZE * 2)
   //     {
   //         printf("wrong result.\n");
   //         break;
   //     }

   //     for (int i = 0; i < FIR_INDEX_SIZE; i++)
   //     {
   //         //int resIndex = result[j][i].index;
   //         int resIndex = index_score.data_[i * 2];
			//cout << "resIndex" << endl;
   //         //输出结果
   //         switch (i)
   //         {
   //             case 0:
   //                 cout << up[resIndex] << " ";
   //                 break;
   //             case 1:
   //                 cout << up_color[resIndex] << " ";
   //                 break;
   //             case 2:
   //                 cout << down[resIndex] << " ";
   //                 break;
   //             case 3:
   //                 cout << down_color[resIndex] << " ";
   //                 break;
   //             case 4:
   //                 cout << bao[resIndex] << " ";
   //                 break;
   //             case 5:
   //                 cout << bag_color[resIndex] << " ";
   //                 break;
   //             case 6:
   //                 cout << head[resIndex] << " ";
   //                 break;
   //             case 7:
   //                 cout << clothing_text[resIndex] << " ";
   //                 break;
   //             case 8:
   //                 cout << sex[resIndex] << " ";
   //                 break;
   //             case 9:
   //                 cout << figure[resIndex] << " ";
   //                 break;
   //             case 10:
   //                 cout << nationality[resIndex] << " ";
   //                 break;
   //             case 11:
   //                 cout << age[resIndex] << " ";
   //                 break;
   //             case 12:
   //                 cout << eye[resIndex] << " ";
   //                 break;
   //             case 13:
   //                 cout << mouth[resIndex] << " ";
   //                 break;
   //             case 14:
   //                 cout << weibo[resIndex] << " ";
   //                 break;
   //             case 15:
   //                 cout << carColor[resIndex] << " ";
   //                 break;
   //             case 16:
   //                 cout << orient[resIndex] << " ";
   //                 break;
   //             case 17:
   //                 cout << drivenum[resIndex] << " ";
   //                 break;
   //             case 18:
   //                 cout << dasan[resIndex] << " ";
   //                 break;
   //             case 19:
   //                 cout << take[resIndex] << " ";
   //                 break;
   //             default:
   //                 break;
   //         }

   //     }
   //     cout << endl;

   //     //cudaFree(images[j].data);
   // }

	return SUCCESS;

}
int HumanCarParsing_Release(void *& handle)
{
	if(handle)
	hcp_release(&handle);
	return SUCCESS;
}