smoke.cc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/perception/paddle3d/smoke/smoke.h"
  15. namespace ultra_infer {
  16. namespace vision {
  17. namespace perception {
  18. Smoke::Smoke(const std::string &model_file, const std::string &params_file,
  19. const std::string &config_file, const RuntimeOption &custom_option,
  20. const ModelFormat &model_format)
  21. : preprocessor_(config_file) {
  22. valid_cpu_backends = {Backend::PDINFER};
  23. valid_gpu_backends = {Backend::PDINFER};
  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 Smoke::Initialize() {
  31. if (!InitRuntime()) {
  32. FDERROR << "Failed to initialize ultra_infer backend." << std::endl;
  33. return false;
  34. }
  35. return true;
  36. }
  37. bool Smoke::Predict(const cv::Mat &im, PerceptionResult *result) {
  38. std::vector<PerceptionResult> results;
  39. if (!BatchPredict({im}, &results)) {
  40. return false;
  41. }
  42. if (results.size()) {
  43. *result = std::move(results[0]);
  44. }
  45. return true;
  46. }
  47. bool Smoke::BatchPredict(const std::vector<cv::Mat> &images,
  48. std::vector<PerceptionResult> *results) {
  49. std::vector<FDMat> fd_images = WrapMat(images);
  50. if (!preprocessor_.Run(&fd_images, &reused_input_tensors_)) {
  51. FDERROR << "Failed to preprocess the input image." << std::endl;
  52. return false;
  53. }
  54. reused_input_tensors_[0].name = InputInfoOfRuntime(0).name;
  55. reused_input_tensors_[1].name = InputInfoOfRuntime(1).name;
  56. reused_input_tensors_[2].name = InputInfoOfRuntime(2).name;
  57. if (!Infer(reused_input_tensors_, &reused_output_tensors_)) {
  58. FDERROR << "Failed to inference by runtime." << std::endl;
  59. return false;
  60. }
  61. if (!postprocessor_.Run(reused_output_tensors_, results)) {
  62. FDERROR << "Failed to postprocess the inference results by runtime."
  63. << std::endl;
  64. return false;
  65. }
  66. return true;
  67. }
  68. } // namespace perception
  69. } // namespace vision
  70. } // namespace ultra_infer