#include "../HCP/HCP.h" //#include #include #include #include #include //#include #include "sy_common.h" #ifdef _WIN32 #include #include #else #include #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] = { "无", "物品"}; const int FIR_INDEX_SIZE = 20; const int SEC_INDEX_SIZE[FIR_INDEX_SIZE] = {12,12,6,12,5,12,6,5,3,3,5,6,4,3,3,13,3,4,2,2 }; #ifdef _WIN32 int getFiles(string path, vector& nameList, vector& pathList) { //文件句柄 intptr_t hFile = 0; //文件信息 struct _finddata_t fileinfo; string p; int fileCount = 0; if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) { do {//如果是目录,迭代之 //如果不是,加入列表 if ((fileinfo.attrib & _A_SUBDIR)) { if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) { int count = getFiles(p.assign(path).append("\\").append(fileinfo.name), nameList, pathList); fileCount += count; //cout << fileinfo.name << " " << count << endl; } } else { fileCount++; pathList.push_back(p.assign(path).append("\\").append(fileinfo.name)); nameList.push_back(fileinfo.name); //cout << p.assign(path).append("\\").append(fileinfo.name) << endl; } } while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile); } return fileCount; } #endif //****************** 返回所有小类别的结果 ********************// void HCPAllResult() { printf("HCP Version: %s\n", HumanCarParsing_GetVersion()); #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 = DEVICE_GPU; param.gpuid = 0; if (InitHCP(tools, param) != 0) cout << "Init HCP 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 HumanCarParsingProcess(tools, img.data, img.cols, img.rows, img.channels(), &result, true); #ifdef _WIN32 QueryPerformanceCounter(&end); printf("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("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; /*释放资源*/ ReleaseHCP(tools); } //****************** 仅返回大类别的检测结果 ********************// void HCPOneResult() { printf("HCP Version: %s\n", HumanCarParsing_GetVersion()); /*初始化*/ void *tools; HCP_Param param; param.mode = DEVICE_GPU; param.gpuid = 0; if (InitHCP(tools, param) != 0) cout << "Init HCP 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); /*初始化结果结构体*/ ctools_result *result; /*人骑车结构化检测*/ HumanCarParsingProcess(tools, img.data, img.cols, img.rows, img.channels(), &result, false); /*输出保存结构*/ cout << endl << imgpath << " " << endl; ctools_result &cur_result = result[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_ != FIR_INDEX_SIZE * 2) { printf("count: %d, wrong result.\n",index_score.data_count_); break; } for (size_t i = 0; i < FIR_INDEX_SIZE; i++) { //各类别统计结果 //int resIndex = result[i][0].index; //仅返回大类别种类时,直接用第一个元素的值即可 int resIndex = index_score.data_[i * 2]; //输出结果 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; } } } /*释放资源*/ ReleaseHCP(tools); } void HCPBatch() { //ofstream f("hcp_result.txt");//log //vector nameList; //vector pathList; //int piccount = getFiles("E:/开发/视频结构化/视频结构化新框架修改/二次属性测试图片/二次属性测试图片/picHCP/", nameList, pathList); //f << "total vehicle pic count = " << piccount << endl; //const int batchsize = 10; printf("HCP Version: %s\n", HumanCarParsing_GetVersion()); const int batchsize = 20; void *tools; HCP_Param param; param.mode = DEVICE_GPU; param.gpuid = 0; if (InitHCP(tools, param) != 0) cout << "Init HCP Failed!" << endl; /*初始化结果结构体*/ ctools_result *result; 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/%d.jpg", b); sprintf(strpath, "../../data/HCPTEST/%d.jpg", b); #else sprintf(strpath, "/home/jlliu/objecteye/video_structure/HumanCarParsing/data/HumanCarImageData/%d.jpg", b); #endif img[b] = cv::imread(strpath); //img[b] = cv::imread(pathList[b]); images[b].SetData(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 time_Array; int count = 10000; //while (count--) { #ifdef _MSC_VER QueryPerformanceCounter(&nBeginTime); #else gettimeofday(&t1, NULL); #endif HumanCarParsingProcessBatch(tools, images, batchsize, &result); #ifdef _MSC_VER QueryPerformanceCounter(&nEndTime); time_val = (double)(nEndTime.QuadPart - nBeginTime.QuadPart) * 1000 / (double)nFreq.QuadPart; printf("HumanCarParsingProcessBatch: %.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("HumanCarParsingProcessBatch = %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("HumanCarParsingProcessBatch avg cost: %.2f\n", total / (time_Array.size() - 1)); for (int b = 0; b < batchsize; b++) { cout << "../../data/HumanCarImageData/"<< b << ".jpg " << endl; 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]; //输出结果 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; //f << b << " - " << nameList[b] << endl; //for (int i = 0; i < FIR_INDEX_SIZE; i++) //{ // //int resIndex = result[j][i].index; // int resIndex = index_score.data_[i * 2]; // //输出结果 // switch (i) // { // case 0: // f << up[resIndex] << "_"; // break; // case 1: // f << up_color[resIndex] << "_"; // break; // case 2: // f<< down[resIndex] << "_"; // break; // case 3: // f<< down_color[resIndex] << "_"; // break; // case 4: // f<< bao[resIndex] << "_"; // break; // case 5: // f << bag_color[resIndex] << "_"; // break; // case 6: // f << head[resIndex] << "_"; // break; // case 7: // f << clothing_text[resIndex] << "_"; // break; // case 8: // f << sex[resIndex] << "_"; // break; // case 9: // f << figure[resIndex] << "_"; // break; // case 10: // f << nationality[resIndex] << "_"; // break; // case 11: // f << age[resIndex] << "_"; // break; // case 12: // f << eye[resIndex] << "_"; // break; // case 13: // f << mouth[resIndex] << "_"; // break; // case 14: // f << weibo[resIndex] << "_"; // break; // case 15: // f << carColor[resIndex] << "_"; // break; // case 16: // f << orient[resIndex] << "_"; // break; // case 17: // f << drivenum[resIndex] << "_"; // break; // case 18: // f << dasan[resIndex] << "_"; // break; // case 19: // f << take[resIndex] << "_"; // break; // default: // break; // } //} //f << endl; //f << endl; } ReleaseHCP(tools); } int main() { HCPBatch(); //int count = 100; //while (count--) // HCPAllResult(); //返回所有小类别的检测结果 // HCPOneResult(); //仅返回大类别的检测结果 #ifdef _MSC_VER system("pause"); #endif return 0; }