遍历该目录下的.jpg图片
| 2 | |
| 3 | //遍历该目录下的.jpg图片 |
| 4 | void load_data_from_folder(std::string path, std::string type, std::vector<std::string> &list_images, std::vector<int> &list_labels, int label) |
| 5 | { |
| 6 | long long hFile = 0; //句柄 |
| 7 | struct _finddata_t fileInfo; |
| 8 | std::string pathName; |
| 9 | if ((hFile = _findfirst(pathName.assign(path).append("\\*.*").c_str(), &fileInfo)) == -1) |
| 10 | { |
| 11 | return; |
| 12 | } |
| 13 | do |
| 14 | { |
| 15 | const char* s = fileInfo.name; |
| 16 | const char* t = type.data(); |
| 17 | |
| 18 | if (fileInfo.attrib&_A_SUBDIR) //是子文件夹 |
| 19 | { |
| 20 | //遍历子文件夹中的文件(夹) |
| 21 | if (strcmp(s, ".") == 0 || strcmp(s, "..") == 0) //子文件夹目录是.或者.. |
| 22 | continue; |
| 23 | std::string sub_path = path + "\\" + fileInfo.name; |
| 24 | label++; |
| 25 | load_data_from_folder(sub_path, type, list_images, list_labels, label); |
| 26 | |
| 27 | } |
| 28 | else //判断是不是后缀为type文件 |
| 29 | { |
| 30 | if (strstr(s, t)) |
| 31 | { |
| 32 | std::string image_path = path + "\\" + fileInfo.name; |
| 33 | list_images.push_back(image_path); |
| 34 | list_labels.push_back(label); |
| 35 | } |
| 36 | } |
| 37 | } while (_findnext(hFile, &fileInfo) == 0); |
| 38 | return; |
| 39 | } |