keypoint.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 "ultra_infer/vision/visualize/visualize.h"
  15. namespace ultra_infer {
  16. namespace vision {
  17. cv::Mat VisKeypointDetection(const cv::Mat &im,
  18. const KeyPointDetectionResult &results,
  19. float conf_threshold) {
  20. const int edge[][2] = {{0, 1}, {0, 2}, {1, 3}, {2, 4}, {3, 5},
  21. {4, 6}, {5, 7}, {6, 8}, {7, 9}, {8, 10},
  22. {5, 11}, {6, 12}, {11, 13}, {12, 14}, {13, 15},
  23. {14, 16}, {11, 12}};
  24. auto colormap = GenerateColorMap();
  25. cv::Mat vis_img = im.clone();
  26. int detection_nums = results.keypoints.size() / 17;
  27. for (int i = 0; i < detection_nums; i++) {
  28. int index = i * 17;
  29. bool is_over_threshold = true;
  30. for (int j = 0; j < results.num_joints; j++) {
  31. if (results.scores[index + j] < conf_threshold) {
  32. is_over_threshold = false;
  33. break;
  34. }
  35. }
  36. if (is_over_threshold) {
  37. for (int k = 0; k < results.num_joints; k++) {
  38. int x_coord = int(results.keypoints[index + k][0]);
  39. int y_coord = int(results.keypoints[index + k][1]);
  40. cv::circle(vis_img, cv::Point2d(x_coord, y_coord), 1,
  41. cv::Scalar(0, 0, 255), 2);
  42. int x_start = int(results.keypoints[index + edge[k][0]][0]);
  43. int y_start = int(results.keypoints[index + edge[k][0]][1]);
  44. int x_end = int(results.keypoints[index + edge[k][1]][0]);
  45. int y_end = int(results.keypoints[index + edge[k][1]][1]);
  46. cv::line(vis_img, cv::Point2d(x_start, y_start),
  47. cv::Point2d(x_end, y_end), colormap[k], 1);
  48. }
  49. }
  50. }
  51. return vis_img;
  52. }
  53. } // namespace vision
  54. } // namespace ultra_infer