#include "human_car_parsing.h" //#include #include #include #include #include #include "sy_common.h" #ifdef _WIN32 #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] = { "无", "物品" }; 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 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; //}