main.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  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/NStage.h"
  8. #include "../util/Visualizer.h"
  9. #include "../util/Logger.h"
  10. #include "../core/ObjectDataAngular.h"
  11. #include "../algo/Berclaz.h"
  12. #include "../algo/KShortestPaths.h"
  13. #include <boost/program_options.hpp>
  14. #include <boost/graph/named_function_params.hpp>
  15. #include <boost/graph/bellman_ford_shortest_paths.hpp>
  16. #include <iomanip>
  17. struct
  18. {
  19. std::string max_frame_skip;
  20. std::string max_tracklet_count;
  21. std::string penalty_value;
  22. double edge_weight_threshold;
  23. std::unordered_map<std::string, double> constraints;
  24. } n_stage_params;
  25. void RunNStage(core::DetectionSequence& sequence, std::vector<core::TrackletPtr>& tracks)
  26. {
  27. util::Logger::LogInfo("Running n-stage");
  28. std::vector<size_t> max_frame_skips;
  29. std::vector<double> penalty_values;
  30. std::vector<size_t> max_tracklet_counts;
  31. // Parse strings to vectors
  32. size_t d_index;
  33. std::string str, part;
  34. str = n_stage_params.max_frame_skip;
  35. do
  36. {
  37. d_index = str.find(",");
  38. part = str.substr(0, d_index);
  39. if (part.size() > 0)
  40. {
  41. max_frame_skips.push_back((unsigned long&&) atoi(part.c_str()));
  42. }
  43. str = str.substr(d_index + 1);
  44. }
  45. while (d_index != std::string::npos);
  46. str = n_stage_params.penalty_value;
  47. do
  48. {
  49. d_index = str.find(",");
  50. part = str.substr(0, d_index);
  51. if (part.size() > 0)
  52. {
  53. penalty_values.push_back(std::atof(part.c_str()));
  54. }
  55. str = str.substr(d_index + 1);
  56. }
  57. while (d_index != std::string::npos);
  58. str = n_stage_params.max_tracklet_count;
  59. do
  60. {
  61. d_index = str.find(",");
  62. part = str.substr(0, d_index);
  63. if (part.size() > 0)
  64. {
  65. max_tracklet_counts.push_back((unsigned long&&) atoi(part.c_str()));
  66. }
  67. str = str.substr(d_index + 1);
  68. }
  69. while (d_index != std::string::npos);
  70. // Init n-stage
  71. algo::NStage n_stage(max_frame_skips, penalty_values, max_tracklet_counts,
  72. n_stage_params.edge_weight_threshold, n_stage_params.constraints);
  73. n_stage.Run(sequence, tracks);
  74. // Interpolate tracks
  75. for (auto track : tracks)
  76. {
  77. track->InterpolateMissingFrames();
  78. }
  79. util::Logger::LogInfo("Finished");
  80. }
  81. struct
  82. {
  83. int h_res;
  84. int v_res;
  85. int vicinity_size;
  86. size_t batch_size;
  87. size_t max_track_count;
  88. util::Filter2D filter;
  89. } berclaz_params;
  90. void RunBerclaz(core::DetectionSequence & sequence, std::vector<core::TrackletPtr> & tracks)
  91. {
  92. util::Logger::LogInfo("Running berclaz");
  93. // Init berclaz
  94. algo::Berclaz berclaz(berclaz_params.h_res,
  95. berclaz_params.v_res,
  96. berclaz_params.vicinity_size);
  97. berclaz.Run(sequence,
  98. berclaz_params.batch_size,
  99. berclaz_params.max_track_count,
  100. tracks,
  101. berclaz_params.filter);
  102. util::Logger::LogInfo("Interpolate tracks");
  103. // Interpolate tracks
  104. for (auto track : tracks)
  105. {
  106. track->InterpolateMissingFrames();
  107. }
  108. util::Logger::LogInfo("Finished");
  109. }
  110. void Run(int argc, char const * const * argv)
  111. {
  112. //TODO output info for all possible constraints
  113. // Algorithm independent values
  114. std::string input_file, output_path, images_folder, algorithm, config_path, header;
  115. std::string input_format, berclaz_filter, n_stage_constraints;
  116. bool info, debug, display, output, output_images, show_grid;
  117. char input_delimiter, output_delimiter;
  118. double temporal_weight, spatial_weight, angular_weight, image_width, image_height;
  119. boost::program_options::options_description opts("Allowed options");
  120. opts.add_options()
  121. ("help",
  122. "produce help message")
  123. ("info",
  124. boost::program_options::value<bool>(&info)
  125. ->default_value(false),
  126. "if the program should show progress information")
  127. ("debug",
  128. boost::program_options::value<bool>(&debug)
  129. ->default_value(false),
  130. "if the program should show debug messages")
  131. ("display.enabled",
  132. boost::program_options::value<bool>(&display)
  133. ->default_value(false),
  134. "if a window with the images and the detected tracks should be opened")
  135. ("display.show-grid",
  136. boost::program_options::value<bool>(&show_grid)
  137. ->default_value(false),
  138. "if a grid should be shown in the visualized tracks (will only be shown if a resolution is given)")
  139. ("config",
  140. boost::program_options::value<std::string>(&config_path),
  141. "the path to the config file, if no path is given the command line arguments are read")
  142. ("input.file",
  143. boost::program_options::value<std::string>(&input_file),
  144. "set detections file path")
  145. ("output.enabled",
  146. boost::program_options::value<bool>(&output)
  147. ->default_value(false),
  148. "if the results should be written into the specified output folder")
  149. ("output.images",
  150. boost::program_options::value<bool>(&output_images)
  151. ->default_value(false),
  152. "if the images containing the visualized detections should be written to the output")
  153. ("output.path",
  154. boost::program_options::value<std::string>(&output_path),
  155. "set the output file path")
  156. ("output.delimiter",
  157. boost::program_options::value<char>(&output_delimiter)
  158. ->default_value(';'),
  159. "the delimiter used to separate values in the specified output file")
  160. ("input.images-folder",
  161. boost::program_options::value<std::string>(&images_folder),
  162. "set images folder path")
  163. ("input.header",
  164. boost::program_options::value<std::string>(&header),
  165. "sets the input header, this value is optional if the input file has a header labeling the values,"
  166. "the delimiter used for the header needs to be the same as for the rest of the file")
  167. ("input.format",
  168. boost::program_options::value<std::string>(&input_format)
  169. ->default_value("ObjectData"),
  170. "the format the input should be parsed into, valid formats are: "
  171. "2D, Box, Angular")
  172. ("input.delimiter",
  173. boost::program_options::value<char>(&input_delimiter)
  174. ->default_value(';'),
  175. "the delimiter used to separate values in the specified input file")
  176. ("input.image-width",
  177. boost::program_options::value<double>(&image_width)
  178. ->default_value(1.0),
  179. "the width of the image")
  180. ("input.image-height",
  181. boost::program_options::value<double>(&image_height)
  182. ->default_value(1.0),
  183. "the height of the image")
  184. ("algorithm",
  185. boost::program_options::value<std::string>(&algorithm),
  186. "set the algorithm to use, current viable options: n-stage berclaz")
  187. ("n-stage.max-frame-skip",
  188. boost::program_options::value<std::string>(&n_stage_params.max_frame_skip)
  189. ->default_value("1,1"),
  190. "(n-stage) set the maximum number of frames a track can skip between two detections,"
  191. " if set to less or equal than zero all frames are linked")
  192. ("n-stage.max-tracklet-count",
  193. boost::program_options::value<std::string>(&n_stage_params.max_tracklet_count)
  194. ->default_value("-1,1"),
  195. "(n-stage) set the maximum number of tracklets to be extracted")
  196. ("n-stage.penalty-value",
  197. boost::program_options::value<std::string>(&n_stage_params.penalty_value)
  198. ->default_value("0,0"),
  199. "(n-stage) set the penalty value for edges from and to source and sink")
  200. ("n-stage.temporal-weight",
  201. boost::program_options::value<double>(&temporal_weight)
  202. ->default_value(1.0),
  203. "(n-stage) temporal weight for difference calculations between two detections")
  204. ("n-stage.spatial-weight",
  205. boost::program_options::value<double>(&spatial_weight)
  206. ->default_value(1.0),
  207. "(n-stage) spatial weight for difference calculations between two detections")
  208. ("n-stage.angular-weight",
  209. boost::program_options::value<double>(&angular_weight)
  210. ->default_value(1.0),
  211. "(n-stage) angular weight for difference calculations between two detections")
  212. ("n-stage.edge-weight-threshold",
  213. boost::program_options::value<double>(&n_stage_params.edge_weight_threshold)
  214. ->default_value(1.0),
  215. "(n-stage) the maximum weight an edge can have in the initial graph, edges with"
  216. "higher edge weights are discarded")
  217. ("n-stage.constraints",
  218. boost::program_options::value<std::string>(&n_stage_constraints)
  219. ->default_value(""),
  220. "(n-stage) the constraints to ensure when creating the object graph,"
  221. " values and keys are separated by commas,"
  222. " for example: key0,value0,key1,value1")
  223. ("berclaz.h-res",
  224. boost::program_options::value<int>(&berclaz_params.h_res)
  225. ->default_value(10),
  226. "(berclaz) the number of horizontal grid cells")
  227. ("berclaz.v-res",
  228. boost::program_options::value<int>(&berclaz_params.v_res)
  229. ->default_value(10),
  230. "(berclaz) the number of vertical grid cells")
  231. ("berclaz.vicinity-size",
  232. boost::program_options::value<int>(&berclaz_params.vicinity_size)
  233. ->default_value(1),
  234. "(berclaz) the vicinity size, the number of cells a detection can travel between two frames")
  235. ("berclaz.max-track-count",
  236. boost::program_options::value<size_t>(&berclaz_params.max_track_count)
  237. ->default_value(1),
  238. "(berclaz) the maximal number of tracks to extract")
  239. ("berclaz.batch-size",
  240. boost::program_options::value<size_t>(&berclaz_params.batch_size)
  241. ->default_value(100),
  242. "(berclaz) the size of one processing batch")
  243. ("berclaz.filter",
  244. boost::program_options::value<std::string>(&berclaz_filter)
  245. ->default_value(""),
  246. "(berclaz) the filter used in the berclaz grid (comma separated),"
  247. " e.g. multiplier, m01, m02,...,mnn");
  248. boost::program_options::variables_map opt_var_map;
  249. boost::program_options::store(
  250. boost::program_options::parse_command_line(argc, argv, opts),
  251. opt_var_map);
  252. boost::program_options::notify(opt_var_map);
  253. // Display help
  254. if (opt_var_map.count("help") != 0)
  255. {
  256. std::cout << opts << std::endl;
  257. exit(0);
  258. }
  259. // Read config
  260. if (opt_var_map.count("config") != 0)
  261. {
  262. std::ifstream config_file(config_path, std::ifstream::in);
  263. if (config_file.is_open())
  264. {
  265. boost::program_options::store(
  266. boost::program_options::parse_config_file(config_file,
  267. opts),
  268. opt_var_map);
  269. config_file.close();
  270. boost::program_options::notify(opt_var_map);
  271. }
  272. else
  273. {
  274. util::Logger::LogError("Unable to open config file!");
  275. exit(0);
  276. }
  277. }
  278. else if (opt_var_map.count("input-file") == 0 ||
  279. opt_var_map.count("input-format") == 0 ||
  280. (opt_var_map.count("output-path") == 0 && (output || output_images)))
  281. {
  282. std::cout << opts << std::endl;
  283. exit(0);
  284. }
  285. // Enable info logging
  286. if (info != 0)
  287. {
  288. util::Logger::SetInfo(true);
  289. util::Logger::LogInfo("Enabled");
  290. }
  291. // Enable debug logging
  292. if (debug != 0)
  293. {
  294. util::Logger::SetDebug(true);
  295. util::Logger::LogDebug("Enabled");
  296. }
  297. // Reading the input file
  298. util::Logger::LogInfo("Read input");
  299. util::ValueMapVector values;
  300. try
  301. {
  302. if (header.size() > 0)
  303. {
  304. util::Logger::LogDebug("header specified");
  305. util::FileIO::ReadCSV(values, header, input_file, input_delimiter);
  306. }
  307. else
  308. {
  309. util::Logger::LogDebug("read header from file");
  310. util::FileIO::ReadCSV(values, input_file, input_delimiter);
  311. }
  312. }
  313. catch (std::exception& e)
  314. {
  315. util::Logger::LogError("Failed to read input file!");
  316. util::Logger::LogError(e.what());
  317. exit(0);
  318. }
  319. // Parsing the read input
  320. core::DetectionSequence sequence;
  321. if (input_format == "2D")
  322. {
  323. util::Parser::ParseObjectData2D(values,
  324. sequence,
  325. image_width,
  326. image_height,
  327. temporal_weight,
  328. spatial_weight);
  329. }
  330. else if (input_format == "Box")
  331. {
  332. util::Parser::ParseObjectDataBox(values,
  333. sequence,
  334. image_width,
  335. image_height,
  336. temporal_weight,
  337. spatial_weight);
  338. }
  339. else if (input_format == "Angular")
  340. {
  341. util::Parser::ParseObjectDataAngular(values,
  342. sequence,
  343. image_width,
  344. image_height,
  345. temporal_weight,
  346. spatial_weight,
  347. angular_weight);
  348. }
  349. else
  350. {
  351. // No valid input-format specified
  352. std::cout << opts << std::endl;
  353. exit(0);
  354. }
  355. // Running the specified algorithm
  356. std::vector<core::TrackletPtr> tracks;
  357. time_t begin_time, end_time;
  358. std::string output_file_name = algorithm + "_";
  359. util::Logger::LogInfo("Start time measurement");
  360. begin_time = time(0);
  361. if (algorithm == "n-stage")
  362. {
  363. //TODO constraints for every stage not just the first
  364. // Parse the constraints
  365. std::vector<std::string> pairs = util::FileIO::Split(n_stage_constraints, ',');
  366. for (size_t i = 0; i < pairs.size(); ++i) {
  367. n_stage_params.constraints[pairs[i]] = stod(pairs[i + 1]);
  368. }
  369. //TODO set the output file name
  370. RunNStage(sequence, tracks);
  371. }
  372. else if (algorithm == "berclaz")
  373. {
  374. output_file_name += std::to_string(berclaz_params.h_res) + "x"
  375. + std::to_string(berclaz_params.v_res) + "_"
  376. + std::to_string(berclaz_params.vicinity_size) + "_"
  377. + std::to_string(berclaz_params.max_track_count) + "_"
  378. + std::to_string(berclaz_params.batch_size);
  379. berclaz_params.filter = util::Filter2D(berclaz_filter, ',');
  380. RunBerclaz(sequence, tracks);
  381. }
  382. else
  383. {
  384. // No valid algorithm specified
  385. std::cout << opts << std::endl;
  386. exit(0);
  387. }
  388. output_file_name += ".csv";
  389. end_time = time(0);
  390. util::Logger::LogInfo("Time measurement stopped");
  391. util::Logger::LogInfo("Time passed: "
  392. + std::to_string(difftime(end_time, begin_time) / 60.0)
  393. + " minutes");
  394. // Write the output file
  395. if (output)
  396. {
  397. util::FileIO::WriteTracks(tracks, output_path + "/" + output_file_name, output_delimiter);
  398. }
  399. // Display the tracking data
  400. if (display)
  401. {
  402. util::Visualizer vis;
  403. if (algorithm == "berclaz")
  404. vis.Display(tracks, sequence.GetFrameOffset(),
  405. images_folder, output_images, output_path, "Visualizer",
  406. 0, 24, show_grid, berclaz_params.h_res, berclaz_params.v_res);
  407. else
  408. vis.Display(tracks, sequence.GetFrameOffset(),
  409. images_folder, output_images, output_path);
  410. }
  411. }
  412. void CreateTestGraph(DirectedGraph& graph, Vertex& source, Vertex& sink)
  413. {
  414. // Create test graph (suurballe wikipedia example)
  415. // std::vector<Vertex> vertices;
  416. // for (size_t i = 0; i < 6; ++i)
  417. // {
  418. // vertices.push_back(
  419. // boost::add_vertex(
  420. // core::ObjectDataPtr(new core::ObjectData(i)),graph));
  421. // }
  422. //
  423. // // AB
  424. // boost::add_edge(vertices[0], vertices[1], 1.0, graph);
  425. // boost::add_edge(vertices[1], vertices[0], 1.0, graph);
  426. //
  427. // // AC
  428. // boost::add_edge(vertices[0], vertices[2], 2.0, graph);
  429. // boost::add_edge(vertices[2], vertices[0], 2.0, graph);
  430. //
  431. // // BD
  432. // boost::add_edge(vertices[1], vertices[3], 1.0, graph);
  433. // boost::add_edge(vertices[3], vertices[1], 1.0, graph);
  434. //
  435. // // BE
  436. // boost::add_edge(vertices[1], vertices[4], 2.0, graph);
  437. // boost::add_edge(vertices[4], vertices[1], 2.0, graph);
  438. //
  439. // // CD
  440. // boost::add_edge(vertices[2], vertices[3], 2.0, graph);
  441. // boost::add_edge(vertices[3], vertices[2], 2.0, graph);
  442. //
  443. // // DF
  444. // boost::add_edge(vertices[3], vertices[5], 1.0, graph);
  445. // boost::add_edge(vertices[5], vertices[3], 1.0, graph);
  446. //
  447. // // EF
  448. // boost::add_edge(vertices[4], vertices[5], 2.0, graph);
  449. // boost::add_edge(vertices[5], vertices[4], 2.0, graph);
  450. //
  451. // source = vertices[0];
  452. // sink = vertices[5];
  453. // Create test graph
  454. std::vector<Vertex> vertices;
  455. for (size_t i = 0; i < 11; ++i)
  456. {
  457. vertices.push_back(
  458. boost::add_vertex(
  459. core::ObjectDataPtr(new core::ObjectData(i)),graph));
  460. }
  461. // boost::add_edge(vertices[0], vertices[1], 0.0, graph);
  462. // boost::add_edge(vertices[0], vertices[8], 0.0, graph);
  463. // boost::add_edge(vertices[0], vertices[4], 0.0, graph);
  464. // boost::add_edge(vertices[1], vertices[2], -1.0, graph);
  465. // boost::add_edge(vertices[1], vertices[5], -1.0, graph);
  466. // boost::add_edge(vertices[2], vertices[3], -1.0, graph);
  467. // boost::add_edge(vertices[2], vertices[6], -1.0, graph);
  468. // boost::add_edge(vertices[2], vertices[10], -1.0, graph);
  469. // boost::add_edge(vertices[3], vertices[7], 4.0, graph);
  470. // boost::add_edge(vertices[4], vertices[2], 1.0, graph);
  471. // boost::add_edge(vertices[4], vertices[5], 1.0, graph);
  472. // boost::add_edge(vertices[4], vertices[9], 1.0, graph);
  473. // boost::add_edge(vertices[5], vertices[6], 2.0, graph);
  474. // boost::add_edge(vertices[5], vertices[3], 2.0, graph);
  475. // boost::add_edge(vertices[6], vertices[7], 4.0, graph);
  476. // boost::add_edge(vertices[8], vertices[2], -3.0, graph);
  477. // boost::add_edge(vertices[8], vertices[9], -3.0, graph);
  478. // boost::add_edge(vertices[9], vertices[3], 3.0, graph);
  479. // boost::add_edge(vertices[9], vertices[10], 3.0, graph);
  480. // boost::add_edge(vertices[10], vertices[7], 4.0, graph);
  481. source = vertices[0];
  482. sink = vertices[10];
  483. for (int i = 1; i < vertices.size() - 1; ++i)
  484. {
  485. boost::add_edge(source, vertices[i], 0.0, graph);
  486. }
  487. boost::add_edge(vertices[1], vertices[4], -1.0, graph);
  488. boost::add_edge(vertices[1], vertices[5], -1.0, graph);
  489. boost::add_edge(vertices[1], vertices[10], 0.0, graph);
  490. boost::add_edge(vertices[4], vertices[7], -1.0, graph);
  491. boost::add_edge(vertices[4], vertices[8], -1.0, graph);
  492. boost::add_edge(vertices[4], vertices[10], 0.0, graph);
  493. boost::add_edge(vertices[7], vertices[10], -1.0, graph);
  494. boost::add_edge(vertices[2], vertices[4], -2.0, graph);
  495. boost::add_edge(vertices[2], vertices[5], -2.0, graph);
  496. boost::add_edge(vertices[2], vertices[6], -2.0, graph);
  497. boost::add_edge(vertices[2], vertices[10], 0.0, graph);
  498. boost::add_edge(vertices[5], vertices[7], -2.0, graph);
  499. boost::add_edge(vertices[5], vertices[8], -2.0, graph);
  500. boost::add_edge(vertices[5], vertices[9], -2.0, graph);
  501. boost::add_edge(vertices[5], vertices[10], 0.0, graph);
  502. boost::add_edge(vertices[8], vertices[10], -2.0, graph);
  503. boost::add_edge(vertices[3], vertices[5], -3.0, graph);
  504. boost::add_edge(vertices[3], vertices[6], -3.0, graph);
  505. boost::add_edge(vertices[3], vertices[10], 0.0, graph);
  506. boost::add_edge(vertices[6], vertices[8], -3.0, graph);
  507. boost::add_edge(vertices[6], vertices[9], -3.0, graph);
  508. boost::add_edge(vertices[6], vertices[10], 0.0, graph);
  509. boost::add_edge(vertices[9], vertices[10], -3.0, graph);
  510. // Connect all with source and sink
  511. // boost::add_edge(vertices[1], sink, 0, graph);
  512. // boost::add_edge(source, vertices[2], 0, graph);
  513. // boost::add_edge(vertices[2], sink, 0, graph);
  514. // boost::add_edge(source, vertices[3], 0, graph);
  515. // boost::add_edge(vertices[4], sink, 0, graph);
  516. // boost::add_edge(source, vertices[5], 0, graph);
  517. // boost::add_edge(vertices[5], sink, 0, graph);
  518. // boost::add_edge(source, vertices[6], 0, graph);
  519. // boost::add_edge(vertices[8], sink, 0, graph);
  520. // boost::add_edge(source, vertices[9], 0, graph);
  521. // boost::add_edge(vertices[9], sink, 0, graph);
  522. // boost::add_edge(source, vertices[10], 0, graph);
  523. // boost::add_edge(vertices[1], vertices[7], 0.0, graph);
  524. // boost::add_edge(vertices[8], vertices[7], 0.0, graph);
  525. }
  526. void TestKBellmanFord(DirectedGraph graph, Vertex source, Vertex sink, size_t n_paths)
  527. {
  528. util::FileIO::WriteCSVMatlab(graph, "/home/wrede/Dokumente/graph_kbf.csv");
  529. MultiPredecessorMap paths;
  530. for (size_t i = 0; i < n_paths; ++i)
  531. {
  532. // Prepare variables for path finding
  533. size_t graph_size = boost::num_vertices(graph);
  534. std::vector<Vertex> pred_list(graph_size);
  535. std::vector<double> dist_list(graph_size);
  536. VertexIndexMap graph_indices = boost::get(boost::vertex_index, graph);
  537. EdgeWeightMap weight_map = boost::get(boost::edge_weight, graph);
  538. PredecessorMap pred_map(&pred_list[0], graph_indices);
  539. DistanceMap dist_map(&dist_list[0], graph_indices);
  540. // Find the shortest path
  541. boost::bellman_ford_shortest_paths(graph, graph_size,
  542. boost::root_vertex(source)
  543. .weight_map(weight_map)
  544. .predecessor_map(pred_map)
  545. .distance_map(dist_map));
  546. // Add path
  547. for (Vertex u = sink, v = pred_map[u]; u != v; u = v, v = pred_map[v])
  548. {
  549. paths[u].insert(v);
  550. if (u != sink && u != source)
  551. boost::clear_out_edges(u, graph);
  552. }
  553. }
  554. util::FileIO::WriteCSVMatlab(paths, source, sink, "/home/wrede/Dokumente/paths_kbf.csv");
  555. }
  556. void TestGrid()
  557. {
  558. int lower_index = 0;
  559. int upper_index = 5;
  560. double lower_bound = 0.0;
  561. double upper_bound = 50.0;
  562. util::Grid grid(upper_index, upper_index, upper_index,
  563. upper_bound, upper_bound, upper_bound);
  564. std::uniform_int_distribution<int> unii(lower_index, upper_index - 1);
  565. std::uniform_real_distribution<double> unif(lower_bound, upper_bound);
  566. std::default_random_engine re;
  567. // Fill with empty values
  568. std::cout << "fill with empty values\n";
  569. for (int z = lower_index; z < upper_index; ++z)
  570. {
  571. for (int y = lower_index; y < upper_index; ++y)
  572. {
  573. for (int x = lower_index; y < upper_index; ++y)
  574. {
  575. grid.SetValue(nullptr, x, y, z);
  576. }
  577. }
  578. }
  579. // Randomly add data
  580. std::cout << "randomly add data\n";
  581. for (int i = 0; i < 10; ++i)
  582. {
  583. int xi = unii(re);
  584. int yi = unii(re);
  585. int zi = unii(re);
  586. core::ObjectDataPtr value(new core::ObjectData((size_t)i));
  587. grid.SetValue(value, xi, yi, zi);
  588. std::cout << xi << "," << yi << "," << zi << " = " << *value << std::endl;
  589. }
  590. // Randomly get data
  591. std::cout << "randomly get data\n";
  592. for (int i = 0; i < 10; ++i)
  593. {
  594. double x = unif(re);
  595. double y = unif(re);
  596. double z = unif(re);
  597. std::cout << x << "," << y << "," << z << " = ";
  598. core::ObjectDataPtr value = grid.GetValue(x, y, z);
  599. if (value)
  600. {
  601. std::cout << *value << std::endl;
  602. }
  603. else
  604. {
  605. std::cout << "nullptr" << std::endl;
  606. }
  607. }
  608. }
  609. void CreateBerclazGraph(DirectedGraph& graph, Vertex& source, Vertex& sink)
  610. {
  611. util::Logger::SetDebug(true);
  612. util::Logger::SetInfo(true);
  613. util::Logger::LogInfo("Test berclaz graph");
  614. // Init grid with data
  615. util::Grid grid(3, 3, 3, 9.0, 9.0, 9.0);
  616. for (int z = 0; z < grid.GetDepthCount(); ++z)
  617. {
  618. for (int y = 0; y < grid.GetHeightCount(); ++y)
  619. {
  620. for (int x = 0; x < grid.GetWidthCount(); ++x)
  621. {
  622. core::ObjectDataPtr value(new core::ObjectData(10));
  623. grid.SetValue(value, x, y, z);
  624. }
  625. }
  626. }
  627. // Add path source->0,0,0->0,0,1->0,0,2->sink
  628. core::ObjectDataPtr value0(new core::ObjectData(1));
  629. value0->SetDetectionScore(1.0);
  630. grid.SetValue(value0, 0, 0, 0);
  631. core::ObjectDataPtr value1(new core::ObjectData(2));
  632. value1->SetDetectionScore(1.0);
  633. grid.SetValue(value1, 0, 0, 1);
  634. core::ObjectDataPtr value2(new core::ObjectData(3));
  635. value2->SetDetectionScore(1.0);
  636. grid.SetValue(value2, 0, 0, 2);
  637. // Add path source->0,1,0->0,1,1->0,1,2->sink
  638. core::ObjectDataPtr value3(new core::ObjectData(4));
  639. value3->SetDetectionScore(0.6);
  640. grid.SetValue(value3, 0, 1, 0);
  641. core::ObjectDataPtr value4(new core::ObjectData(5));
  642. value4->SetDetectionScore(0.6);
  643. grid.SetValue(value4, 0, 1, 1);
  644. core::ObjectDataPtr value5(new core::ObjectData(6));
  645. value5->SetDetectionScore(0.6);
  646. grid.SetValue(value5, 0, 1, 2);
  647. // Add path source->0,2,0->0,2,1->0,2,2->sink
  648. core::ObjectDataPtr value6(new core::ObjectData(7));
  649. value6->SetDetectionScore(0.55);
  650. grid.SetValue(value6, 0, 2, 0);
  651. core::ObjectDataPtr value7(new core::ObjectData(8));
  652. value7->SetDetectionScore(0.55);
  653. grid.SetValue(value7, 0, 2, 1);
  654. core::ObjectDataPtr value8(new core::ObjectData(9));
  655. value8->SetDetectionScore(0.55);
  656. grid.SetValue(value8, 0, 2, 2);
  657. util::Logger::LogDebug("add vertices");
  658. // Add grid vertices
  659. graph.clear();
  660. for (int z = 0; z < grid.GetDepthCount(); ++z)
  661. {
  662. for (int y = 0; y < grid.GetHeightCount(); ++y)
  663. {
  664. for (int x = 0; x < grid.GetWidthCount(); ++x)
  665. {
  666. boost::add_vertex(grid.GetValue(x, y, z), graph);
  667. }
  668. }
  669. }
  670. util::Logger::LogDebug("vertex count " + std::to_string(boost::num_vertices(graph)));
  671. util::Logger::LogDebug("edge count " + std::to_string(boost::num_edges(graph)));
  672. // Add source and sink vertex
  673. source = boost::add_vertex(core::ObjectDataPtr(new core::ObjectData()), graph);
  674. sink = boost::add_vertex(core::ObjectDataPtr(new core::ObjectData()), graph);
  675. util::Logger::LogDebug("add edges");
  676. // Iterate all vertices but source and sink
  677. VertexIndexMap vertices = boost::get(boost::vertex_index, graph);
  678. VertexValueMap values = boost::get(boost::vertex_name, graph);
  679. int vicinity_size = 1;
  680. int layer_size = grid.GetWidthCount() * grid.GetHeightCount();
  681. for (int z = 0; z < grid.GetDepthCount(); ++z)
  682. {
  683. for (int y = 0; y < grid.GetHeightCount(); ++y)
  684. {
  685. for (int x = 0; x < grid.GetWidthCount(); ++x)
  686. {
  687. // First vertex index
  688. int vi = x + y * grid.GetHeightCount() + z * layer_size;
  689. // Get the score, clamp it, prevent division by zero and
  690. // logarithm of zero
  691. double score = values[vi]->GetDetectionScore();
  692. if (score > 0.999999)
  693. {
  694. score = 0.999999;
  695. }
  696. else if (score < 0.000001)
  697. {
  698. score = 0.000001;
  699. }
  700. // Calculate the edge weight
  701. double weight = -std::log(score / (1 - score));
  702. // Connect with the next frame only if there is a next frame
  703. if (z < grid.GetDepthCount() - 1)
  704. {
  705. // Iterate all nearby cells in the next frame
  706. for (int ny = std::max(0, y - vicinity_size);
  707. ny < std::min(grid.GetHeightCount(), y + vicinity_size + 1);
  708. ++ny)
  709. {
  710. for (int nx = std::max(0, x - vicinity_size);
  711. nx < std::min(grid.GetWidthCount(), x + vicinity_size + 1);
  712. ++nx)
  713. {
  714. // Second vertex index
  715. int vj = nx + ny * grid.GetHeightCount() + (z + 1) * layer_size;
  716. // Connect to nearby cells
  717. boost::add_edge(vertices[vi], vertices[vj], weight, graph);
  718. }
  719. }
  720. // boost::add_edge(vertices[vi], sink, 0.0, graph);
  721. }
  722. else
  723. {
  724. boost::add_edge(vertices[vi], sink, weight, graph);
  725. }
  726. if (z < 1)
  727. {
  728. // Connect with source
  729. boost::add_edge(source, vertices[vi], 0.0, graph);
  730. }
  731. }
  732. }
  733. }
  734. util::Logger::LogDebug("vertex count " + std::to_string(boost::num_vertices(graph)));
  735. util::Logger::LogDebug("edge count " + std::to_string(boost::num_edges(graph)));
  736. }
  737. void CreatePresentationGraph(DirectedGraph& graph, Vertex& source, Vertex& sink, bool two_paths)
  738. {
  739. std::vector<Vertex> vertices;
  740. if (two_paths)
  741. {
  742. for (size_t i = 0; i < 10; ++i)
  743. {
  744. vertices.push_back(
  745. boost::add_vertex(core::ObjectDataPtr(new core::ObjectData(i)), graph));
  746. }
  747. source = vertices[0];
  748. sink = vertices[9];
  749. boost::add_edge(vertices[0], vertices[1], 1.0, graph);
  750. boost::add_edge(vertices[0], vertices[2], 1.0, graph);
  751. boost::add_edge(vertices[1], vertices[3], 12.0, graph);
  752. boost::add_edge(vertices[1], vertices[4], 15.0, graph);
  753. boost::add_edge(vertices[2], vertices[3], 15.0, graph);
  754. boost::add_edge(vertices[2], vertices[4], 10.0, graph);
  755. boost::add_edge(vertices[3], vertices[5], 15.0, graph);
  756. boost::add_edge(vertices[3], vertices[6], 12.0, graph);
  757. boost::add_edge(vertices[4], vertices[5], 12.0, graph);
  758. boost::add_edge(vertices[4], vertices[6], 11.0, graph);
  759. boost::add_edge(vertices[5], vertices[7], 12.0, graph);
  760. boost::add_edge(vertices[5], vertices[8], 12.0, graph);
  761. boost::add_edge(vertices[6], vertices[7], 11.0, graph);
  762. boost::add_edge(vertices[6], vertices[8], 10.0, graph);
  763. boost::add_edge(vertices[7], vertices[9], 1.0, graph);
  764. boost::add_edge(vertices[8], vertices[9], 1.0, graph);
  765. }
  766. else
  767. {
  768. for (size_t i = 0; i < 14; ++i)
  769. {
  770. vertices.push_back(
  771. boost::add_vertex(core::ObjectDataPtr(new core::ObjectData(i)), graph));
  772. }
  773. source = vertices[0];
  774. sink = vertices[9];
  775. boost::add_edge(vertices[0], vertices[1], 1.0, graph);
  776. boost::add_edge(vertices[0], vertices[2], 1.0, graph);
  777. boost::add_edge(vertices[1], vertices[3], 12.0, graph);
  778. boost::add_edge(vertices[1], vertices[4], 15.0, graph);
  779. boost::add_edge(vertices[2], vertices[3], 15.0, graph);
  780. boost::add_edge(vertices[2], vertices[4], 10.0, graph);
  781. boost::add_edge(vertices[3], vertices[5], 15.0, graph);
  782. boost::add_edge(vertices[3], vertices[6], 12.0, graph);
  783. boost::add_edge(vertices[4], vertices[5], 12.0, graph);
  784. boost::add_edge(vertices[4], vertices[6], 11.0, graph);
  785. boost::add_edge(vertices[5], vertices[7], 12.0, graph);
  786. boost::add_edge(vertices[5], vertices[8], 12.0, graph);
  787. boost::add_edge(vertices[6], vertices[7], 11.0, graph);
  788. boost::add_edge(vertices[6], vertices[8], 10.0, graph);
  789. boost::add_edge(vertices[7], vertices[9], 1.0, graph);
  790. boost::add_edge(vertices[8], vertices[9], 1.0, graph);
  791. boost::add_edge(vertices[0], vertices[10], 20.0, graph);
  792. boost::add_edge(vertices[10], vertices[11], 20.0, graph);
  793. boost::add_edge(vertices[10], vertices[3], 20.0, graph);
  794. boost::add_edge(vertices[10], vertices[4], 20.0, graph);
  795. boost::add_edge(vertices[11], vertices[12], 20.0, graph);
  796. boost::add_edge(vertices[11], vertices[5], 20.0, graph);
  797. boost::add_edge(vertices[12], vertices[6], 20.0, graph);
  798. boost::add_edge(vertices[13], vertices[9], 20.0, graph);
  799. }
  800. }
  801. void CreateSuurballeGraph(DirectedGraph& graph, Vertex& source, Vertex& sink, bool first)
  802. {
  803. std::vector<Vertex> vertices;
  804. if (first)
  805. {
  806. // First example graph
  807. for (int i = 0; i < 7; ++i)
  808. {
  809. vertices.push_back(boost::add_vertex(graph));
  810. }
  811. source = vertices[0];
  812. sink = vertices[6];
  813. boost::add_edge(vertices[0], vertices[1], 5.0, graph);
  814. boost::add_edge(vertices[0], vertices[4], 2.0, graph);
  815. boost::add_edge(vertices[1], vertices[2], 1.0, graph);
  816. boost::add_edge(vertices[1], vertices[4], 1.0, graph);
  817. boost::add_edge(vertices[2], vertices[6], 1.0, graph);
  818. boost::add_edge(vertices[3], vertices[2], 1.0, graph);
  819. boost::add_edge(vertices[4], vertices[3], 2.0, graph);
  820. boost::add_edge(vertices[4], vertices[5], 1.0, graph);
  821. boost::add_edge(vertices[5], vertices[2], 1.0, graph);
  822. boost::add_edge(vertices[5], vertices[6], 1.0, graph);
  823. }
  824. else
  825. {
  826. // Second example graph
  827. for (int i = 0; i < 8; ++i)
  828. {
  829. vertices.push_back(boost::add_vertex(graph));
  830. }
  831. source = vertices[0];
  832. sink = vertices[7];
  833. boost::add_edge(vertices[0], vertices[1], 1.0, graph);
  834. boost::add_edge(vertices[0], vertices[4], 8.0, graph);
  835. boost::add_edge(vertices[0], vertices[5], 1.0, graph);
  836. boost::add_edge(vertices[1], vertices[2], 1.0, graph);
  837. boost::add_edge(vertices[1], vertices[7], 8.0, graph);
  838. boost::add_edge(vertices[2], vertices[3], 1.0, graph);
  839. boost::add_edge(vertices[3], vertices[4], 1.0, graph);
  840. boost::add_edge(vertices[3], vertices[6], 2.0, graph);
  841. boost::add_edge(vertices[4], vertices[7], 1.0, graph);
  842. boost::add_edge(vertices[5], vertices[2], 2.0, graph);
  843. boost::add_edge(vertices[5], vertices[6], 6.0, graph);
  844. boost::add_edge(vertices[6], vertices[7], 1.0, graph);
  845. }
  846. }
  847. void TestYAOKSP()
  848. {
  849. Vertex source, sink;
  850. DirectedGraph graph;
  851. // CreatePresentationGraph(graph, source, sink, true);
  852. CreateSuurballeGraph(graph, source, sink, false);
  853. // CreateBerclazGraph(graph, source, sink);
  854. algo::KShortestPaths ksp(graph, source, sink);
  855. ksp.Run(3);
  856. std::vector<std::vector<Vertex>> paths;
  857. ksp.GetPaths(paths);
  858. for (auto path : paths)
  859. {
  860. std::cout << "path: ";
  861. for (auto v : path)
  862. {
  863. std::cout << std::setw(2) << v << " ";
  864. }
  865. std::cout << std::endl;
  866. }
  867. }
  868. int main(int argc, char const * const * argv)
  869. {
  870. //TODO load with frame offset
  871. //TODO check visualizer for offset errors
  872. //TODO check why SEGFAULT is caused by some berclaz resolutions
  873. Run(argc, argv);
  874. return 0;
  875. }