main.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //
  2. // Created by wrede on 19.04.16.
  3. //
  4. #include "../core/DetectionSequence.h"
  5. #include "../util/FileIO.h"
  6. #include "../util/Parser.h"
  7. #include "../algo/TwoStage.h"
  8. #include "../algo/KShortestPaths.h"
  9. #include "../visual/Visualizer.h"
  10. #include "../util/Logger.h"
  11. #include "../core/ObjectDataAngular.h"
  12. #include <boost/program_options.hpp>
  13. void ReadInput(const std::string& input_file, core::DetectionSequence& sequence,
  14. double temporal_weight, double spatial_weight, double angular_weight)
  15. {
  16. util::Logger::LogInfo("Reading input");
  17. util::Vector3d values;
  18. util::FileIO::ReadCSV(values, input_file);
  19. util::Parser::ParseObjectDataAngular(values, sequence,
  20. temporal_weight,
  21. spatial_weight,
  22. angular_weight);
  23. if (util::Logger::IsDebugEnabled())
  24. {
  25. size_t sequence_object_count = 0;
  26. for (size_t i = 0; i < sequence.GetFrameCount(); i++)
  27. {
  28. sequence_object_count += sequence.GetObjectCount(i);
  29. }
  30. util::Logger::LogDebug("sequence object count " + std::to_string(sequence_object_count));
  31. }
  32. }
  33. struct
  34. {
  35. size_t iterations;
  36. size_t max_frame_skip;
  37. size_t max_tracklet_count;
  38. double penalty_value;
  39. } two_stage_params;
  40. void RunTwoStage(core::DetectionSequence& sequence, const std::string& output_file,
  41. const std::string& images_folder, bool display)
  42. {
  43. util::Logger::LogInfo("Running two-stage");
  44. algo::TwoStage two_stage(two_stage_params.max_frame_skip,
  45. two_stage_params.penalty_value,
  46. two_stage_params.max_tracklet_count);
  47. // Running the two stage graph algorithm
  48. DirectedGraph obj_graph;
  49. two_stage.CreateObjectGraph(obj_graph, sequence);
  50. // Run the tracklet creation at least once
  51. DirectedGraph tlt_graph_1, tlt_graph_2;
  52. two_stage.CreateTrackletGraph(obj_graph, tlt_graph_1,
  53. sequence.GetFrameCount());
  54. // Run the tracklet creation iteratively
  55. for (size_t i = 1; i < two_stage_params.iterations; ++i)
  56. {
  57. if (i % 2 == 0)
  58. {
  59. two_stage.CreateTrackletGraph(tlt_graph_2, tlt_graph_1,
  60. sequence.GetFrameCount());
  61. }
  62. else
  63. {
  64. two_stage.CreateTrackletGraph(tlt_graph_1, tlt_graph_2,
  65. sequence.GetFrameCount());
  66. }
  67. }
  68. // Extract tracklets and flatten tracklets
  69. std::vector<core::TrackletPtr> tracks;
  70. if (two_stage_params.iterations % 2 == 0)
  71. {
  72. two_stage.ExtractTracks(tlt_graph_2, two_stage_params.iterations - 1, tracks);
  73. }
  74. else
  75. {
  76. two_stage.ExtractTracks(tlt_graph_1, two_stage_params.iterations - 1, tracks);
  77. }
  78. // Interpolate tracks
  79. for (auto track : tracks)
  80. {
  81. track->InterpolateMissingFrames();
  82. }
  83. // Display the tracking data
  84. if (display)
  85. {
  86. util::Logger::LogInfo("Displaying data");
  87. visual::Visualizer vis;
  88. vis.Display(tracks, images_folder);
  89. }
  90. util::Logger::LogInfo("Finished");
  91. }
  92. void Run(int argc, char** argv)
  93. {
  94. // Algorithm independent values
  95. std::string input_file, output_file, images_folder, algorithm;
  96. bool display;
  97. // Input dependent variables
  98. double temporal_weight, spatial_weight, angular_weight;
  99. boost::program_options::options_description opts("Allowed options");
  100. opts.add_options()
  101. ("help",
  102. "produce help message")
  103. ("info",
  104. "if the program should show progress information")
  105. ("debug",
  106. "if the program should show debug messages")
  107. ("input-file,i",
  108. boost::program_options::value<std::string>(&input_file),
  109. "set detections file path")
  110. ("output-file,o",
  111. boost::program_options::value<std::string>(&output_file),
  112. "set the output file path")
  113. ("algorithm,a",
  114. boost::program_options::value<std::string>(&algorithm),
  115. "set the algorithm to use, current viable options: two-stage")
  116. ("display",
  117. "if a window with the images and the detected tracks should be opened")
  118. ("images-folder,f",
  119. boost::program_options::value<std::string>(&images_folder),
  120. "set images folder path")
  121. ("iterations",
  122. boost::program_options::value<size_t>(&two_stage_params.iterations)->default_value((2)),
  123. "(two-stage) number of tracklet extraction iterations")
  124. ("max-frame-skip",
  125. boost::program_options::value<size_t>(&two_stage_params.max_frame_skip)->default_value(1),
  126. "(two stage) set the maximum number of frames a track can skip between two detections,"
  127. " if set to less or equal than zero all frames are linked")
  128. ("max-tracklet-count",
  129. boost::program_options::value<size_t>(&two_stage_params.max_tracklet_count)->default_value(1),
  130. "(two stage) set the maximum number of tracklets to be extracted")
  131. ("penalty-value",
  132. boost::program_options::value<double>(&two_stage_params.penalty_value)->default_value(0.0),
  133. "(two stage) set the penalty value for edges from and to source and sink")
  134. ("temporal-weight",
  135. boost::program_options::value<double>(&temporal_weight)->default_value(1.0),
  136. "temporal weight for difference calculations between two detections")
  137. ("spatial-weight",
  138. boost::program_options::value<double>(&spatial_weight)->default_value(1.0),
  139. "spatial weight for difference calculations between two detections")
  140. ("angular-weight",
  141. boost::program_options::value<double>(&angular_weight)->default_value(1.0),
  142. "angular weight for difference calculations between two detections");
  143. boost::program_options::variables_map opt_var_map;
  144. #pragma clang diagnostic push
  145. #pragma clang diagnostic ignored "-Wincompatible-pointer-types-discards-qualifiers"
  146. boost::program_options::store(boost::program_options::parse_command_line(argc, argv, opts), opt_var_map);
  147. #pragma clang diagnostic pop
  148. boost::program_options::notify(opt_var_map);
  149. if (opt_var_map.count("help") != 0)
  150. {
  151. std::cout << opts << std::endl;
  152. exit(0);
  153. }
  154. if (opt_var_map.count("info") != 0)
  155. {
  156. util::Logger::SetInfo(true);
  157. util::Logger::LogInfo("Enabled");
  158. }
  159. if (opt_var_map.count("debug") != 0)
  160. {
  161. util::Logger::SetDebug(true);
  162. util::Logger::LogDebug("Enabled");
  163. }
  164. display = opt_var_map.count("display") != 0;
  165. core::DetectionSequence sequence;
  166. ReadInput(input_file, sequence, temporal_weight, spatial_weight, angular_weight);
  167. if (algorithm == "two-stage")
  168. {
  169. RunTwoStage(sequence, output_file, images_folder, display);
  170. }
  171. else
  172. {
  173. std::cout << opts << std::endl;
  174. exit(0);
  175. }
  176. }
  177. void CreateTestGraph(DirectedGraph& graph, Vertex& source, Vertex& sink)
  178. {
  179. // Create test graph (suurballe wikipedia example)
  180. // std::vector<Vertex> vertices;
  181. // for (size_t i = 0; i < 6; ++i)
  182. // {
  183. // vertices.push_back(
  184. // boost::add_vertex(
  185. // core::ObjectDataPtr(new core::ObjectData(i)),graph));
  186. // }
  187. //
  188. // // AB
  189. // boost::add_edge(vertices[0], vertices[1], 1.0, graph);
  190. //
  191. // // AC
  192. // boost::add_edge(vertices[0], vertices[2], 2.0, graph);
  193. //
  194. // // BD
  195. // boost::add_edge(vertices[1], vertices[3], 1.0, graph);
  196. //
  197. // // BE
  198. // boost::add_edge(vertices[1], vertices[4], 2.0, graph);
  199. //
  200. // // CD
  201. // boost::add_edge(vertices[2], vertices[3], 2.0, graph);
  202. //
  203. // // DF
  204. // boost::add_edge(vertices[3], vertices[5], 1.0, graph);
  205. //
  206. // // EF
  207. // boost::add_edge(vertices[4], vertices[5], 2.0, graph);
  208. //
  209. // source = vertices[0];
  210. // sink = vertices[5];
  211. // Create test graph (disjoint path finding example)
  212. std::vector<Vertex> vertices;
  213. for (size_t i = 0; i < 8; ++i)
  214. {
  215. vertices.push_back(
  216. boost::add_vertex(
  217. core::ObjectDataPtr(new core::ObjectData(i)),graph));
  218. }
  219. boost::add_edge(vertices[0], vertices[1], 1.0, graph);
  220. boost::add_edge(vertices[0], vertices[4], 1.0, graph);
  221. boost::add_edge(vertices[1], vertices[2], 1.0, graph);
  222. boost::add_edge(vertices[2], vertices[3], 1.0, graph);
  223. boost::add_edge(vertices[3], vertices[7], 1.0, graph);
  224. boost::add_edge(vertices[4], vertices[3], 1.0, graph);
  225. boost::add_edge(vertices[4], vertices[5], 3.0, graph);
  226. boost::add_edge(vertices[5], vertices[6], 1.0, graph);
  227. boost::add_edge(vertices[5], vertices[3], 1.0, graph);
  228. boost::add_edge(vertices[6], vertices[7], 1.0, graph);
  229. source = vertices[0];
  230. sink = vertices[7];
  231. }
  232. int main(int argc, char** argv)
  233. {
  234. Run(argc, argv);
  235. //TestTracklet();
  236. // Vertex source, sink;
  237. // DirectedGraph graph;
  238. //
  239. // util::Logger::SetDebug(true);
  240. // util::Logger::SetInfo(true);
  241. //
  242. // CreateTestGraph(graph, source, sink);
  243. //
  244. // algo::KShortestPaths ksp(graph, source, sink);
  245. // MultiPredecessorMap paths = ksp.Run(3);
  246. //
  247. // util::Logger::LogDebug("found paths:");
  248. // for (Vertex first : paths[sink])
  249. // {
  250. // std::string path;
  251. // path += std::to_string(sink) + "->" + std::to_string(first);
  252. //
  253. // for (Vertex u = first, v = (*paths[u].begin());
  254. // u != v; u = v, v = (*paths[v].begin()))
  255. // {
  256. // path += "->" + std::to_string(v);
  257. // }
  258. //
  259. // util::Logger::LogDebug(path);
  260. // }
  261. return 0;
  262. }