ocr_det.cpp 3.9 KB
Newer Older
littletomatodonkey's avatar
littletomatodonkey 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <include/ocr_det.h>

namespace PaddleOCR {

littletomatodonkey's avatar
littletomatodonkey 已提交
19
void DBDetector::LoadModel(const std::string &model_dir) {
littletomatodonkey's avatar
littletomatodonkey 已提交
20 21 22
  AnalysisConfig config;
  config.SetModel(model_dir + "/model", model_dir + "/params");

littletomatodonkey's avatar
littletomatodonkey 已提交
23 24 25 26
  if (this->use_gpu_) {
    config.EnableUseGpu(this->gpu_mem_, this->gpu_id_);
  } else {
    config.DisableGpu();
littletomatodonkey's avatar
littletomatodonkey 已提交
27 28 29
    if (this->use_mkldnn_) {
      config.EnableMKLDNN();
    }
littletomatodonkey's avatar
littletomatodonkey 已提交
30 31
    config.SetCpuMathLibraryNumThreads(this->cpu_math_library_num_threads_);
  }
littletomatodonkey's avatar
littletomatodonkey 已提交
32

littletomatodonkey's avatar
littletomatodonkey 已提交
33
  // false for zero copy tensor
34
  // true for commom tensor
35
  config.SwitchUseFeedFetchOps(!this->use_zero_copy_run_);
littletomatodonkey's avatar
littletomatodonkey 已提交
36
  // true for multiple input
littletomatodonkey's avatar
littletomatodonkey 已提交
37
  config.SwitchSpecifyInputNames(true);
littletomatodonkey's avatar
littletomatodonkey 已提交
38 39 40 41

  config.SwitchIrOptim(true);

  config.EnableMemoryOptim();
littletomatodonkey's avatar
littletomatodonkey 已提交
42
  config.DisableGlogInfo();
littletomatodonkey's avatar
littletomatodonkey 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

  this->predictor_ = CreatePaddlePredictor(config);
}

void DBDetector::Run(cv::Mat &img,
                     std::vector<std::vector<std::vector<int>>> &boxes) {
  float ratio_h{};
  float ratio_w{};

  cv::Mat srcimg;
  cv::Mat resize_img;
  img.copyTo(srcimg);
  this->resize_op_.Run(img, resize_img, this->max_side_len_, ratio_h, ratio_w);

  this->normalize_op_.Run(&resize_img, this->mean_, this->scale_,
                          this->is_scale_);

littletomatodonkey's avatar
littletomatodonkey 已提交
60 61
  std::vector<float> input(1 * 3 * resize_img.rows * resize_img.cols, 0.0f);
  this->permute_op_.Run(&resize_img, input.data());
littletomatodonkey's avatar
littletomatodonkey 已提交
62

63
  // Inference.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  if (this->use_zero_copy_run_) {
    auto input_names = this->predictor_->GetInputNames();
    auto input_t = this->predictor_->GetInputTensor(input_names[0]);
    input_t->Reshape({1, 3, resize_img.rows, resize_img.cols});
    input_t->copy_from_cpu(input.data());
    this->predictor_->ZeroCopyRun();
  } else {
    paddle::PaddleTensor input_t;
    input_t.shape = {1, 3, resize_img.rows, resize_img.cols};
    input_t.data =
        paddle::PaddleBuf(input.data(), input.size() * sizeof(float));
    input_t.dtype = PaddleDType::FLOAT32;
    std::vector<paddle::PaddleTensor> outputs;
    this->predictor_->Run({input_t}, &outputs, 1);
  }
littletomatodonkey's avatar
littletomatodonkey 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

  std::vector<float> out_data;
  auto output_names = this->predictor_->GetOutputNames();
  auto output_t = this->predictor_->GetOutputTensor(output_names[0]);
  std::vector<int> output_shape = output_t->shape();
  int out_num = std::accumulate(output_shape.begin(), output_shape.end(), 1,
                                std::multiplies<int>());

  out_data.resize(out_num);
  output_t->copy_to_cpu(out_data.data());

  int n2 = output_shape[2];
  int n3 = output_shape[3];
  int n = n2 * n3;

littletomatodonkey's avatar
littletomatodonkey 已提交
94 95
  std::vector<float> pred(n, 0.0);
  std::vector<unsigned char> cbuf(n, ' ');
littletomatodonkey's avatar
littletomatodonkey 已提交
96 97 98 99 100 101

  for (int i = 0; i < n; i++) {
    pred[i] = float(out_data[i]);
    cbuf[i] = (unsigned char)((out_data[i]) * 255);
  }

littletomatodonkey's avatar
littletomatodonkey 已提交
102 103
  cv::Mat cbuf_map(n2, n3, CV_8UC1, (unsigned char *)cbuf.data());
  cv::Mat pred_map(n2, n3, CV_32F, (float *)pred.data());
littletomatodonkey's avatar
littletomatodonkey 已提交
104

littletomatodonkey's avatar
littletomatodonkey 已提交
105
  const double threshold = this->det_db_thresh_ * 255;
littletomatodonkey's avatar
littletomatodonkey 已提交
106 107 108 109
  const double maxvalue = 255;
  cv::Mat bit_map;
  cv::threshold(cbuf_map, bit_map, threshold, maxvalue, cv::THRESH_BINARY);

littletomatodonkey's avatar
littletomatodonkey 已提交
110 111
  boxes = post_processor_.BoxesFromBitmap(
      pred_map, bit_map, this->det_db_box_thresh_, this->det_db_unclip_ratio_);
littletomatodonkey's avatar
littletomatodonkey 已提交
112

littletomatodonkey's avatar
littletomatodonkey 已提交
113
  boxes = post_processor_.FilterTagDetRes(boxes, ratio_h, ratio_w, srcimg);
littletomatodonkey's avatar
littletomatodonkey 已提交
114 115

  //// visualization
littletomatodonkey's avatar
littletomatodonkey 已提交
116 117
  if (this->visualize_) {
    Utility::VisualizeBboxes(srcimg, boxes);
littletomatodonkey's avatar
littletomatodonkey 已提交
118 119 120
  }
}

littletomatodonkey's avatar
littletomatodonkey 已提交
121
} // namespace PaddleOCR