video_preproc.hpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #ifndef MODULES_INFERENCE2_INCLUDE_VIDEO_PREPROC_HPP_
  21. #define MODULES_INFERENCE2_INCLUDE_VIDEO_PREPROC_HPP_
  22. /**
  23. * \file video_preproc.hpp
  24. *
  25. * This file contains a declaration of class VideoPreproc
  26. */
  27. #include <memory>
  28. #include <string>
  29. #include <utility>
  30. #include <vector>
  31. #include "cnis/contrib/video_helper.h"
  32. #include "cnis/infer_server.h"
  33. #include "cnis/processor.h"
  34. #include "cnstream_frame.hpp"
  35. #include "cnstream_frame_va.hpp"
  36. #include "reflex_object.h"
  37. namespace cnstream {
  38. /**
  39. * @brief VideoPreproc is the base class of video preprocessing.
  40. */
  41. class VideoPreproc : virtual public ReflexObjectEx<VideoPreproc> {
  42. public:
  43. /**
  44. * @brief Destructs an object.
  45. *
  46. * @return No return value.
  47. */
  48. virtual ~VideoPreproc() {}
  49. /**
  50. * @brief Creates a preprocess object with the given preprocess's class name.
  51. *
  52. * @param[in] proc_name The preprocess class name.
  53. *
  54. * @return The pointer to preprocess object.
  55. */
  56. static VideoPreproc* Create(const std::string& proc_name);
  57. /**
  58. * @brief Sets model input pixel format.
  59. *
  60. * @param[in] fmt The model input pixel format.
  61. *
  62. * @return No return value.
  63. */
  64. void SetModelInputPixelFormat(infer_server::video::PixelFmt fmt) {
  65. model_input_pixel_format_ = fmt;
  66. }
  67. /**
  68. * @brief Executes preprocessing on the origin data.
  69. *
  70. * @param[out] model_input The input of neural network.
  71. * @param[in] input_data The raw input data. The user could get infer_server::video::VideoFrame object from it.
  72. * @param[in] model_info The model information, e.g., input/output number, shape and etc.
  73. *
  74. * @note The input_data holds infer_server::video::VideoFrame object. Use the statement to get video frame:
  75. * `const infer_server::video::VideoFrame& frame = input_data.GetLref<infer_server::video::VideoFrame>();`.
  76. * After preprocessing, you should set the result to model_output. For example, the model only has one input,
  77. * then you should copy the result to `model_input->buffers[0].MutableData()` which is a void pointer.
  78. *
  79. * @return Returns true if successful, otherwise returns false.
  80. */
  81. virtual bool Execute(infer_server::ModelIO* model_input, const infer_server::InferData& input_data,
  82. const infer_server::ModelInfo& model_info) = 0;
  83. protected:
  84. infer_server::video::PixelFmt model_input_pixel_format_ = infer_server::video::PixelFmt::RGB24;
  85. }; // class VideoPreproc
  86. } // namespace cnstream
  87. #endif // ifndef MODULES_INFERENCE2_INCLUDE_VIDEO_PREPROC_HPP_