FileIO.cpp 12 KB

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