Parser.cpp 9.9 KB

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