Parser.cpp 9.5 KB

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