mot.cc 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <iomanip>
  15. #include "ultra_infer/vision/visualize/visualize.h"
  16. namespace ultra_infer {
  17. namespace vision {
  18. cv::Scalar GetMOTBoxColor(int idx) {
  19. idx = idx * 3;
  20. cv::Scalar color =
  21. cv::Scalar((37 * idx) % 255, (17 * idx) % 255, (29 * idx) % 255);
  22. return color;
  23. }
  24. cv::Mat VisMOT(const cv::Mat &img, const MOTResult &results,
  25. float score_threshold, tracking::TrailRecorder *recorder) {
  26. cv::Mat vis_img = img.clone();
  27. int im_h = img.rows;
  28. int im_w = img.cols;
  29. float text_scale = std::max(1, static_cast<int>(im_w / 1600.));
  30. float text_thickness = 2.;
  31. float line_thickness = std::max(1, static_cast<int>(im_w / 500.));
  32. for (int i = 0; i < results.boxes.size(); ++i) {
  33. if (results.scores[i] < score_threshold) {
  34. continue;
  35. }
  36. const int obj_id = results.ids[i];
  37. const float score = results.scores[i];
  38. cv::Scalar color = GetMOTBoxColor(obj_id);
  39. if (recorder != nullptr) {
  40. int id = results.ids[i];
  41. auto iter = recorder->records.find(id);
  42. if (iter != recorder->records.end()) {
  43. for (int j = 0; j < iter->second.size(); j++) {
  44. cv::Point center(iter->second[j][0], iter->second[j][1]);
  45. cv::circle(vis_img, center, text_thickness, color);
  46. }
  47. }
  48. }
  49. cv::Point pt1 = cv::Point(results.boxes[i][0], results.boxes[i][1]);
  50. cv::Point pt2 = cv::Point(results.boxes[i][2], results.boxes[i][3]);
  51. cv::Point id_pt = cv::Point(results.boxes[i][0], results.boxes[i][1] + 10);
  52. cv::Point score_pt =
  53. cv::Point(results.boxes[i][0], results.boxes[i][1] - 10);
  54. cv::rectangle(vis_img, pt1, pt2, color, line_thickness);
  55. std::ostringstream idoss;
  56. idoss << std::setiosflags(std::ios::fixed) << std::setprecision(4);
  57. idoss << obj_id;
  58. std::string id_text = idoss.str();
  59. cv::putText(vis_img, id_text, id_pt, cv::FONT_HERSHEY_PLAIN, text_scale,
  60. color, text_thickness);
  61. std::ostringstream soss;
  62. soss << std::setiosflags(std::ios::fixed) << std::setprecision(2);
  63. soss << score;
  64. std::string score_text = soss.str();
  65. cv::putText(vis_img, score_text, score_pt, cv::FONT_HERSHEY_PLAIN,
  66. text_scale, color, text_thickness);
  67. }
  68. return vis_img;
  69. }
  70. } // namespace vision
  71. } // namespace ultra_infer