FileIO.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. //
  2. // Created by wrede on 19.04.16.
  3. //
  4. #include <algorithm>
  5. #include "FileIO.h"
  6. #include "Logger.h"
  7. #include "dirent.h"
  8. #include "../core/ObjectData2D.h"
  9. namespace util
  10. {
  11. void FileIO::ReadCSV(Vector3d& values, const std::string& file_name, char delimiter)
  12. {
  13. Logger::LogInfo("Read CSV file");
  14. std::ifstream in(file_name, std::ifstream::in);
  15. std::string line;
  16. // Read lines while the reader is in good condition
  17. while (in.good() && !in.eof())
  18. {
  19. getline(in, line);
  20. // Ignore empty lines
  21. if (line.size() == 0) continue;
  22. // Get frame index
  23. size_t dIndex = line.find(delimiter);
  24. std::string part = line.substr(0, dIndex);
  25. size_t frameIndex = std::stoul(part.c_str());
  26. // Extract point values
  27. std::vector<double> pointValues;
  28. while (dIndex != std::string::npos)
  29. {
  30. line = line.substr(dIndex + 1);
  31. dIndex = line.find(delimiter);
  32. part = line.substr(0, dIndex);
  33. if (part.size() > 0)
  34. {
  35. try
  36. {
  37. pointValues.push_back(std::stof(part.c_str()));
  38. }
  39. catch (const std::exception& e)
  40. {
  41. /* EMPTY */
  42. // Possible cause: "\n"
  43. }
  44. }
  45. }
  46. // Add point data to detection data
  47. while (frameIndex >= values.size())
  48. {
  49. values.push_back(std::vector<std::vector<double>>());
  50. }
  51. values[frameIndex].push_back(pointValues);
  52. }
  53. in.close();
  54. Logger::LogDebug("frame count " + std::to_string(values.size()));
  55. }
  56. void FileIO::ReadCSV(Vector2d& values, const std::string& file_name, char delimiter)
  57. {
  58. Logger::LogInfo("Read CSV file");
  59. std::ifstream in(file_name, std::ifstream::in);
  60. std::string line;
  61. // Read lines while the reader is in good condition and the
  62. // end of file is not reached
  63. while (in.good() && !in.eof())
  64. {
  65. getline(in, line);
  66. // Ignore empty lines
  67. if (line.size() == 0) continue;
  68. // Extract point values
  69. size_t dIndex;
  70. std::string part;
  71. std::vector<double> pointValues;
  72. do
  73. {
  74. dIndex = line.find(delimiter);
  75. part = line.substr(0, dIndex);
  76. if (part.size() > 0)
  77. {
  78. pointValues.push_back(std::stof(part.c_str()));
  79. }
  80. line = line.substr(dIndex + 1);
  81. }
  82. while (dIndex != std::string::npos);
  83. // Add point data to detection data
  84. values.push_back(pointValues);
  85. }
  86. in.close();
  87. Logger::LogDebug("line count " + std::to_string(values.size()));
  88. }
  89. void FileIO::ListFiles(const std::string& folder, std::vector<std::string>& file_names,
  90. bool sort)
  91. {
  92. Logger::LogInfo("List files in folder");
  93. DIR* dir;
  94. struct dirent *ent;
  95. if ((dir = opendir(folder.c_str())) != NULL)
  96. {
  97. int offset = 2;
  98. while ((ent = readdir(dir)) != NULL)
  99. {
  100. if (offset <= 0)
  101. {
  102. file_names.push_back(ent->d_name);
  103. }
  104. offset--;
  105. }
  106. closedir(dir);
  107. if (sort)
  108. {
  109. std::sort(file_names.begin(), file_names.end());
  110. }
  111. Logger::LogDebug("file count " + std::to_string(file_names.size()));
  112. }
  113. else
  114. {
  115. Logger::LogError("Could not open folder");
  116. }
  117. }
  118. void FileIO::WriteCSVMatlab(DirectedGraph& graph, const std::string& file_name)
  119. {
  120. char delimiter = ',';
  121. std::ofstream out(file_name, std::ofstream::out);
  122. // Iterate all outgoing edges of every vertex
  123. EdgeWeightMap weights = boost::get(boost::edge_weight, graph);
  124. VertexIndexMap indices = boost::get(boost::vertex_index, graph);
  125. boost::graph_traits<DirectedGraph>::vertex_iterator vi, vi_end;
  126. boost::graph_traits<DirectedGraph>::out_edge_iterator oei, oei_end;
  127. for (boost::tie(vi, vi_end) = boost::vertices(graph); vi != vi_end; ++vi)
  128. {
  129. for (boost::tie(oei, oei_end) = boost::out_edges(*vi, graph);
  130. oei != oei_end; ++oei)
  131. {
  132. unsigned long si = indices[boost::source(*oei, graph)];
  133. unsigned long ti = indices[boost::target(*oei, graph)];
  134. // Write the edge to file
  135. out << (si + 1) << delimiter
  136. << (ti + 1) << delimiter
  137. << weights[*oei] << std::endl;
  138. }
  139. }
  140. out.close();
  141. }
  142. void FileIO::WriteCSVMatlab(MultiPredecessorMap& map, Vertex& source, Vertex& sink,
  143. const std::string& file_name)
  144. {
  145. char delimiter = ',';
  146. std::ofstream out(file_name, std::ofstream::out);
  147. for (Vertex first : map[sink])
  148. {
  149. out << (sink + 1) << delimiter << (first + 1);
  150. for (Vertex u = first, v = (*map[u].begin()); u != v; u = v, v = (*map[v].begin()))
  151. {
  152. out << delimiter << (v + 1);
  153. if (v == source)
  154. {
  155. break;
  156. }
  157. }
  158. out << std::endl;
  159. }
  160. out.close();
  161. }
  162. void FileIO::ReadCSV(ValueMapVector& values, const std::string& file_name, char delimiter)
  163. {
  164. // Read the file
  165. std::ifstream in(file_name, std::ifstream::in);
  166. // Only proceed if the file could be opened
  167. if (!in.is_open())
  168. {
  169. throw "Unable to open file: " + file_name;
  170. }
  171. std::string line;
  172. // Get the first line that is not empty
  173. while (in.good() && !in.eof())
  174. {
  175. getline(in, line);
  176. if (line.size() > 0) break;
  177. }
  178. in.close();
  179. ReadCSV(values, line, file_name, delimiter);
  180. //TODO do not read the first line
  181. }
  182. void FileIO::ReadCSV(ValueMapVector& values, const std::string& header,
  183. const std::string& file_name, char delimiter)
  184. {
  185. Logger::LogInfo("Read CSV file");
  186. // Read the file
  187. std::ifstream in(file_name, std::ifstream::in);
  188. // Only proceed if the file could be opened
  189. if (!in.is_open())
  190. {
  191. throw "Unable to open file: " + file_name;
  192. }
  193. std::string line, part;
  194. size_t d_index;
  195. // Split the header into separate keys
  196. std::vector<std::string> key_vector;
  197. line = header;
  198. do
  199. {
  200. d_index = line.find(delimiter);
  201. part = line.substr(0, d_index);
  202. key_vector.push_back(part);
  203. line = line.substr(d_index + 1);
  204. }
  205. while (d_index != std::string::npos);
  206. util::Logger::LogDebug("parsed keys:");
  207. for (std::string str : key_vector)
  208. {
  209. util::Logger::LogDebug(str);
  210. }
  211. // Read lines while the reader is in good condition and the
  212. // end of file is not reached
  213. while (in.good() && !in.eof())
  214. {
  215. getline(in, line);
  216. // Ignore empty lines
  217. if (line.size() == 0) continue;
  218. // Extract detection values
  219. size_t key_index = 0;
  220. ValueMap detection_values;
  221. do
  222. {
  223. d_index = line.find(delimiter);
  224. part = line.substr(0, d_index);
  225. // Try to parse the value
  226. double value;
  227. try
  228. {
  229. value = std::stof(part.c_str());
  230. }
  231. catch (std::exception& e)
  232. {
  233. util::Logger::LogError(e.what());
  234. value = 0.0;
  235. }
  236. // Store the value
  237. detection_values[key_vector[key_index]] = value;
  238. ++key_index;
  239. line = line.substr(d_index + 1);
  240. }
  241. while (d_index != std::string::npos &&
  242. key_index < key_vector.size());
  243. // Add point data to detection data
  244. values.push_back(detection_values);
  245. }
  246. util::Logger::LogDebug("parsed values in line 2:");
  247. for (std::string str : key_vector)
  248. {
  249. util::Logger::LogDebug(str + "=" + std::to_string(values[1][str]));
  250. }
  251. in.close();
  252. }
  253. void FileIO::WriteTracks(std::vector<core::TrackletPtr> & tracks,
  254. std::string const & file_name, char delimiter)
  255. {
  256. // Get the frame range
  257. size_t first_frame = tracks[0]->GetFirstFrameIndex();
  258. size_t last_frame = tracks[0]->GetLastFrameIndex();
  259. for (auto track : tracks)
  260. {
  261. if (track->GetFirstFrameIndex() < first_frame)
  262. first_frame = track->GetFirstFrameIndex();
  263. if (track->GetLastFrameIndex() > last_frame)
  264. last_frame = track->GetLastFrameIndex();
  265. }
  266. std::ofstream out(file_name, std::ios::out);
  267. // Check if the file is open
  268. if (!out.is_open())
  269. {
  270. util::Logger::LogError("Unable to open file: " + file_name);
  271. return;
  272. }
  273. // Print every track, one detection per line, sorted by frame
  274. for (size_t frame = first_frame; frame <= last_frame; ++frame)
  275. {
  276. for (size_t i = 0; i < tracks.size(); ++i)
  277. {
  278. core::ObjectDataPtr obj = tracks[i]->GetFrameObject(frame);
  279. if (obj != nullptr)
  280. {
  281. std::string str = obj->ToString(delimiter);
  282. std::vector<std::string> parts = Split(str, delimiter);
  283. parts.insert(parts.begin() + 1, std::to_string(i + 1));
  284. out << parts[0];
  285. for (size_t j = 1; j < parts.size(); ++j)
  286. {
  287. out << delimiter << parts[j];
  288. }
  289. out << std::endl;
  290. }
  291. }
  292. }
  293. out.close();
  294. }
  295. std::vector<std::string> FileIO::Split(const std::string& input, char delimiter)
  296. {
  297. std::vector<std::string> output;
  298. std::stringstream ss(input);
  299. std::string part;
  300. while (getline(ss, part, delimiter))
  301. {
  302. output.push_back(part);
  303. }
  304. return output;
  305. }
  306. }