Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / samples / validation_app / classification_set_generator.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include "classification_set_generator.hpp"
6
7 #include <algorithm>
8 #include <fstream>
9 #include <iostream>
10 #include <list>
11 #include <map>
12 #include <string>
13 #include <utility>
14 #include <vector>
15
16 #include "user_exception.hpp"
17 #include "details/ie_exception.hpp"
18
19 #ifdef _WIN32
20 # include "w_dirent.h"
21 #else
22 # include <sys/stat.h>
23 # include <dirent.h>
24 #endif
25
26 #include "../common/samples/common.hpp"
27
28 /**
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
32  */
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);
38 }
39
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 = "";
44
45     if (!inputFile.is_open())
46         THROW_IE_EXCEPTION << "Cannot open file: " << filename;
47
48     size_t lineNumber = 0;
49     while (std::getline(inputFile, strLine)) {
50         lineNumber++;
51         perLine(strLine, lineNumber);
52     }
53 }
54
55 std::map<std::string, int> ClassificationSetGenerator::readLabels(const std::string& labels) {
56     _classes.clear();
57     int i = 0;
58
59     readFile(labels, [&](std::string& line, size_t lineNumber) {
60         trim(line);
61         _classes[line] = i++;
62     });
63
64     return _classes;
65 }
66
67 std::string getFullName(const std::string& name, const std::string& dir) {
68     return dir + "/" + name;
69 }
70
71 std::list<std::string> ClassificationSetGenerator::getDirContents(const std::string& dir, bool includePath) {
72     struct stat sb;
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";
76     }
77
78     std::list<std::string> list;
79     DIR *dp;
80     dp = opendir(dir.c_str());
81     if (dp == nullptr) {
82         THROW_USER_EXCEPTION(1) << "Can't open directory " << dir;
83     }
84
85     struct dirent *ep;
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);
90     }
91     closedir(dp);
92     return list;
93 }
94
95 std::vector<std::pair<int, std::string>> ClassificationSetGenerator::validationMapFromTxt(const std::string& file) {
96     std::string ext = fileExt(file);
97     if (ext != "txt") {
98         THROW_USER_EXCEPTION(1) << "Unknown dataset data file format: " << ext << "";
99     }
100
101     std::string dir = folderOf(file);
102     std::vector<std::pair<int, std::string>> validationMap;
103     std::string imgPath = "";
104     int classId = -1;
105
106     readFile(file, [&](std::string& line, size_t lineNumber) {
107         trim(line);
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;
111         }
112         try {
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();
117         }
118         imgPath = line.substr(0, pos);
119         validationMap.push_back({ classId, dir + imgPath });
120     });
121
122     return validationMap;
123 }
124
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);
128
129     for (auto& label : validation_labels) {
130         auto val = _classes.find(label);
131         if (val == _classes.end()) continue;
132
133         int id = val->second;
134         for (auto& image : getDirContents(getFullName(label, dir))) {
135             validationMap.push_back({ id, image });
136         }
137     }
138     return validationMap;
139 }
140
141 std::vector<std::pair<int, std::string>> ClassificationSetGenerator::getValidationMap(const std::string& path) {
142     struct stat sb;
143     if (stat(path.c_str(), &sb) == 0) {
144         if (S_ISDIR(sb.st_mode)) {
145             return validationMapFromFolder(path);
146         } else {
147             return validationMapFromTxt(path);
148         }
149     } else {
150         if (errno == ENOENT || errno == EINVAL || errno == EACCES) {
151             THROW_USER_EXCEPTION(3) << "The specified path \"" << path << "\" can not be found or accessed";
152         }
153     }
154     return{};
155 }