det_postprocessor.cc 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/ocr/ppocr/det_postprocessor.h"
  15. #include "ultra_infer/utils/perf.h"
  16. #include "ultra_infer/vision/ocr/ppocr/utils/ocr_utils.h"
  17. namespace ultra_infer {
  18. namespace vision {
  19. namespace ocr {
  20. bool DBDetectorPostprocessor::SingleBatchPostprocessor(
  21. const float *out_data, int n2, int n3,
  22. const std::array<int, 4> &det_img_info,
  23. std::vector<std::array<int, 8>> *boxes_result) {
  24. int n = n2 * n3;
  25. // prepare bitmap
  26. std::vector<float> pred(n, 0.0);
  27. std::vector<unsigned char> cbuf(n, ' ');
  28. for (int i = 0; i < n; i++) {
  29. pred[i] = float(out_data[i]);
  30. cbuf[i] = (unsigned char)((out_data[i]) * 255);
  31. }
  32. cv::Mat cbuf_map(n2, n3, CV_8UC1, (unsigned char *)cbuf.data());
  33. cv::Mat pred_map(n2, n3, CV_32F, (float *)pred.data());
  34. const double threshold = det_db_thresh_ * 255;
  35. const double maxvalue = 255;
  36. cv::Mat bit_map;
  37. cv::threshold(cbuf_map, bit_map, threshold, maxvalue, cv::THRESH_BINARY);
  38. if (use_dilation_) {
  39. cv::Mat dila_ele =
  40. cv::getStructuringElement(cv::MORPH_RECT, cv::Size(2, 2));
  41. cv::dilate(bit_map, bit_map, dila_ele);
  42. }
  43. std::vector<std::vector<std::vector<int>>> boxes;
  44. boxes = util_post_processor_.BoxesFromBitmap(
  45. pred_map, bit_map, det_db_box_thresh_, det_db_unclip_ratio_,
  46. det_db_score_mode_);
  47. boxes = util_post_processor_.FilterTagDetRes(boxes, det_img_info);
  48. // boxes to boxes_result
  49. for (int i = 0; i < boxes.size(); i++) {
  50. std::array<int, 8> new_box;
  51. int k = 0;
  52. for (auto &vec : boxes[i]) {
  53. for (auto &e : vec) {
  54. new_box[k++] = e;
  55. }
  56. }
  57. boxes_result->emplace_back(new_box);
  58. }
  59. return true;
  60. }
  61. bool DBDetectorPostprocessor::Run(
  62. const std::vector<FDTensor> &tensors,
  63. std::vector<std::vector<std::array<int, 8>>> *results,
  64. const std::vector<std::array<int, 4>> &batch_det_img_info) {
  65. // DBDetector have only 1 output tensor.
  66. const FDTensor &tensor = tensors[0];
  67. // For DBDetector, the output tensor shape = [batch, 1, ?, ?]
  68. size_t batch = tensor.shape[0];
  69. size_t length = accumulate(tensor.shape.begin() + 1, tensor.shape.end(), 1,
  70. std::multiplies<int>());
  71. const float *tensor_data = reinterpret_cast<const float *>(tensor.Data());
  72. results->resize(batch);
  73. for (int i_batch = 0; i_batch < batch; ++i_batch) {
  74. SingleBatchPostprocessor(tensor_data, tensor.shape[2], tensor.shape[3],
  75. batch_det_img_info[i_batch],
  76. &results->at(i_batch));
  77. tensor_data = tensor_data + length;
  78. }
  79. return true;
  80. }
  81. } // namespace ocr
  82. } // namespace vision
  83. } // namespace ultra_infer