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