base.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/common/processors/base.h"
  15. #include "ultra_infer/utils/utils.h"
  16. #include "ultra_infer/vision/common/processors/proc_lib.h"
  17. namespace ultra_infer {
  18. namespace vision {
  19. bool Processor::ImplByOpenCV(FDMat *mat) {
  20. FDERROR << Name() << " Not Implement Yet." << std::endl;
  21. return false;
  22. }
  23. bool Processor::ImplByOpenCV(FDMatBatch *mat_batch) {
  24. for (size_t i = 0; i < mat_batch->mats->size(); ++i) {
  25. if (ImplByOpenCV(&(*(mat_batch->mats))[i]) != true) {
  26. return false;
  27. }
  28. }
  29. return true;
  30. }
  31. bool Processor::ImplByFlyCV(FDMat *mat) { return ImplByOpenCV(mat); }
  32. bool Processor::ImplByFlyCV(FDMatBatch *mat_batch) {
  33. for (size_t i = 0; i < mat_batch->mats->size(); ++i) {
  34. if (ImplByFlyCV(&(*(mat_batch->mats))[i]) != true) {
  35. return false;
  36. }
  37. }
  38. return true;
  39. }
  40. bool Processor::ImplByCuda(FDMat *mat) {
  41. FDWARNING << Name()
  42. << " is not implemented with CUDA, will fallback to OpenCV."
  43. << std::endl;
  44. return ImplByOpenCV(mat);
  45. }
  46. bool Processor::ImplByCuda(FDMatBatch *mat_batch) {
  47. for (size_t i = 0; i < mat_batch->mats->size(); ++i) {
  48. if (ImplByCuda(&(*(mat_batch->mats))[i]) != true) {
  49. return false;
  50. }
  51. }
  52. return true;
  53. }
  54. bool Processor::ImplByCvCuda(FDMat *mat) {
  55. FDWARNING << Name()
  56. << " is not implemented with CV-CUDA, will fallback to OpenCV."
  57. << std::endl;
  58. return ImplByOpenCV(mat);
  59. }
  60. bool Processor::ImplByCvCuda(FDMatBatch *mat_batch) {
  61. for (size_t i = 0; i < mat_batch->mats->size(); ++i) {
  62. if (ImplByCvCuda(&(*(mat_batch->mats))[i]) != true) {
  63. return false;
  64. }
  65. }
  66. return true;
  67. }
  68. bool Processor::operator()(FDMat *mat) {
  69. ProcLib target = mat->proc_lib;
  70. if (mat->proc_lib == ProcLib::DEFAULT) {
  71. target = DefaultProcLib::default_lib;
  72. }
  73. if (target == ProcLib::FLYCV) {
  74. #ifdef ENABLE_FLYCV
  75. return ImplByFlyCV(mat);
  76. #else
  77. FDASSERT(false, "UltraInfer didn't compile with FlyCV.");
  78. #endif
  79. } else if (target == ProcLib::CUDA) {
  80. #ifdef WITH_GPU
  81. FDASSERT(mat->Stream() != nullptr,
  82. "CUDA processor requires cuda stream, please set stream for Mat");
  83. return ImplByCuda(mat);
  84. #else
  85. FDASSERT(false, "UltraInfer didn't compile with WITH_GPU.");
  86. #endif
  87. } else if (target == ProcLib::CVCUDA) {
  88. #ifdef ENABLE_CVCUDA
  89. FDASSERT(mat->Stream() != nullptr,
  90. "CV-CUDA requires cuda stream, please set stream for Mat");
  91. return ImplByCvCuda(mat);
  92. #else
  93. FDASSERT(false, "UltraInfer didn't compile with CV-CUDA.");
  94. #endif
  95. }
  96. // DEFAULT & OPENCV
  97. return ImplByOpenCV(mat);
  98. }
  99. bool Processor::operator()(FDMat *mat, ProcLib lib) {
  100. mat->proc_lib = lib;
  101. return operator()(mat);
  102. }
  103. bool Processor::operator()(FDMatBatch *mat_batch) {
  104. ProcLib target = mat_batch->proc_lib;
  105. if (mat_batch->proc_lib == ProcLib::DEFAULT) {
  106. target = DefaultProcLib::default_lib;
  107. }
  108. if (target == ProcLib::FLYCV) {
  109. #ifdef ENABLE_FLYCV
  110. return ImplByFlyCV(mat_batch);
  111. #else
  112. FDASSERT(false, "UltraInfer didn't compile with FlyCV.");
  113. #endif
  114. } else if (target == ProcLib::CUDA) {
  115. #ifdef WITH_GPU
  116. FDASSERT(
  117. mat_batch->Stream() != nullptr,
  118. "CUDA processor requires cuda stream, please set stream for mat_batch");
  119. return ImplByCuda(mat_batch);
  120. #else
  121. FDASSERT(false, "UltraInfer didn't compile with WITH_GPU.");
  122. #endif
  123. } else if (target == ProcLib::CVCUDA) {
  124. #ifdef ENABLE_CVCUDA
  125. FDASSERT(mat_batch->Stream() != nullptr,
  126. "CV-CUDA processor requires cuda stream, please set stream for "
  127. "mat_batch");
  128. return ImplByCvCuda(mat_batch);
  129. #else
  130. FDASSERT(false, "UltraInfer didn't compile with CV-CUDA.");
  131. #endif
  132. }
  133. // DEFAULT & OPENCV
  134. return ImplByOpenCV(mat_batch);
  135. }
  136. void EnableFlyCV() {
  137. #ifdef ENABLE_FLYCV
  138. DefaultProcLib::default_lib = ProcLib::FLYCV;
  139. FDINFO << "Will change to use image processing library "
  140. << DefaultProcLib::default_lib << std::endl;
  141. #else
  142. FDWARNING << "UltraInfer didn't compile with FlyCV, "
  143. "will fallback to use OpenCV instead."
  144. << std::endl;
  145. #endif
  146. }
  147. void DisableFlyCV() {
  148. DefaultProcLib::default_lib = ProcLib::OPENCV;
  149. FDINFO << "Will change to use image processing library "
  150. << DefaultProcLib::default_lib << std::endl;
  151. }
  152. void SetProcLibCpuNumThreads(int threads) {
  153. cv::setNumThreads(threads);
  154. #ifdef ENABLE_FLYCV
  155. fcv::set_thread_num(threads);
  156. #endif
  157. }
  158. } // namespace vision
  159. } // namespace ultra_infer