preprocess_lprnet.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 <memory>
  22. #include <utility>
  23. #include <vector>
  24. #include "cnstream_frame_va.hpp"
  25. #include "preproc.hpp"
  26. class PreprocLprnet : public cnstream::ObjPreproc {
  27. public:
  28. int Execute(const std::vector<float*>& net_inputs, const std::shared_ptr<edk::ModelLoader>& model,
  29. const cnstream::CNFrameInfoPtr& finfo, const std::shared_ptr<cnstream::CNInferObject>& pobj) override;
  30. DECLARE_REFLEX_OBJECT_EX(PreprocLprnet, cnstream::ObjPreproc);
  31. }; // class PreprocLprnet
  32. IMPLEMENT_REFLEX_OBJECT_EX(PreprocLprnet, cnstream::ObjPreproc)
  33. int PreprocLprnet::Execute(const std::vector<float*>& net_inputs, const std::shared_ptr<edk::ModelLoader>& model,
  34. const cnstream::CNFrameInfoPtr& finfo,
  35. const std::shared_ptr<cnstream::CNInferObject>& pobj) {
  36. cnstream::CNDataFramePtr frame = finfo->collection.Get<cnstream::CNDataFramePtr>(cnstream::kCNDataFrameTag);
  37. // origin frame
  38. cv::Mat frame_bgr = frame->ImageBGR();
  39. // crop objct from frame
  40. int w = frame->width;
  41. int h = frame->height;
  42. cv::Rect obj_roi(pobj->bbox.x * w, pobj->bbox.y * h, pobj->bbox.w * w, pobj->bbox.h * h);
  43. cv::Mat obj_bgr = frame_bgr(obj_roi);
  44. // resize
  45. int input_w = model->InputShape(0).W();
  46. int input_h = model->InputShape(0).H();
  47. cv::Mat obj_bgr_resized;
  48. cv::resize(obj_bgr, obj_bgr_resized, cv::Size(input_h, input_w));
  49. // transpose
  50. cv::transpose(obj_bgr_resized, obj_bgr_resized);
  51. // bgr2bgra
  52. cv::Mat obj_bgra;
  53. cv::Mat a(input_h, input_w, CV_8UC1, cv::Scalar(0.0));
  54. std::vector<cv::Mat> vec_mat = {obj_bgr_resized, a};
  55. cv::merge(std::move(vec_mat), obj_bgra);
  56. // convert to float32, required by inferencer module
  57. cv::Mat obj_bgra_float32(input_h, input_w, CV_32FC4, net_inputs[0]);
  58. obj_bgra.convertTo(obj_bgra_float32, CV_32FC4);
  59. return 0;
  60. }