video_postprocess_classification.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <memory>
  21. #include <string>
  22. #include <utility>
  23. #include <vector>
  24. #include "cnstream_frame_va.hpp"
  25. #include "cnstream_logging.hpp"
  26. #include "video_postproc.hpp"
  27. /**
  28. * @brief Video postprocessing for classification neural network
  29. */
  30. class VideoPostprocClassification : public cnstream::VideoPostproc {
  31. public:
  32. /**
  33. * @brief Execute secondary classification neural networks postprocessing
  34. *
  35. * @param output_data: postproc result. The result of postprocessing should be set to it.
  36. * You could set any type of data to this parameter and get it in UserProcess function.
  37. * @param model_output: the raw output data from neural network
  38. * @param model_info: model information, e.g., input/output number, shape and etc.
  39. *
  40. * @return return true if succeed
  41. * @see VideoObjPostprocClassification::UserProcess
  42. */
  43. bool Execute(infer_server::InferData* output_data, const infer_server::ModelIO& model_output,
  44. const infer_server::ModelInfo& model_info) override;
  45. DECLARE_REFLEX_OBJECT_EX(VideoPostprocClassification, cnstream::VideoPostproc)
  46. }; // classd VideoPostprocClassification
  47. IMPLEMENT_REFLEX_OBJECT_EX(VideoPostprocClassification, cnstream::VideoPostproc)
  48. bool VideoPostprocClassification::Execute(infer_server::InferData* output_data,
  49. 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() != 1);
  53. LOGF_IF(DEMO, model_output.buffers.size() != 1);
  54. const float* data = reinterpret_cast<const float*>(model_output.buffers[0].Data());
  55. auto len = model_info.OutputShape(0).DataCount();
  56. auto score_ptr = data;
  57. float max_score = 0;
  58. int label = 0;
  59. for (decltype(len) i = 0; i < len; ++i) {
  60. auto score = *(score_ptr + i);
  61. if (score > max_score) {
  62. max_score = score;
  63. label = i;
  64. }
  65. }
  66. auto obj = std::make_shared<cnstream::CNInferObject>();
  67. obj->id = std::to_string(label);
  68. obj->score = max_score;
  69. cnstream::CNFrameInfoPtr frame = output_data->GetUserData<cnstream::CNFrameInfoPtr>();
  70. cnstream::CNInferObjsPtr objs_holder = frame->collection.Get<cnstream::CNInferObjsPtr>(cnstream::kCNInferObjsTag);
  71. std::lock_guard<std::mutex> objs_mutex(objs_holder->mutex_);
  72. objs_holder->objs_.push_back(obj);
  73. return true;
  74. }
  75. /**
  76. * @brief Video postprocessing for secondary classification
  77. */
  78. class VideoObjPostprocClassification : public cnstream::VideoPostproc {
  79. public:
  80. /**
  81. * @brief Execute secondary classification neural networks postprocessing
  82. *
  83. * @param output_data: postproc result. The result of postprocessing should be set to it.
  84. * You could set any type of data to this parameter and get it in UserProcess function.
  85. * @param model_output: the raw output data from neural network
  86. * @param model_info: model information, e.g., input/output number, shape and etc.
  87. *
  88. * @return return true if succeed
  89. * @see VideoObjPostprocClassification::UserProcess
  90. */
  91. bool Execute(infer_server::InferData* output_data, const infer_server::ModelIO& model_output,
  92. const infer_server::ModelInfo& model_info) override;
  93. DECLARE_REFLEX_OBJECT_EX(VideoObjPostprocClassification, cnstream::VideoPostproc)
  94. }; // classd VideoObjPostprocClassification
  95. IMPLEMENT_REFLEX_OBJECT_EX(VideoObjPostprocClassification, cnstream::VideoPostproc)
  96. bool VideoObjPostprocClassification::Execute(infer_server::InferData* output_data,
  97. const infer_server::ModelIO& model_output,
  98. const infer_server::ModelInfo& model_info) {
  99. LOGF_IF(DEMO, model_info.InputNum() != 1);
  100. LOGF_IF(DEMO, model_info.OutputNum() != 1);
  101. LOGF_IF(DEMO, model_output.buffers.size() != 1);
  102. const float* data = reinterpret_cast<const float*>(model_output.buffers[0].Data());
  103. auto len = model_info.OutputShape(0).DataCount();
  104. auto score_ptr = data;
  105. float max_score = 0;
  106. int label = 0;
  107. for (decltype(len) i = 0; i < len; ++i) {
  108. auto score = *(score_ptr + i);
  109. if (score > max_score) {
  110. max_score = score;
  111. label = i;
  112. }
  113. }
  114. cnstream::CNInferAttr attr;
  115. attr.id = 0;
  116. attr.value = label;
  117. attr.score = max_score;
  118. std::shared_ptr<cnstream::CNInferObject> obj = output_data->GetUserData<std::shared_ptr<cnstream::CNInferObject>>();
  119. obj->AddAttribute("classification", attr);
  120. return true;
  121. }