video_postprocess_yolov3_mm.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 <algorithm>
  21. #include <memory>
  22. #include <string>
  23. #include <utility>
  24. #include <vector>
  25. #include "cnstream_frame_va.hpp"
  26. #include "cnstream_logging.hpp"
  27. #include "video_postproc.hpp"
  28. /**
  29. * @brief Video postprocessing for magicmind YOLOv3 neural network
  30. * The input frame of the model should keep aspect ratio.
  31. */
  32. class VideoPostprocYolov3MM : public cnstream::VideoPostproc {
  33. public:
  34. /**
  35. * @brief User process. Postprocess on outputs of magicmind YOLOv3 neural network and fill data to frame.
  36. *
  37. * @param output_data: the raw output data from neural network
  38. * @param model_output: the raw neural network output data
  39. * @param model_info: model information, e.g., input/output number, shape and etc.
  40. *
  41. * @return return true if succeed
  42. */
  43. bool Execute(infer_server::InferData* output_data, const infer_server::ModelIO& model_output,
  44. const infer_server::ModelInfo& model_info) override;
  45. private:
  46. DECLARE_REFLEX_OBJECT_EX(VideoPostprocYolov3MM, cnstream::VideoPostproc);
  47. }; // class VideoPostprocYolov3MM
  48. IMPLEMENT_REFLEX_OBJECT_EX(VideoPostprocYolov3MM, cnstream::VideoPostproc);
  49. bool VideoPostprocYolov3MM::Execute(infer_server::InferData* output_data, const infer_server::ModelIO& model_output,
  50. const infer_server::ModelInfo& model_info) {
  51. LOGF_IF(DEMO, model_info.InputNum() != 1);
  52. LOGF_IF(DEMO, model_info.OutputNum() != 2);
  53. LOGF_IF(DEMO, model_output.buffers.size() != 2);
  54. cnstream::CNFrameInfoPtr frame = output_data->GetUserData<cnstream::CNFrameInfoPtr>();
  55. cnstream::CNInferObjsPtr objs_holder = frame->collection.Get<cnstream::CNInferObjsPtr>(cnstream::kCNInferObjsTag);
  56. cnstream::CNObjsVec& objs = objs_holder->objs_;
  57. const auto input_sp = model_info.InputShape(0);
  58. const int img_w = frame->collection.Get<cnstream::CNDataFramePtr>(cnstream::kCNDataFrameTag)->width;
  59. const int img_h = frame->collection.Get<cnstream::CNDataFramePtr>(cnstream::kCNDataFrameTag)->height;
  60. int w_idx = 2;
  61. int h_idx = 1;
  62. if (model_info.InputLayout(0).order == infer_server::DimOrder::NCHW) {
  63. w_idx = 3;
  64. h_idx = 2;
  65. }
  66. const int model_input_w = static_cast<int>(input_sp[w_idx]);
  67. const int model_input_h = static_cast<int>(input_sp[h_idx]);
  68. // scaling factors
  69. const float scaling_factors = std::min(1.0 * model_input_w / img_w, 1.0 * model_input_h / img_h);
  70. // The input frame of the model should keep aspect ratio.
  71. // If mlu resize and convert operator is used as preproc, parameter keep_aspect_ratio of Inferencer2 module
  72. // should be set to true in config json file.
  73. // If cpu preproc is used as preproc, please make sure keep aspect ratio in custom preproc.
  74. // Scaler does not support keep aspect ratio.
  75. // If the input frame does not keep aspect ratio, set scaled_w = model_input_w and scaled_h = model_input_h
  76. // scaled size
  77. const int scaled_w = scaling_factors * img_w;
  78. const int scaled_h = scaling_factors * img_h;
  79. // second output contains box_num
  80. const int box_num = reinterpret_cast<const int*>(model_output.buffers[1].Data())[0];
  81. int box_step = 7;
  82. auto range_0_1 = [](float num) { return std::max(.0f, std::min(1.0f, num)); };
  83. // first output contains box, score and label
  84. const float* net_output = reinterpret_cast<const float*>(model_output.buffers[0].Data());
  85. for (int box_idx = 0; box_idx < box_num; ++box_idx) {
  86. float left = range_0_1(net_output[box_idx * box_step + 3]);
  87. float right = range_0_1(net_output[box_idx * box_step + 5]);
  88. float top = range_0_1(net_output[box_idx * box_step + 4]);
  89. float bottom = range_0_1(net_output[box_idx * box_step + 6]);
  90. // rectify
  91. left = (left * model_input_w - (model_input_w - scaled_w) / 2) / scaled_w;
  92. right = (right * model_input_w - (model_input_w - scaled_w) / 2) / scaled_w;
  93. top = (top * model_input_h - (model_input_h - scaled_h) / 2) / scaled_h;
  94. bottom = (bottom * model_input_h - (model_input_h - scaled_h) / 2) / scaled_h;
  95. left = std::max(0.0f, left);
  96. right = std::max(0.0f, right);
  97. top = std::max(0.0f, top);
  98. bottom = std::max(0.0f, bottom);
  99. auto obj = std::make_shared<cnstream::CNInferObject>();
  100. obj->id = std::to_string(static_cast<int>(net_output[box_idx * box_step + 1]));
  101. obj->score = net_output[box_idx * box_step + 2];
  102. obj->bbox.x = left;
  103. obj->bbox.y = top;
  104. obj->bbox.w = std::min(1.0f - obj->bbox.x, right - left);
  105. obj->bbox.h = std::min(1.0f - obj->bbox.y, bottom - top);
  106. if (obj->bbox.h <= 0 || obj->bbox.w <= 0 || (obj->score < threshold_ && threshold_ > 0)) continue;
  107. std::lock_guard<std::mutex> objs_mutex(objs_holder->mutex_);
  108. objs.push_back(obj);
  109. }
  110. return true;
  111. }