1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
5 #include "classification_set_generator.hpp"
16 #include "user_exception.hpp"
17 #include "details/ie_exception.hpp"
20 # include "w_dirent.h"
22 # include <sys/stat.h>
26 #include "../common/samples/common.hpp"
29 * @brief Gets path part of a filename including separator
30 * @param filepath - filename to extract path part from
31 * @return string with path part of the filename
33 inline std::string folderOf(const std::string &filepath) {
34 auto pos = filepath.rfind("/");
35 if (pos == std::string::npos) pos = filepath.rfind("\\");
36 if (pos == std::string::npos) return "";
37 return filepath.substr(0, pos + 1);
40 void readFile(std::string filename, std::function<void(std::string&, int lineNumber)> perLine) {
41 std::ifstream inputFile;
42 inputFile.open(filename, std::ios::in);
43 std::string strLine = "";
45 if (!inputFile.is_open())
46 THROW_IE_EXCEPTION << "Cannot open file: " << filename;
48 size_t lineNumber = 0;
49 while (std::getline(inputFile, strLine)) {
51 perLine(strLine, lineNumber);
55 std::map<std::string, int> ClassificationSetGenerator::readLabels(const std::string& labels) {
59 readFile(labels, [&](std::string& line, size_t lineNumber) {
67 std::string getFullName(const std::string& name, const std::string& dir) {
68 return dir + "/" + name;
71 std::list<std::string> ClassificationSetGenerator::getDirContents(const std::string& dir, bool includePath) {
73 if (stat(dir.c_str(), &sb) != 0 || !S_ISDIR(sb.st_mode)) {
74 return std::list<std::string>();
75 // THROW_USER_EXCEPTION(1) << "Can't read contents of directory " << dir << ". It isn't a directory or not accessible";
78 std::list<std::string> list;
80 dp = opendir(dir.c_str());
82 THROW_USER_EXCEPTION(1) << "Can't open directory " << dir;
86 while (nullptr != (ep = readdir(dp))) {
87 std::string fileName = ep->d_name;
88 if (fileName == "." || fileName == "..") continue;
89 list.push_back(includePath ? getFullName(ep->d_name, dir) : ep->d_name);
95 std::vector<std::pair<int, std::string>> ClassificationSetGenerator::validationMapFromTxt(const std::string& file) {
96 std::string ext = fileExt(file);
98 THROW_USER_EXCEPTION(1) << "Unknown dataset data file format: " << ext << "";
101 std::string dir = folderOf(file);
102 std::vector<std::pair<int, std::string>> validationMap;
103 std::string imgPath = "";
106 readFile(file, [&](std::string& line, size_t lineNumber) {
108 size_t pos = line.rfind(" ");
109 if (pos == std::string::npos) {
110 THROW_USER_EXCEPTION(1) << "Bad file format! Cannot parse line " << lineNumber << ":\n> " << line;
113 classId = std::stoi(line.substr(pos + 1));
114 } catch (const std::invalid_argument& e) {
115 THROW_USER_EXCEPTION(1) << "Invalid class id specified at line " << lineNumber << ":\n> " << line
116 << " Error: " << e.what();
118 imgPath = line.substr(0, pos);
119 validationMap.push_back({ classId, dir + imgPath });
122 return validationMap;
125 std::vector<std::pair<int, std::string>> ClassificationSetGenerator::validationMapFromFolder(const std::string& dir) {
126 std::vector<std::pair<int, std::string>> validationMap;
127 std::list<std::string> validation_labels = getDirContents(dir, false);
129 for (auto& label : validation_labels) {
130 auto val = _classes.find(label);
131 if (val == _classes.end()) continue;
133 int id = val->second;
134 for (auto& image : getDirContents(getFullName(label, dir))) {
135 validationMap.push_back({ id, image });
138 return validationMap;
141 std::vector<std::pair<int, std::string>> ClassificationSetGenerator::getValidationMap(const std::string& path) {
143 if (stat(path.c_str(), &sb) == 0) {
144 if (S_ISDIR(sb.st_mode)) {
145 return validationMapFromFolder(path);
147 return validationMapFromTxt(path);
150 if (errno == ENOENT || errno == EINVAL || errno == EACCES) {
151 THROW_USER_EXCEPTION(3) << "The specified path \"" << path << "\" can not be found or accessed";