FileIO.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. }
  181. void FileIO::ReadCSV(ValueMapVector& values, const std::string& header,
  182. const std::string& file_name, char delimiter)
  183. {
  184. Logger::LogInfo("Read CSV file");
  185. // Read the file
  186. std::ifstream in(file_name, std::ifstream::in);
  187. // Only proceed if the file could be opened
  188. if (!in.is_open())
  189. {
  190. throw "Unable to open file: " + file_name;
  191. }
  192. std::string line, part;
  193. size_t d_index;
  194. // Split the header into separate keys
  195. std::vector<std::string> key_vector;
  196. line = header;
  197. do
  198. {
  199. d_index = line.find(delimiter);
  200. part = line.substr(0, d_index);
  201. key_vector.push_back(part);
  202. line = line.substr(d_index + 1);
  203. }
  204. while (d_index != std::string::npos);
  205. util::Logger::LogDebug("parsed keys:");
  206. for (std::string str : key_vector)
  207. {
  208. util::Logger::LogDebug(str);
  209. }
  210. // Read lines while the reader is in good condition and the
  211. // end of file is not reached
  212. while (in.good() && !in.eof())
  213. {
  214. getline(in, line);
  215. // Ignore empty lines
  216. if (line.size() == 0) continue;
  217. // Extract detection values
  218. size_t key_index = 0;
  219. ValueMap detection_values;
  220. do
  221. {
  222. d_index = line.find(delimiter);
  223. part = line.substr(0, d_index);
  224. // Try to parse the value
  225. double value;
  226. try
  227. {
  228. value = std::stof(part.c_str());
  229. }
  230. catch (std::exception& e)
  231. {
  232. util::Logger::LogError(e.what());
  233. value = 0.0;
  234. }
  235. // Store the value
  236. detection_values[key_vector[key_index]] = value;
  237. ++key_index;
  238. line = line.substr(d_index + 1);
  239. }
  240. while (d_index != std::string::npos &&
  241. key_index < key_vector.size());
  242. // Add point data to detection data
  243. values.push_back(detection_values);
  244. }
  245. util::Logger::LogDebug("parsed values in line 2:");
  246. for (std::string str : key_vector)
  247. {
  248. util::Logger::LogDebug(str + "=" + std::to_string(values[1][str]));
  249. }
  250. in.close();
  251. }
  252. void FileIO::WriteTracks(std::vector<core::TrackletPtr>& tracks, const std::string& file_name,
  253. char delimiter)
  254. {
  255. // Get the frame range
  256. size_t first_frame = tracks[0]->GetFirstFrameIndex();
  257. size_t last_frame = tracks[0]->GetLastFrameIndex();
  258. for (auto track : tracks)
  259. {
  260. if (track->GetFirstFrameIndex() < first_frame)
  261. first_frame = track->GetFirstFrameIndex();
  262. if (track->GetLastFrameIndex() > last_frame)
  263. last_frame = track->GetLastFrameIndex();
  264. }
  265. std::ofstream out(file_name, std::ios::out);
  266. if (!out.is_open())
  267. {
  268. util::Logger::LogError("Unable to open file: " + file_name);
  269. return;
  270. }
  271. for (size_t frame = first_frame; frame <= last_frame; ++frame)
  272. {
  273. for (size_t i = 0; i < tracks.size(); ++i)
  274. {
  275. core::ObjectData2DPtr obj =
  276. std::static_pointer_cast<core::ObjectData2D>(tracks[i]->GetFrameObject(frame));
  277. if (obj != nullptr)
  278. {
  279. out << obj->GetPosition().x << delimiter;
  280. out << obj->GetPosition().y;
  281. }
  282. else
  283. {
  284. out << delimiter;
  285. }
  286. if (i < (tracks.size() - 1))
  287. {
  288. out << delimiter;
  289. }
  290. }
  291. out << std::endl;
  292. }
  293. out.close();
  294. }
  295. void FileIO::ReadTracks(std::vector<core::TrackletPtr>& tracks, const std::string& file_name,
  296. char delimiter)
  297. {
  298. std::ifstream in(file_name, std::ios::in);
  299. if (!in.is_open())
  300. {
  301. util::Logger::LogError("Unable to open file: " + file_name);
  302. return;
  303. }
  304. std::string line;
  305. size_t line_index = 0;
  306. while (in.good() && !in.eof())
  307. {
  308. getline(in, line);
  309. if (line.size() == 0) continue;
  310. std::vector<std::string> parts = split(line, delimiter);
  311. while (tracks.size() < (parts.size() / 2))
  312. {
  313. tracks.push_back(core::TrackletPtr(new core::Tracklet()));
  314. }
  315. for (size_t i = 1; i < parts.size(); i += 2)
  316. {
  317. if (!parts[i - 1].empty() && !parts[i].empty())
  318. {
  319. cv::Point2d point(std::stod(parts[i - 1]), std::stod(parts[i]));
  320. tracks[(i - 1) / 2]->AddPathObject(
  321. core::ObjectData2DPtr(new core::ObjectData2D(line_index, point)));
  322. }
  323. }
  324. line_index++;
  325. }
  326. in.close();
  327. }
  328. std::vector<std::string> FileIO::split(const std::string& input, char delimiter)
  329. {
  330. std::vector<std::string> output;
  331. std::stringstream ss(input);
  332. std::string part;
  333. while (getline(ss, part, delimiter))
  334. {
  335. output.push_back(part);
  336. }
  337. return output;
  338. }
  339. }