pose_osd_module.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*************************************************************************
  2. * Copyright (C) [2021] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #include <opencv2/opencv.hpp>
  21. #include <algorithm>
  22. #include <memory>
  23. #include <random>
  24. #include <string>
  25. #include <utility>
  26. #include <vector>
  27. #include "cnstream_frame_va.hpp"
  28. #include "cnstream_module.hpp"
  29. #include "cns_openpose.hpp"
  30. namespace cns_openpose {
  31. static
  32. std::vector<cv::Scalar> GenerateColors(size_t ncolors) {
  33. std::random_device rd;
  34. std::mt19937 num_gen(rd());
  35. std::uniform_int_distribution<> b(64, 255);
  36. std::uniform_int_distribution<> g(120, 255);
  37. std::uniform_int_distribution<> r(90, 200);
  38. std::vector<cv::Scalar> colors;
  39. for (size_t i = 0; i < ncolors; ++i) {
  40. colors.emplace_back(cv::Scalar(b(num_gen), g(num_gen), r(num_gen)));
  41. }
  42. return colors;
  43. }
  44. class PoseOsd : public cnstream::Module, public cnstream::ModuleCreator<PoseOsd> {
  45. public:
  46. explicit PoseOsd(const std::string& name) : cnstream::Module(name) {}
  47. bool Open(cnstream::ModuleParamSet param_set) override {
  48. if (param_set.find("nkeypoints") == param_set.end()) {
  49. LOGE(POSE_OSD) << "[nkeypoints] the number of keypoints must be set. For body25, the number of keypoints is 25";
  50. return false;
  51. }
  52. if (param_set.find("nlimbs") == param_set.end()) {
  53. LOGE(POSE_OSD) << "[nlimbs] the number of limbs must be set. For body25, the number of limbs is 26";
  54. return false;
  55. }
  56. try {
  57. nkeypoints_ = std::stoi(param_set["nkeypoints"]);
  58. nlimbs_ = std::stoi(param_set["nlimbs"]);
  59. } catch (std::exception& e) {
  60. LOGE(POSE_OSD) << "Parse [nkeypoints] or [nlimbs] failed, maybe there are not integers.";
  61. return false;
  62. }
  63. colors_ = GenerateColors(std::max(nkeypoints_, nlimbs_));
  64. return true;
  65. }
  66. void Close() override {}
  67. int Process(std::shared_ptr<cnstream::CNFrameInfo> package) override {
  68. auto frame = package->collection.Get<cnstream::CNDataFramePtr>(cnstream::kCNDataFrameTag);
  69. const auto& keypoints = package->collection.Get<Keypoints>(kPoseKeypointsTag);
  70. const auto& total_limbs = package->collection.Get<Limbs>(kPoseLimbsTag);
  71. if (keypoints.size() != nkeypoints_) {
  72. LOGF(POSE_OSD) << "keypoints number mismatch!";
  73. }
  74. if (total_limbs.size() != nlimbs_) {
  75. LOGF(POSE_OSD) << "limbs number mismatch!";
  76. }
  77. cv::Mat origin_img = frame->ImageBGR();
  78. // draw limbs
  79. for (size_t i = 0; i < total_limbs.size(); ++i) {
  80. for (const auto& limb : total_limbs[i]) {
  81. cv::line(origin_img, keypoints[limb.first.x][limb.first.y],
  82. keypoints[limb.second.x][limb.second.y],
  83. colors_[i], 3);
  84. }
  85. }
  86. return 0;
  87. }
  88. private:
  89. size_t nkeypoints_ = 0;
  90. size_t nlimbs_ = 0;
  91. std::vector<cv::Scalar> colors_;
  92. }; // class PoseOsd
  93. } // namespace cns_openpose