video_preprocess_yolov5.cpp 5.0 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 <vector>
  24. #include "opencv2/highgui/highgui.hpp"
  25. #include "opencv2/imgproc/imgproc.hpp"
  26. #if (CV_MAJOR_VERSION >= 3)
  27. #include "opencv2/imgcodecs/imgcodecs.hpp"
  28. #endif
  29. #include "cnis/contrib/video_helper.h"
  30. #include "cnstream_frame_va.hpp"
  31. #include "cnstream_logging.hpp"
  32. #include "video_preproc.hpp"
  33. #include "video_preprocess_common.hpp"
  34. /**
  35. * @brief Video preprocessing for YOLOv5 neural network
  36. */
  37. class VideoPreprocYolov5 : public cnstream::VideoPreproc {
  38. public:
  39. /**
  40. * @brief Execute YOLOv5 neural network preprocessing
  41. *
  42. * @param model_input: the input of neural network. The preproc result should be set to it.
  43. * @param input_data: the raw input data. The user could get infer_server::video::VideoFrame object from it.
  44. * @param model_info: model information, e.g., input/output number, shape and etc.
  45. *
  46. * @return return true if succeed
  47. */
  48. bool Execute(infer_server::ModelIO* model_input, const infer_server::InferData& input_data,
  49. const infer_server::ModelInfo& model_info) {
  50. // check model input number and shape
  51. uint32_t input_num = model_info.InputNum();
  52. if (input_num != 1) {
  53. LOGE(DEMO) << "[VideoPreprocYolov5] model input number not supported. It should be 1, but " << input_num;
  54. return false;
  55. }
  56. infer_server::Shape input_shape;
  57. input_shape = model_info.InputShape(0);
  58. int c_idx = 3;
  59. int w_idx = 2;
  60. int h_idx = 1;
  61. if (model_info.InputLayout(0).order == infer_server::DimOrder::NCHW) {
  62. c_idx = 1;
  63. w_idx = 3;
  64. h_idx = 2;
  65. }
  66. if (input_shape[c_idx] != 3) {
  67. LOGE(DEMO) << "[VideoPreprocYolov5] model input shape not supported, `c` should be 3, but " << input_shape[c_idx];
  68. return false;
  69. }
  70. // do preproc
  71. const infer_server::video::VideoFrame& frame = input_data.GetLref<infer_server::video::VideoFrame>();
  72. size_t src_w = frame.width;
  73. size_t src_h = frame.height;
  74. uint32_t dst_w = input_shape[w_idx];
  75. uint32_t dst_h = input_shape[h_idx];
  76. uint8_t* img_data = new (std::nothrow) uint8_t[frame.GetTotalSize()];
  77. if (!img_data) {
  78. LOGE(DEMO) << "[VideoPreprocYolov5] Failed to alloc memory, size: " << frame.GetTotalSize();
  79. return false;
  80. }
  81. uint8_t* img_data_tmp = img_data;
  82. for (auto plane_idx = 0u; plane_idx < frame.plane_num; ++plane_idx) {
  83. memcpy(img_data_tmp, frame.plane[plane_idx].Data(), frame.GetPlaneSize(plane_idx));
  84. img_data_tmp += frame.GetPlaneSize(plane_idx);
  85. }
  86. // convert color space from src to dst
  87. cv::Mat dst_cvt_color_img;
  88. if (!ConvertColorSpace(src_w, src_h, frame.format, model_input_pixel_format_, img_data, &dst_cvt_color_img)) {
  89. LOGW(DEMO) << "[VideoPreprocYolov5] Unsupport pixel format. src: " << static_cast<int>(frame.format)
  90. << " dst: " << static_cast<int>(model_input_pixel_format_);
  91. delete[] img_data;
  92. return false;
  93. }
  94. cv::Mat img = dst_cvt_color_img;
  95. // resize
  96. if (src_h != dst_h || src_w != dst_w) {
  97. cv::Mat dst(dst_h, dst_w, CV_8UC3, cv::Scalar(0, 0, 0));
  98. const float scaling_factors = std::min(1.0 * dst_w / src_w, 1.0 * dst_h / src_h);
  99. cv::Mat resized(src_h * scaling_factors, src_w * scaling_factors, CV_8UC3);
  100. cv::resize(img, resized, cv::Size(resized.cols, resized.rows));
  101. cv::Rect roi;
  102. roi.x = (dst.cols - resized.cols) / 2;
  103. roi.y = (dst.rows - resized.rows) / 2;
  104. roi.width = resized.cols;
  105. roi.height = resized.rows;
  106. resized.copyTo(dst(roi));
  107. img = dst;
  108. }
  109. // copy data to model_input buffer
  110. cv::Mat dst(dst_h, dst_w, CV_32FC3, model_input->buffers[0].MutableData());
  111. img.convertTo(dst, CV_32FC3);
  112. dst /= 255.0;
  113. delete[] img_data;
  114. return true;
  115. }
  116. private:
  117. DECLARE_REFLEX_OBJECT_EX(VideoPreprocYolov5, cnstream::VideoPreproc);
  118. }; // class VideoPreprocYolov5
  119. IMPLEMENT_REFLEX_OBJECT_EX(VideoPreprocYolov5, cnstream::VideoPreproc);