preprocess_yolov5.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 <vector>
  23. #include "opencv2/highgui/highgui.hpp"
  24. #include "opencv2/imgproc/imgproc.hpp"
  25. #if (CV_MAJOR_VERSION >= 3)
  26. #include "opencv2/imgcodecs/imgcodecs.hpp"
  27. #endif
  28. #include "cnstream_frame_va.hpp"
  29. #include "preproc.hpp"
  30. #include "cnstream_logging.hpp"
  31. class PreprocYolov5 : public cnstream::Preproc {
  32. public:
  33. int Execute(const std::vector<float*>& net_inputs, const std::shared_ptr<edk::ModelLoader>& model,
  34. const std::shared_ptr<cnstream::CNFrameInfo>& package) {
  35. // check params
  36. auto input_shape = model->InputShape(0);
  37. if (net_inputs.size() != 1 || input_shape.C() != 3) {
  38. LOGE(DEMO) << "[PreprocCpu] model input shape not supported";
  39. return -1;
  40. }
  41. cnstream::CNDataFramePtr frame = package->collection.Get<cnstream::CNDataFramePtr>(cnstream::kCNDataFrameTag);
  42. int width = frame->width;
  43. int height = frame->height;
  44. int dst_w = input_shape.W();
  45. int dst_h = input_shape.H();
  46. cv::Mat img = frame->ImageBGR();
  47. // resize
  48. if (height != dst_h || width != dst_w) {
  49. cv::Mat dst(dst_h, dst_w, CV_8UC3, cv::Scalar(0, 0, 0));
  50. const float scaling_factors = std::min(1.0 * dst_w / width, 1.0 * dst_h / height);
  51. cv::Mat resized(height * scaling_factors, width * scaling_factors, CV_8UC3);
  52. cv::resize(img, resized, cv::Size(resized.cols, resized.rows));
  53. cv::Rect roi;
  54. roi.x = (dst.cols - resized.cols) / 2;
  55. roi.y = (dst.rows - resized.rows) / 2;
  56. roi.width = resized.cols;
  57. roi.height = resized.rows;
  58. resized.copyTo(dst(roi));
  59. img = dst;
  60. }
  61. // bgr 2 rgb
  62. cv::cvtColor(img, img, cv::COLOR_BGR2RGB);
  63. // since model input data type is float, convert image to float
  64. cv::Mat dst(dst_h, dst_w, CV_32FC3, net_inputs[0]);
  65. img.convertTo(dst, CV_32F);
  66. dst /= 255.0;
  67. return 0;
  68. }
  69. private:
  70. DECLARE_REFLEX_OBJECT_EX(PreprocYolov5, cnstream::Preproc);
  71. }; // class PreprocYolov5
  72. IMPLEMENT_REFLEX_OBJECT_EX(PreprocYolov5, cnstream::Preproc);