test.cpp 14.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
#include "human_car_parsing.h"
//#include <cv.h>
#include <highgui.h>
#include <opencv2/opencv.hpp>
#include <fstream>
#include <iostream>
#include "sy_common.h"

#ifdef _WIN32
#include<windows.h>
#else
#include <sys/time.h>
#endif


using namespace std;
//using namespace cv;

//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 head[] = { "长发", "短发", "头盔", "其他" };
static std::string eye[] = { "未戴眼镜", "戴眼镜" };
static std::string mouth[] = { "未戴口罩", "戴口罩" };
static std::string weibo[] = { "未戴围巾", "戴围巾" };
static std::string up[] = { "T恤/背心", "衬衫", "毛衣", "外套", "连衣裙", "其他" };
static std::string up_color[] = { "黑", "白", "灰", "红", "蓝", "黄", "绿", "多色", "不明" };
static std::string clothing_text[] = { "纯色", "碎花", "条纹/格子", "其他" };
static std::string bao[] = { "无包", "背包" };
static std::string sex[] = { "男", "女", "不明" };
static std::string age[] = { "小孩", "成人", "其他" };
static std::string carColor[] = { "黑", "白", "红", "其他" };
static std::string orient[] = { "正面", "背面", "侧面" };
static std::string dasan[] = { "无", "有" };
static std::string take[] = { "无", "有" };

//string top_list[12] = { "T恤", "马甲/吊带/背心", "衬衫", "西服", "毛衣", "皮衣/夹克", "羽绒服", "大衣/风衣", "外套", "连衣裙", "无上衣", "其他" };
//string top_color_list[12] = { "黑", "白", "红", "黄", "蓝", "绿", "紫", "棕", "灰", "橙", "多色", "其他" };
//string bottom_list[6] = { "长裤", "短裤", "长裙", "短裙", "连衣裙", "其他" };
//string bottom_color_list[12] = { "黑", "白", "红", "黄", "蓝", "绿", "紫", "棕", "灰", "橙", "多色", "其他" };
//string bag_list[5] = { "无包", "单肩包", "双肩包", "其他", "钱包" };
//string bag_color_list[12] = { "黑", "白", "红", "黄", "蓝", "绿", "紫", "棕", "灰", "橙", "多色", "其他" };
//string head_feature_list[6] = { "长发", "短发", "光头", "帽子", "头盔", "其他" };
//string clothes_pattern_list[5] = { "纯色", "碎花", "条纹", "格子", "其他" };
//string gender_list[3] = { "男", "女", "不明" };
//string body_size_list[3] = { "胖", "瘦", "中" };
//string ethnic_group_list[5] = { "汉族", "维族", "黑人", "白人", "其他" };
//string age_group_list[6] = { "幼儿", "儿童", "青年", "中年", "老年", "不明" };
//string eye_feature_list[4] = { "正常眼睛", "眼镜", "墨镜", "其他" };
//string mouth_feature_list[3] = { "正常嘴", "戴口罩", "其他" };
//string neck_feature_list[3] = { "无围巾", "普通围巾", "包头围巾" };
//string vehicle_color_list[13] = { "黑", "白", "红", "黄", "蓝", "绿", "紫", "棕", "灰", "橙", "多色", "其他", "银" };
//string direction_list[3] = { "正面", "背面", "侧面" };
//string passenger_count_list[4] = { "0人", "1人", "2人", "更多人" };
//string umbrella_list[2] = { "无", "有" };
//string belongings_list[2] = { "无", "物品" };


//const int FIR_INDEX_SIZE = 20;
//const int HCP_SEC_INDEX_SIZE[HCP_FIR_INDEX_SIZE] = { 12,12,6,12,5,12,6,5,3,3,5,6,4,3,3,13,3,4,2,2 };
//const int HCP_SEC_INDEX_SIZE[HCP_ATTRI_INDEX_SIZE] = { 3,2,2,2,6,5,4,2,3,3,5,3,2,2 };

////****************** 返回所有小类别的结果 ********************//

//void hcp_all_result()
//{
//	printf("%s\n", hcp_get_version());
//#ifdef _WIN32
//	LARGE_INTEGER begin, end, freq;
//	QueryPerformanceFrequency(&freq);
//#else
//	struct timeval tv1, tv2;
//#endif
//	double totalTime = 0.0;
//
//	/*初始化*/
//	void *tools;
//	hcp_param param;
//	//	param.mode = "cpu";
//	param.mode = DEVICE_GPU;
//	param.gpuid = 0;
//	if (hcp_init(&tools, param) != 0)
//		cout << "hcp_init failed!" << endl;
//
//
//	int count = 11;
//	//	while (count--)
//	{
//		for (int index = 0; index < 10; index++)
//			//		for (int index = 0; index < 1; index++)
//		{
//			char imgpath[260];
//			cv::Mat img;
//
//#ifdef _MSC_VER
//			sprintf(imgpath, "../../data/HumanCarImageData/%d.jpg", index);// b);
//#else
//			sprintf(imgpath, "/home/jlliu/objecteye/video_structure/HumanCarParsing/data/HumanCarImageData/%d.jpg", index);
//
//#endif
//			img = cv::imread(imgpath);
//
//			ctools_result *result;
//
//			/*人骑车结构化检测*/
//#ifdef _WIN32
//			QueryPerformanceCounter(&begin);
//#else
//			gettimeofday(&tv1, NULL);
//#endif
//
//			hcp_process(tools, img.data, img.cols, img.rows, img.channels(), &result);
//
//#ifdef _WIN32
//			QueryPerformanceCounter(&end);
//			printf("hcp process time: %.2f\n", (double)(end.QuadPart - begin.QuadPart) * 1000 / (double)freq.QuadPart);
//#else
//			gettimeofday(&tv2, NULL);
//			long dt1 = tv1.tv_sec * 1000 + tv1.tv_usec / 1000;
//			long dt2 = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
//			printf("hcp process time: %ld ms\n", dt2 - dt1);
//#endif
//
//			/*输出保存结构*/
//			//cout << endl << imgpath << " " << endl;
//			cout << imgpath << " " << endl;
//
//			ctools_result &cur_result = result[0];
//			int big_class_count = cur_result.obj_count_;
//			if (big_class_count != FIR_INDEX_SIZE)
//			{
//				printf("big_class_count: %d, wrong result.\n", big_class_count);
//				break;
//			}
//
//			for (size_t i = 0; i < FIR_INDEX_SIZE; i++)
//			{
//				//各类别统计结果
//				int resIndex = 0;
//				float resMaxprob = -100;
//
//				ctools_obj_result &index_score = cur_result.obj_results_[i];
//				for (int j = 0; j < index_score.data_count_; j++)
//				{
//					if (index_score.data_[j] > resMaxprob)
//					{
//						resMaxprob = index_score.data_[j];
//
//						resIndex = j;
//					}
//				}
//				//输出结果
//				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;
//		}
//	}
//
//	//	cout << "AVG TIME: " << totalTime / 100 << endl;
//		/*释放资源*/
//	hcp_release(&tools);
//}


//****************** 仅返回大类别的检测结果 ********************//
void hcp_one_result()
{
	printf("%s\n", hcp_get_version());
	/*初始化*/
	void *tools;
	hcp_param param;
	param.mode = DEVICE_GPU;
	param.gpuid = 3;
	if (hcp_init(&tools, param) != 0)
		cout << "hcp_init failed!" << endl;


	for (int index = 0; index < 10; index++)
	{
		char imgpath[260];
		cv::Mat img;

#ifdef _MSC_VER
		sprintf(imgpath, "../../data/HumanCarImageData/%d.jpg", index);// b);
#else
		sprintf(imgpath, "/home/jlliu/objecteye/video_structure/HumanCarParsing/data/HumanCarImageData/%d.jpg", index);

#endif
		img = cv::imread(imgpath);
		sy_img syimg;
		syimg.set_data(img.cols, img.rows, img.channels(), img.data);

		/*初始化结果结构体*/
		hcp_analysis_result result;

		/*人骑车结构化检测*/
		hcp_process(tools, syimg, &result);

		/*输出保存结构*/
		cout << endl << imgpath << " " << endl;

		for (size_t i = 0; i < HCP_ATTRI_INDEX_SIZE; i++)
		{
			//各类别统计结果
			int resIndex = result.res_objs[i].res_index;
			//输出结果
			switch (i)
			{
			case 0:
				std::cout << head[resIndex] << " ";
				break;
			case 1:
				std::cout << eye[resIndex] << " ";
				break;
			case 2:
				std::cout << mouth[resIndex] << " ";
				break;
			case 3:
				std::cout << weibo[resIndex] << " ";
				break;
			case 4:
				std::cout << up[resIndex] << " ";
				break;
			case 5:
				std::cout << up_color[resIndex] << " ";
				break;
			case 6:
				std::cout << clothing_text[resIndex] << " ";
				break;
			case 7:
				std::cout << bao[resIndex] << " ";
				break;
			case 8:
				std::cout << sex[resIndex] << " ";
				break;
			case 9:
				std::cout << age[resIndex] << " ";
				break;
			case 10:
				std::cout << carColor[resIndex] << " ";
				break;
			case 11:
				std::cout << orient[resIndex] << " ";
				break;
			case 12:
				std::cout << dasan[resIndex] << " ";
				break;
			case 13:
				std::cout << take[resIndex] << " ";
				break;
			default:
				break;
			}

			
		}
		cout << endl;
	}
	/*释放资源*/
	hcp_release(&tools);
}

void hcp_batch_test()
{
	printf("%s\n", hcp_get_version());
	const int batchsize = 20;

	void *tools;
	hcp_param param;
	param.mode = DEVICE_GPU;
	param.gpuid = 0;
 param.engine = ENGINE_TENSORRT;
	param.serialize_file = "HCP";
	param.max_batch = 20;

	if (hcp_init(&tools, param) != 0)
		cout << "hcp_init failed!" << endl;

	/*初始化结果结构体*/
	hcp_analysis_result result[batchsize];
	sy_img images[batchsize];
	cv::Mat img[batchsize];

	char strpath[1024];
	memset(strpath, 0, sizeof(strpath));
	for (int b = 0; b < batchsize; b++)
	{
#ifdef _MSC_VER
		sprintf(strpath, "../../data/HumanCarImageData/RC_75547.jpg", b);
#else
		sprintf(strpath, "../../data/HumanCarImageData/RC_75547.jpg", b);

#endif
		img[b] = cv::imread(strpath);
		images[b].set_data(img[b].cols, img[b].rows, img[b].channels(), img[b].data);
	}

#ifdef _MSC_VER
	LARGE_INTEGER nFreq, nBeginTime, nEndTime;
	QueryPerformanceFrequency(&nFreq);
#else
	struct timeval t1, t2;
#endif
	double time_val = 0;
	vector<double> time_Array;

	int count = 1000;
	while (count--)
	{

#ifdef _MSC_VER
		QueryPerformanceCounter(&nBeginTime);
#else
		gettimeofday(&t1, NULL);

#endif

		hcp_batch(tools, images, batchsize, result);

#ifdef _MSC_VER
		QueryPerformanceCounter(&nEndTime);
		time_val = (double)(nEndTime.QuadPart - nBeginTime.QuadPart) * 1000 / (double)nFreq.QuadPart;
		printf("hcp_batch cost: %.2f ms  \n", time_val);
#else
		gettimeofday(&t2, NULL);
		time_val = ((t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec - t1.tv_usec) / 1000.0;
		printf("hcp_batch cost= %lf ms\n", time_val);
#endif
		time_Array.push_back(time_val);
	}
	double total = 0;
	for (int n = 1; n < time_Array.size(); n++)
	{
		total += time_Array[n];
	}

	printf("hcp_batch avg cost: %.2f\n", total / (time_Array.size() - 1));


	for (int b = 0; b < batchsize; b++)
	{
		for (int i = 0; i < HCP_ATTRI_INDEX_SIZE; i++)
		{
			int resIndex = result[b].res_objs[i].res_index;
			cout << resIndex << " ";
			////输出结果
			/*switch (i)
			{
			case 0:
				std::cout << head[resIndex] << " ";
				break;
			case 1:
				std::cout << eye[resIndex] << " ";
				break;
			case 2:
				std::cout << mouth[resIndex] << " ";
				break;
			case 3:
				std::cout << weibo[resIndex] << " ";
				break;
			case 4:
				std::cout << up[resIndex] << " ";
				break;
			case 5:
				std::cout << up_color[resIndex] << " ";
				break;
			case 6:
				std::cout << clothing_text[resIndex] << " ";
				break;
			case 7:
				std::cout << bao[resIndex] << " ";
				break;
			case 8:
				std::cout << sex[resIndex] << " ";
				break;
			case 9:
				std::cout << age[resIndex] << " ";
				break;
			case 10:
				std::cout << carColor[resIndex] << " ";
				break;
			case 11:
				std::cout << orient[resIndex] << " ";
				break;
			case 12:
				std::cout << dasan[resIndex] << " ";
				break;
			case 13:
				std::cout << take[resIndex] << " ";
				break;
			default:
				break;
			}*/
		}
		cout << endl << endl;
	}
	hcp_release(&tools);
}

int main()
{
	hcp_batch_test();
	//int count = 100;
	//while (count--)
	//hcp_all_result();    //返回所有小类别的检测结果
	//hcp_one_result();    //仅返回大类别的检测结果

#ifdef _MSC_VER
	system("pause");
#endif  

	return 0;
}



//20类
//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;
//}