edvr.cc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "ultra_infer/vision/sr/ppsr/edvr.h"
  15. namespace ultra_infer {
  16. namespace vision {
  17. namespace sr {
  18. EDVR::EDVR(const std::string &model_file, const std::string &params_file,
  19. const RuntimeOption &custom_option,
  20. const ModelFormat &model_format) {
  21. // unsupported ORT backend
  22. valid_cpu_backends = {Backend::PDINFER, Backend::ORT, Backend::OPENVINO};
  23. valid_gpu_backends = {Backend::PDINFER, Backend::TRT, Backend::ORT};
  24. runtime_option = custom_option;
  25. runtime_option.model_format = model_format;
  26. runtime_option.model_file = model_file;
  27. runtime_option.params_file = params_file;
  28. initialized = Initialize();
  29. }
  30. bool EDVR::Postprocess(std::vector<FDTensor> &infer_results,
  31. std::vector<cv::Mat> &results) {
  32. // group to image
  33. // output_shape is [b, n, c, h, w] n = frame_nums b=1(default)
  34. // b and n is dependence export model shape
  35. // see
  36. // https://github.com/PaddlePaddle/PaddleGAN/blob/develop/docs/zh_CN/tutorials/video_super_resolution.md
  37. auto output_shape = infer_results[0].shape;
  38. // EDVR
  39. int h_ = output_shape[2];
  40. int w_ = output_shape[3];
  41. int c_ = output_shape[1];
  42. int frame_num = 1;
  43. float *out_data = static_cast<float *>(infer_results[0].Data());
  44. cv::Mat temp = cv::Mat::zeros(h_, w_, CV_32FC3); // RGB image
  45. int pix_num = h_ * w_;
  46. int frame_pix_num = pix_num * c_;
  47. for (int frame = 0; frame < frame_num; frame++) {
  48. int index = 0;
  49. for (int h = 0; h < h_; ++h) {
  50. for (int w = 0; w < w_; ++w) {
  51. temp.at<cv::Vec3f>(h, w) = {
  52. out_data[2 * pix_num + index + frame_pix_num * frame],
  53. out_data[pix_num + index + frame_pix_num * frame],
  54. out_data[index + frame_pix_num * frame]};
  55. index += 1;
  56. }
  57. }
  58. // tmp data type is float[0-1.0],convert to uint type
  59. cv::Mat res = cv::Mat::zeros(temp.size(), CV_8UC3);
  60. temp.convertTo(res, CV_8UC3, 255);
  61. results.push_back(res);
  62. }
  63. return true;
  64. }
  65. } // namespace sr
  66. } // namespace vision
  67. } // namespace ultra_infer