Parser.cpp 11 KB

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