Parser.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //
  2. // Created by wrede on 22.04.16.
  3. //
  4. #include "Parser.h"
  5. #include "MyMath.h"
  6. #include "Logger.h"
  7. #include "../core/ObjectDataAngular.h"
  8. #include "../core/ObjectDataBox.h"
  9. namespace util
  10. {
  11. //TODO rename if necessary
  12. const std::string Parser::KEY_FRAME = "frame";
  13. const std::string Parser::KEY_ID = "id";
  14. const std::string Parser::KEY_SCORE = "score";
  15. const std::string Parser::KEY_X = "x";
  16. const std::string Parser::KEY_Y = "y";
  17. const std::string Parser::KEY_WIDTH = "width";
  18. const std::string Parser::KEY_HEIGHT = "height";
  19. const std::string Parser::KEY_ANGLE = "angle";
  20. void Parser::ParseObjectData2D(ValueMapVector& values,
  21. core::DetectionSequence& sequence,
  22. double image_width, double image_height,
  23. double temporal_weight,
  24. double spatial_weight)
  25. {
  26. util::Logger::LogInfo("Parsing ObjectData2D detections");
  27. // Calculate max and min score to normalize the score
  28. double max_score = std::numeric_limits<double>::min();
  29. double min_score = std::numeric_limits<double>::max();
  30. for (size_t line_index = 0; line_index < values.size(); ++line_index)
  31. {
  32. double score = values[line_index][KEY_SCORE];
  33. if (score > max_score)
  34. {
  35. max_score = score;
  36. }
  37. if (score < min_score)
  38. {
  39. min_score = score;
  40. }
  41. }
  42. util::Logger::LogDebug("min score " + std::to_string(min_score));
  43. util::Logger::LogDebug("max score " + std::to_string(max_score));
  44. // Create objects
  45. size_t obj_count = 0;
  46. for (size_t line_index = 0; line_index < values.size(); ++line_index)
  47. {
  48. size_t frame = (size_t) fabs(values[line_index][KEY_FRAME]);
  49. double x = values[line_index][KEY_X] / image_width;
  50. double y = values[line_index][KEY_Y] / image_height;
  51. double score = values[line_index][KEY_SCORE];
  52. cv::Point2d point(x, y);
  53. core::ObjectData2DPtr object(
  54. new core::ObjectData2D(frame, point));
  55. object->SetTemporalWeight(temporal_weight);
  56. object->SetSpatialWeight(spatial_weight);
  57. object->SetDetectionScore(
  58. util::MyMath::InverseLerp(min_score, max_score, score));
  59. sequence.AddObject(object);
  60. obj_count++;
  61. }
  62. util::Logger::LogDebug("objects parsed " + std::to_string(obj_count));
  63. util::Logger::LogDebug("frame count " + std::to_string(sequence.GetFrameCount()));
  64. }
  65. void Parser::ParseObjectDataAngular(ValueMapVector& values,
  66. core::DetectionSequence& sequence,
  67. double image_width,
  68. double image_height,
  69. double temporal_weight,
  70. double spatial_weight,
  71. double angular_weight)
  72. {
  73. util::Logger::LogInfo("Parsing ObjectDataAngular detections");
  74. // Calculate max and min score to normalize the score
  75. double max_score = std::numeric_limits<double>::min();
  76. double min_score = std::numeric_limits<double>::max();
  77. for (size_t line_index = 0; line_index < values.size(); ++line_index)
  78. {
  79. for (size_t object_i = 0; object_i < values[line_index].size();
  80. ++object_i)
  81. {
  82. double score = values[line_index][KEY_SCORE];
  83. if (score > max_score)
  84. {
  85. max_score = score;
  86. }
  87. if (score < min_score)
  88. {
  89. min_score = score;
  90. }
  91. }
  92. }
  93. util::Logger::LogDebug("min score " + std::to_string(min_score));
  94. util::Logger::LogDebug("max score " + std::to_string(max_score));
  95. // Create objects
  96. size_t obj_count = 0;
  97. for (size_t line_index = 0; line_index < values.size(); ++line_index)
  98. {
  99. size_t frame = (size_t) fabs(values[line_index][KEY_FRAME]);
  100. double x = values[line_index][KEY_X] / image_width;
  101. double y = values[line_index][KEY_Y] / image_height;
  102. double angle = MyMath::Radian(values[line_index][KEY_ANGLE]);
  103. double score = values[line_index][KEY_SCORE];
  104. //TODO detection score interpolation (0.5 <-> 1.0) (experimental)
  105. score = util::MyMath::InverseLerp(min_score, max_score, score);
  106. score = score * 0.5 + 0.5;
  107. cv::Point2d point(x, y);
  108. core::ObjectDataAngularPtr object(
  109. new core::ObjectDataAngular(frame, point, angle));
  110. object->SetTemporalWeight(temporal_weight);
  111. object->SetSpatialWeight(spatial_weight);
  112. object->SetAngularWeight(angular_weight);
  113. object->SetDetectionScore(score);
  114. sequence.AddObject(object);
  115. obj_count++;
  116. }
  117. util::Logger::LogDebug("objects parsed " + std::to_string(obj_count));
  118. util::Logger::LogDebug("frame count " + std::to_string(sequence.GetFrameCount()));
  119. }
  120. void Parser::ParseObjectDataBox(ValueMapVector& values,
  121. core::DetectionSequence& sequence,
  122. double image_width,
  123. double image_height,
  124. double temporal_weight,
  125. double spatial_weight)
  126. {
  127. util::Logger::LogInfo("Parsing ObjectDataBox detections");
  128. // Calculate max and min score to normalize the score
  129. double max_score = std::numeric_limits<double>::min();
  130. double min_score = std::numeric_limits<double>::max();
  131. for (size_t line_index = 0; line_index < values.size(); ++line_index)
  132. {
  133. double score = values[line_index][KEY_SCORE];
  134. if (score > max_score)
  135. {
  136. max_score = score;
  137. }
  138. if (score < min_score)
  139. {
  140. min_score = score;
  141. }
  142. }
  143. util::Logger::LogDebug("min score " + std::to_string(min_score));
  144. util::Logger::LogDebug("max score " + std::to_string(max_score));
  145. // Create objects
  146. size_t obj_count = 0;
  147. for (size_t line_index = 0; line_index < values.size(); ++line_index)
  148. {
  149. size_t frame = (size_t) fabs(values[line_index][KEY_FRAME]);
  150. double x = values[line_index][KEY_X] / image_width;
  151. double y = values[line_index][KEY_Y] / image_height;
  152. double width = values[line_index][KEY_WIDTH] / image_width;
  153. double height = values[line_index][KEY_HEIGHT] / image_height;
  154. double score = values[line_index][KEY_SCORE];
  155. cv::Point2d point(x, y);
  156. cv::Point2d size(width, height);
  157. core::ObjectDataBoxPtr object(
  158. new core::ObjectDataBox(frame, point, size));
  159. object->SetTemporalWeight(temporal_weight);
  160. object->SetSpatialWeight(spatial_weight);
  161. object->SetDetectionScore(
  162. util::MyMath::InverseLerp(min_score, max_score, score));
  163. sequence.AddObject(object);
  164. obj_count++;
  165. }
  166. util::Logger::LogDebug("objects parsed " + std::to_string(obj_count));
  167. util::Logger::LogDebug("frame count " + std::to_string(sequence.GetFrameCount()));
  168. }
  169. Grid Parser::ParseGrid(core::DetectionSequence& sequence,
  170. size_t start, size_t stop,
  171. double min_x, double max_x, int res_x,
  172. double min_y, double max_y, int res_y)
  173. {
  174. stop = std::min(stop, sequence.GetFrameCount());
  175. int res_z = (int) (stop - start);
  176. double width = max_x - min_x;
  177. double height = max_y - min_y;
  178. double depth = (double) (stop - start);
  179. Grid grid(res_x, res_y, res_z, width, height, depth);
  180. // Fill with elements with detection score of 0
  181. for (int z = 0; z < grid.GetDepthCount(); ++z)
  182. {
  183. for (int y = 0; y < grid.GetHeightCount(); ++y)
  184. {
  185. for (int x = 0; x < grid.GetWidthCount(); ++x)
  186. {
  187. // Add virtual object, thus the object will not be added to the final track and
  188. // instead be interpolated from real detections
  189. core::ObjectDataPtr value(new core::ObjectData());
  190. grid.SetValue(value, x, y, z);
  191. }
  192. }
  193. }
  194. // Add the detections
  195. for (size_t f = start; f < stop; ++f)
  196. {
  197. for (size_t i = 0; i < sequence.GetObjectCount(f); ++i)
  198. {
  199. core::ObjectDataPtr original_value = sequence.GetObject(f, i);
  200. core::ObjectData2DPtr value =
  201. std::static_pointer_cast<core::ObjectData2D>(original_value);
  202. double x = value->GetPosition().x;
  203. double y = value->GetPosition().y;
  204. double stored_score = grid.GetValue(x, y, f - start)->GetDetectionScore();
  205. // Only overwrite if the new detection score is at least as good
  206. // as the detection score of the already stored value
  207. if (stored_score <= original_value->GetDetectionScore())
  208. {
  209. grid.SetValue(original_value, x, y, f - start);
  210. }
  211. }
  212. }
  213. return grid;
  214. }
  215. }