seg_preprocess.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (c) 2021 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 "model_deploy/ppseg/include/seg_preprocess.h"
  15. namespace PaddleDeploy {
  16. bool SegPreprocess::Init(const YAML::Node& yaml_config) {
  17. if (!BuildTransform(yaml_config)) {
  18. return false;
  19. }
  20. return true;
  21. }
  22. bool SegPreprocess::PrepareInputs(const std::vector<ShapeInfo>& shape_infos,
  23. std::vector<cv::Mat>* imgs,
  24. std::vector<DataBlob>* inputs,
  25. int thread_num) {
  26. inputs->clear();
  27. if (!PreprocessImages(shape_infos, imgs, thread_num = thread_num)) {
  28. std::cerr << "Error happend while execute function "
  29. << "SegPreprocess::Run" << std::endl;
  30. return false;
  31. }
  32. DataBlob im("x");
  33. int batch = imgs->size();
  34. int w = shape_infos[0].shapes.back()[0];
  35. int h = shape_infos[0].shapes.back()[1];
  36. im.Resize({batch, 3, h, w}, FLOAT32);
  37. int sample_shape = 3 * h * w;
  38. #pragma omp parallel for num_threads(thread_num)
  39. for (auto i = 0; i < batch; ++i) {
  40. memcpy(im.data.data() + i * sample_shape * sizeof(float), (*imgs)[i].data,
  41. sample_shape * sizeof(float));
  42. }
  43. inputs->clear();
  44. inputs->push_back(std::move(im));
  45. return true;
  46. }
  47. bool SegPreprocess::Run(std::vector<cv::Mat>* imgs,
  48. std::vector<DataBlob>* inputs,
  49. std::vector<ShapeInfo>* shape_infos, int thread_num) {
  50. if ((*imgs).size() == 0) {
  51. std::cerr << "empty input image on SegPreprocess" << std::endl;
  52. return true;
  53. }
  54. if (!ShapeInfer(*imgs, shape_infos, thread_num)) {
  55. std::cerr << "ShapeInfer failed while call SegPreprocess::Run" << std::endl;
  56. return false;
  57. }
  58. if (!PrepareInputs(*shape_infos, imgs, inputs, thread_num)) {
  59. std::cerr << "PrepareInputs failed while call "
  60. << "SegPreprocess::PrepareInputs" << std::endl;
  61. return false;
  62. }
  63. return true;
  64. }
  65. } // namespace PaddleDeploy