ocr_db_crnn.cc 22.7 KB
Newer Older
W
WenmuZhou 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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 <chrono>
C
cuicheng01 已提交
16 17
#include "paddle_api.h" // NOLINT
#include "paddle_place.h"
W
WenmuZhou 已提交
18 19 20 21

#include "cls_process.h"
#include "crnn_process.h"
#include "db_post_process.h"
C
cuicheng01 已提交
22
#include "AutoLog/auto_log/lite_autolog.h"
W
WenmuZhou 已提交
23 24 25 26 27 28 29 30 31

using namespace paddle::lite_api; // NOLINT
using namespace std;

// fill tensor with mean and scale and trans layout: nhwc -> nchw, neon speed up
void NeonMeanScale(const float *din, float *dout, int size,
                   const std::vector<float> mean,
                   const std::vector<float> scale) {
  if (mean.size() != 3 || scale.size() != 3) {
C
cuicheng01 已提交
32
    std::cerr << "[ERROR] mean or scale size must equal to 3" << std::endl;
W
WenmuZhou 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    exit(1);
  }
  float32x4_t vmean0 = vdupq_n_f32(mean[0]);
  float32x4_t vmean1 = vdupq_n_f32(mean[1]);
  float32x4_t vmean2 = vdupq_n_f32(mean[2]);
  float32x4_t vscale0 = vdupq_n_f32(scale[0]);
  float32x4_t vscale1 = vdupq_n_f32(scale[1]);
  float32x4_t vscale2 = vdupq_n_f32(scale[2]);

  float *dout_c0 = dout;
  float *dout_c1 = dout + size;
  float *dout_c2 = dout + size * 2;

  int i = 0;
  for (; i < size - 3; i += 4) {
    float32x4x3_t vin3 = vld3q_f32(din);
    float32x4_t vsub0 = vsubq_f32(vin3.val[0], vmean0);
    float32x4_t vsub1 = vsubq_f32(vin3.val[1], vmean1);
    float32x4_t vsub2 = vsubq_f32(vin3.val[2], vmean2);
    float32x4_t vs0 = vmulq_f32(vsub0, vscale0);
    float32x4_t vs1 = vmulq_f32(vsub1, vscale1);
    float32x4_t vs2 = vmulq_f32(vsub2, vscale2);
    vst1q_f32(dout_c0, vs0);
    vst1q_f32(dout_c1, vs1);
    vst1q_f32(dout_c2, vs2);

    din += 12;
    dout_c0 += 4;
    dout_c1 += 4;
    dout_c2 += 4;
  }
  for (; i < size; i++) {
    *(dout_c0++) = (*(din++) - mean[0]) * scale[0];
    *(dout_c1++) = (*(din++) - mean[1]) * scale[1];
    *(dout_c2++) = (*(din++) - mean[2]) * scale[2];
  }
}

// resize image to a size multiple of 32 which is required by the network
cv::Mat DetResizeImg(const cv::Mat img, int max_size_len,
                     std::vector<float> &ratio_hw) {
  int w = img.cols;
  int h = img.rows;

  float ratio = 1.f;
  int max_wh = w >= h ? w : h;
  if (max_wh > max_size_len) {
    if (h > w) {
      ratio = static_cast<float>(max_size_len) / static_cast<float>(h);
    } else {
      ratio = static_cast<float>(max_size_len) / static_cast<float>(w);
    }
  }

  int resize_h = static_cast<int>(float(h) * ratio);
  int resize_w = static_cast<int>(float(w) * ratio);
  if (resize_h % 32 == 0)
    resize_h = resize_h;
  else if (resize_h / 32 < 1 + 1e-5)
    resize_h = 32;
  else
    resize_h = (resize_h / 32 - 1) * 32;

  if (resize_w % 32 == 0)
    resize_w = resize_w;
  else if (resize_w / 32 < 1 + 1e-5)
    resize_w = 32;
  else
    resize_w = (resize_w / 32 - 1) * 32;

  cv::Mat resize_img;
  cv::resize(img, resize_img, cv::Size(resize_w, resize_h));

  ratio_hw.push_back(static_cast<float>(resize_h) / static_cast<float>(h));
  ratio_hw.push_back(static_cast<float>(resize_w) / static_cast<float>(w));
  return resize_img;
}

cv::Mat RunClsModel(cv::Mat img, std::shared_ptr<PaddlePredictor> predictor_cls,
                    const float thresh = 0.9) {
  std::vector<float> mean = {0.5f, 0.5f, 0.5f};
  std::vector<float> scale = {1 / 0.5f, 1 / 0.5f, 1 / 0.5f};

  cv::Mat srcimg;
  img.copyTo(srcimg);
  cv::Mat crop_img;
  img.copyTo(crop_img);
  cv::Mat resize_img;

  int index = 0;
  float wh_ratio =
      static_cast<float>(crop_img.cols) / static_cast<float>(crop_img.rows);

  resize_img = ClsResizeImg(crop_img);
  resize_img.convertTo(resize_img, CV_32FC3, 1 / 255.f);

  const float *dimg = reinterpret_cast<const float *>(resize_img.data);

  std::unique_ptr<Tensor> input_tensor0(std::move(predictor_cls->GetInput(0)));
  input_tensor0->Resize({1, 3, resize_img.rows, resize_img.cols});
  auto *data0 = input_tensor0->mutable_data<float>();

  NeonMeanScale(dimg, data0, resize_img.rows * resize_img.cols, mean, scale);
  // Run CLS predictor
  predictor_cls->Run();

  // Get output and run postprocess
  std::unique_ptr<const Tensor> softmax_out(
      std::move(predictor_cls->GetOutput(0)));
  auto *softmax_scores = softmax_out->mutable_data<float>();
  auto softmax_out_shape = softmax_out->shape();
  float score = 0;
  int label = 0;
  for (int i = 0; i < softmax_out_shape[1]; i++) {
    if (softmax_scores[i] > score) {
      score = softmax_scores[i];
      label = i;
    }
  }
  if (label % 2 == 1 && score > thresh) {
    cv::rotate(srcimg, srcimg, 1);
  }
  return srcimg;
}

void RunRecModel(std::vector<std::vector<std::vector<int>>> boxes, cv::Mat img,
                 std::shared_ptr<PaddlePredictor> predictor_crnn,
                 std::vector<std::string> &rec_text,
                 std::vector<float> &rec_text_score,
                 std::vector<std::string> charactor_dict,
                 std::shared_ptr<PaddlePredictor> predictor_cls,
C
cuicheng01 已提交
164
                 int use_direction_classify,
L
LDOUBLEV 已提交
165 166
                 std::vector<double> *times,
                 int rec_image_height) {
W
WenmuZhou 已提交
167 168 169 170 171 172 173 174 175
  std::vector<float> mean = {0.5f, 0.5f, 0.5f};
  std::vector<float> scale = {1 / 0.5f, 1 / 0.5f, 1 / 0.5f};

  cv::Mat srcimg;
  img.copyTo(srcimg);
  cv::Mat crop_img;
  cv::Mat resize_img;

  int index = 0;
176 177

  std::vector<double> time_info = {0, 0, 0};
W
WenmuZhou 已提交
178
  for (int i = boxes.size() - 1; i >= 0; i--) {
179
    auto preprocess_start = std::chrono::steady_clock::now();
W
WenmuZhou 已提交
180 181 182 183 184 185 186
    crop_img = GetRotateCropImage(srcimg, boxes[i]);
    if (use_direction_classify >= 1) {
      crop_img = RunClsModel(crop_img, predictor_cls);
    }
    float wh_ratio =
        static_cast<float>(crop_img.cols) / static_cast<float>(crop_img.rows);

L
LDOUBLEV 已提交
187
    resize_img = CrnnResizeImg(crop_img, wh_ratio, rec_image_height);
W
WenmuZhou 已提交
188 189 190 191 192 193 194 195 196 197
    resize_img.convertTo(resize_img, CV_32FC3, 1 / 255.f);

    const float *dimg = reinterpret_cast<const float *>(resize_img.data);

    std::unique_ptr<Tensor> input_tensor0(
        std::move(predictor_crnn->GetInput(0)));
    input_tensor0->Resize({1, 3, resize_img.rows, resize_img.cols});
    auto *data0 = input_tensor0->mutable_data<float>();

    NeonMeanScale(dimg, data0, resize_img.rows * resize_img.cols, mean, scale);
198
    auto preprocess_end = std::chrono::steady_clock::now();
W
WenmuZhou 已提交
199
    //// Run CRNN predictor
200
    auto inference_start = std::chrono::steady_clock::now();
W
WenmuZhou 已提交
201 202 203 204 205 206 207
    predictor_crnn->Run();

    // Get output and run postprocess
    std::unique_ptr<const Tensor> output_tensor0(
        std::move(predictor_crnn->GetOutput(0)));
    auto *predict_batch = output_tensor0->data<float>();
    auto predict_shape = output_tensor0->shape();
208
    auto inference_end = std::chrono::steady_clock::now();
W
WenmuZhou 已提交
209 210

    // ctc decode
211
    auto postprocess_start = std::chrono::steady_clock::now();
W
WenmuZhou 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224
    std::string str_res;
    int argmax_idx;
    int last_index = 0;
    float score = 0.f;
    int count = 0;
    float max_value = 0.0f;

    for (int n = 0; n < predict_shape[1]; n++) {
      argmax_idx = int(Argmax(&predict_batch[n * predict_shape[2]],
                              &predict_batch[(n + 1) * predict_shape[2]]));
      max_value =
          float(*std::max_element(&predict_batch[n * predict_shape[2]],
                                  &predict_batch[(n + 1) * predict_shape[2]]));
W
WenmuZhou 已提交
225
      if (argmax_idx > 0 && (!(n > 0 && argmax_idx == last_index))) {
W
WenmuZhou 已提交
226 227 228 229 230 231 232 233 234
        score += max_value;
        count += 1;
        str_res += charactor_dict[argmax_idx];
      }
      last_index = argmax_idx;
    }
    score /= count;
    rec_text.push_back(str_res);
    rec_text_score.push_back(score);
235 236 237 238 239 240 241 242 243
    auto postprocess_end = std::chrono::steady_clock::now();

    std::chrono::duration<float> preprocess_diff = preprocess_end - preprocess_start;
    time_info[0] += double(preprocess_diff.count() * 1000);
    std::chrono::duration<float> inference_diff = inference_end - inference_start;
    time_info[1] += double(inference_diff.count() * 1000);
    std::chrono::duration<float> postprocess_diff = postprocess_end - postprocess_start;
    time_info[2] += double(postprocess_diff.count() * 1000);

W
WenmuZhou 已提交
244
  }
245 246 247 248

times->push_back(time_info[0]);
times->push_back(time_info[1]);
times->push_back(time_info[2]);
W
WenmuZhou 已提交
249 250 251 252
}

std::vector<std::vector<std::vector<int>>>
RunDetModel(std::shared_ptr<PaddlePredictor> predictor, cv::Mat img,
C
cuicheng01 已提交
253
            std::map<std::string, double> Config, std::vector<double> *times) {
W
WenmuZhou 已提交
254 255
  // Read img
  int max_side_len = int(Config["max_side_len"]);
W
WenmuZhou 已提交
256
  int det_db_use_dilate = int(Config["det_db_use_dilate"]);
W
WenmuZhou 已提交
257 258 259

  cv::Mat srcimg;
  img.copyTo(srcimg);
C
cuicheng01 已提交
260 261
  
  auto preprocess_start = std::chrono::steady_clock::now();
W
WenmuZhou 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275
  std::vector<float> ratio_hw;
  img = DetResizeImg(img, max_side_len, ratio_hw);
  cv::Mat img_fp;
  img.convertTo(img_fp, CV_32FC3, 1.0 / 255.f);

  // Prepare input data from image
  std::unique_ptr<Tensor> input_tensor0(std::move(predictor->GetInput(0)));
  input_tensor0->Resize({1, 3, img_fp.rows, img_fp.cols});
  auto *data0 = input_tensor0->mutable_data<float>();

  std::vector<float> mean = {0.485f, 0.456f, 0.406f};
  std::vector<float> scale = {1 / 0.229f, 1 / 0.224f, 1 / 0.225f};
  const float *dimg = reinterpret_cast<const float *>(img_fp.data);
  NeonMeanScale(dimg, data0, img_fp.rows * img_fp.cols, mean, scale);
C
cuicheng01 已提交
276
  auto preprocess_end = std::chrono::steady_clock::now();
W
WenmuZhou 已提交
277 278

  // Run predictor
C
cuicheng01 已提交
279
  auto inference_start = std::chrono::steady_clock::now();
W
WenmuZhou 已提交
280 281 282 283 284 285 286
  predictor->Run();

  // Get output and post process
  std::unique_ptr<const Tensor> output_tensor(
      std::move(predictor->GetOutput(0)));
  auto *outptr = output_tensor->data<float>();
  auto shape_out = output_tensor->shape();
C
cuicheng01 已提交
287
  auto inference_end = std::chrono::steady_clock::now();
W
WenmuZhou 已提交
288 289

  // Save output
C
cuicheng01 已提交
290
  auto postprocess_start = std::chrono::steady_clock::now();
W
WenmuZhou 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304
  float pred[shape_out[2] * shape_out[3]];
  unsigned char cbuf[shape_out[2] * shape_out[3]];

  for (int i = 0; i < int(shape_out[2] * shape_out[3]); i++) {
    pred[i] = static_cast<float>(outptr[i]);
    cbuf[i] = static_cast<unsigned char>((outptr[i]) * 255);
  }

  cv::Mat cbuf_map(shape_out[2], shape_out[3], CV_8UC1,
                   reinterpret_cast<unsigned char *>(cbuf));
  cv::Mat pred_map(shape_out[2], shape_out[3], CV_32F,
                   reinterpret_cast<float *>(pred));

  const double threshold = double(Config["det_db_thresh"]) * 255;
W
WenmuZhou 已提交
305
  const double max_value = 255;
W
WenmuZhou 已提交
306
  cv::Mat bit_map;
W
WenmuZhou 已提交
307
  cv::threshold(cbuf_map, bit_map, threshold, max_value, cv::THRESH_BINARY);
W
WenmuZhou 已提交
308 309 310 311 312 313 314 315
  if (det_db_use_dilate == 1) {
    cv::Mat dilation_map;
    cv::Mat dila_ele =
        cv::getStructuringElement(cv::MORPH_RECT, cv::Size(2, 2));
    cv::dilate(bit_map, dilation_map, dila_ele);
    bit_map = dilation_map;
  }
  auto boxes = BoxesFromBitmap(pred_map, bit_map, Config);
W
WenmuZhou 已提交
316 317 318

  std::vector<std::vector<std::vector<int>>> filter_boxes =
      FilterTagDetRes(boxes, ratio_hw[0], ratio_hw[1], srcimg);
C
cuicheng01 已提交
319 320 321 322 323 324 325 326
  auto postprocess_end = std::chrono::steady_clock::now();

  std::chrono::duration<float> preprocess_diff = preprocess_end - preprocess_start;
  times->push_back(double(preprocess_diff.count() * 1000));
  std::chrono::duration<float> inference_diff = inference_end - inference_start;
  times->push_back(double(inference_diff.count() * 1000));
  std::chrono::duration<float> postprocess_diff = postprocess_end - postprocess_start;
  times->push_back(double(postprocess_diff.count() * 1000));
W
WenmuZhou 已提交
327 328 329 330

  return filter_boxes;
}

C
cuicheng01 已提交
331
std::shared_ptr<PaddlePredictor> loadModel(std::string model_file, int num_threads) {
W
WenmuZhou 已提交
332 333 334
  MobileConfig config;
  config.set_model_from_file(model_file);

C
cuicheng01 已提交
335
  config.set_threads(num_threads);
W
WenmuZhou 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
  std::shared_ptr<PaddlePredictor> predictor =
      CreatePaddlePredictor<MobileConfig>(config);
  return predictor;
}

cv::Mat Visualization(cv::Mat srcimg,
                      std::vector<std::vector<std::vector<int>>> boxes) {
  cv::Point rook_points[boxes.size()][4];
  for (int n = 0; n < boxes.size(); n++) {
    for (int m = 0; m < boxes[0].size(); m++) {
      rook_points[n][m] = cv::Point(static_cast<int>(boxes[n][m][0]),
                                    static_cast<int>(boxes[n][m][1]));
    }
  }
  cv::Mat img_vis;
  srcimg.copyTo(img_vis);
  for (int n = 0; n < boxes.size(); n++) {
    const cv::Point *ppt[1] = {rook_points[n]};
    int npt[] = {4};
    cv::polylines(img_vis, ppt, npt, 1, 1, CV_RGB(0, 255, 0), 2, 8, 0);
  }

  cv::imwrite("./vis.jpg", img_vis);
  std::cout << "The detection visualized image saved in ./vis.jpg" << std::endl;
  return img_vis;
}

std::vector<std::string> split(const std::string &str,
                               const std::string &delim) {
  std::vector<std::string> res;
  if ("" == str)
    return res;
  char *strs = new char[str.length() + 1];
  std::strcpy(strs, str.c_str());

  char *d = new char[delim.length() + 1];
  std::strcpy(d, delim.c_str());

  char *p = std::strtok(strs, d);
  while (p) {
    string s = p;
    res.push_back(s);
    p = std::strtok(NULL, d);
  }

  return res;
}

std::map<std::string, double> LoadConfigTxt(std::string config_path) {
  auto config = ReadDict(config_path);

  std::map<std::string, double> dict;
  for (int i = 0; i < config.size(); i++) {
    std::vector<std::string> res = split(config[i], " ");
    dict[res[0]] = stod(res[1]);
  }
  return dict;
}

C
cuicheng01 已提交
395 396 397
void check_params(int argc, char **argv) {
  if (argc<=1 || (strcmp(argv[1], "det")!=0 && strcmp(argv[1], "rec")!=0 && strcmp(argv[1], "system")!=0)) {
    std::cerr << "Please choose one mode of [det, rec, system] !" << std::endl;
W
WenmuZhou 已提交
398 399
    exit(1);
  }
C
cuicheng01 已提交
400 401 402
  if (strcmp(argv[1], "det") == 0) {
      if (argc < 9){
        std::cerr << "[ERROR] usage:" << argv[0]
C
cuicheng01 已提交
403
                  << " det det_model runtime_device num_threads batchsize img_dir det_config lite_benchmark_value" << std::endl;
C
cuicheng01 已提交
404 405 406 407 408 409 410
        exit(1);
      }
  }

  if (strcmp(argv[1], "rec") == 0) {
      if (argc < 9){
        std::cerr << "[ERROR] usage:" << argv[0]
C
cuicheng01 已提交
411
                  << " rec rec_model runtime_device num_threads batchsize img_dir key_txt lite_benchmark_value" << std::endl;
C
cuicheng01 已提交
412 413 414 415 416 417 418
        exit(1);
      }
  }

  if (strcmp(argv[1], "system") == 0) {
      if (argc < 12){
        std::cerr << "[ERROR] usage:" << argv[0]
C
cuicheng01 已提交
419
                  << " system det_model rec_model clas_model runtime_device num_threads batchsize img_dir det_config key_txt lite_benchmark_value" << std::endl;
C
cuicheng01 已提交
420 421 422 423 424 425 426 427 428
        exit(1);
      }
  }
}

void system(char **argv){
  std::string det_model_file = argv[2];
  std::string rec_model_file = argv[3];
  std::string cls_model_file = argv[4];
C
cuicheng01 已提交
429 430 431 432
  std::string runtime_device = argv[5];
  std::string precision = argv[6];
  std::string num_threads = argv[7];
  std::string batchsize = argv[8];
C
cuicheng01 已提交
433 434 435 436
  std::string img_dir = argv[9];
  std::string det_config_path = argv[10];
  std::string dict_path = argv[11];

C
cuicheng01 已提交
437
  if (strcmp(argv[6], "FP32") != 0 && strcmp(argv[6], "INT8") != 0) {
C
cuicheng01 已提交
438 439 440 441 442 443
      std::cerr << "Only support FP32 or INT8." << std::endl;
      exit(1);
  }

  std::vector<cv::String> cv_all_img_names;
  cv::glob(img_dir, cv_all_img_names);
W
WenmuZhou 已提交
444 445

  //// load config from txt file
C
cuicheng01 已提交
446
  auto Config = LoadConfigTxt(det_config_path);
W
WenmuZhou 已提交
447
  int use_direction_classify = int(Config["use_direction_classify"]);
L
LDOUBLEV 已提交
448
  int rec_image_height = int(Config["rec_image_height"]);
C
cuicheng01 已提交
449 450 451 452
  auto charactor_dict = ReadDict(dict_path);
  charactor_dict.insert(charactor_dict.begin(), "#"); // blank char for ctc
  charactor_dict.push_back(" ");

C
cuicheng01 已提交
453 454 455
  auto det_predictor = loadModel(det_model_file, std::stoi(num_threads));
  auto rec_predictor = loadModel(rec_model_file, std::stoi(num_threads));
  auto cls_predictor = loadModel(cls_model_file, std::stoi(num_threads));
W
WenmuZhou 已提交
456

457 458 459
  std::vector<double> det_time_info = {0, 0, 0};
  std::vector<double> rec_time_info = {0, 0, 0};

C
cuicheng01 已提交
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
  for (int i = 0; i < cv_all_img_names.size(); ++i) {
    std::cout << "The predict img: " << cv_all_img_names[i] << std::endl;
    cv::Mat srcimg = cv::imread(cv_all_img_names[i], cv::IMREAD_COLOR);

    if (!srcimg.data) {
      std::cerr << "[ERROR] image read failed! image path: " << cv_all_img_names[i] << std::endl;
      exit(1);
    }

    std::vector<double> det_times;
    auto boxes = RunDetModel(det_predictor, srcimg, Config, &det_times);
  
    std::vector<std::string> rec_text;
    std::vector<float> rec_text_score;
  
    std::vector<double> rec_times;
    RunRecModel(boxes, srcimg, rec_predictor, rec_text, rec_text_score,
L
fix bug  
LDOUBLEV 已提交
477
                charactor_dict, cls_predictor, use_direction_classify, &rec_times, rec_image_height);
C
cuicheng01 已提交
478 479 480 481 482 483 484
  
    //// visualization
    auto img_vis = Visualization(srcimg, boxes);
  
    //// print recognized text
    for (int i = 0; i < rec_text.size(); i++) {
      std::cout << i << "\t" << rec_text[i] << "\t" << rec_text_score[i]
485 486
                <<  std::endl;

C
cuicheng01 已提交
487
    }
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516

    det_time_info[0] += det_times[0];
    det_time_info[1] += det_times[1];
    det_time_info[2] += det_times[2];
    rec_time_info[0] += rec_times[0];
    rec_time_info[1] += rec_times[1];
    rec_time_info[2] += rec_times[2];
  }
  if (strcmp(argv[12], "True") == 0) {
    AutoLogger autolog_det(det_model_file, 
                       runtime_device,
                       std::stoi(num_threads),
                       std::stoi(batchsize), 
                       "dynamic", 
                       precision, 
                       det_time_info, 
                       cv_all_img_names.size());
    AutoLogger autolog_rec(rec_model_file, 
                       runtime_device,
                       std::stoi(num_threads),
                       std::stoi(batchsize), 
                       "dynamic", 
                       precision, 
                       rec_time_info, 
                       cv_all_img_names.size());

    autolog_det.report();
    std::cout << std::endl;
    autolog_rec.report();
C
cuicheng01 已提交
517 518 519 520 521
  }
}

void det(int argc, char **argv) {
  std::string det_model_file = argv[2];
C
cuicheng01 已提交
522 523 524 525
  std::string runtime_device = argv[3];
  std::string precision = argv[4];
  std::string num_threads = argv[5];
  std::string batchsize = argv[6];
C
cuicheng01 已提交
526 527 528
  std::string img_dir = argv[7];
  std::string det_config_path = argv[8];

C
cuicheng01 已提交
529
  if (strcmp(argv[4], "FP32") != 0 && strcmp(argv[4], "INT8") != 0) {
C
cuicheng01 已提交
530 531 532 533 534 535 536 537 538 539
      std::cerr << "Only support FP32 or INT8." << std::endl;
      exit(1);
  }

  std::vector<cv::String> cv_all_img_names;
  cv::glob(img_dir, cv_all_img_names);

  //// load config from txt file
  auto Config = LoadConfigTxt(det_config_path);

C
cuicheng01 已提交
540
  auto det_predictor = loadModel(det_model_file, std::stoi(num_threads));
C
cuicheng01 已提交
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558

  std::vector<double> time_info = {0, 0, 0};
  for (int i = 0; i < cv_all_img_names.size(); ++i) {
    std::cout << "The predict img: " << cv_all_img_names[i] << std::endl;
    cv::Mat srcimg = cv::imread(cv_all_img_names[i], cv::IMREAD_COLOR);

    if (!srcimg.data) {
      std::cerr << "[ERROR] image read failed! image path: " << cv_all_img_names[i] << std::endl;
      exit(1);
    }

    std::vector<double> times;
    auto boxes = RunDetModel(det_predictor, srcimg, Config, &times);

    //// visualization
    auto img_vis = Visualization(srcimg, boxes);
    std::cout << boxes.size() << " bboxes have detected:" << std::endl;

559 560 561 562 563 564 565 566 567
    for (int i=0; i<boxes.size(); i++){
      std::cout << "The " << i << " box:" << std::endl;
      for (int j=0; j<4; j++){
        for (int k=0; k<2; k++){
          std::cout << boxes[i][j][k] << "\t";
        }
      }
      std::cout << std::endl;
    }
C
cuicheng01 已提交
568 569 570 571 572 573 574
    time_info[0] += times[0];
    time_info[1] += times[1];
    time_info[2] += times[2];
  }

  if (strcmp(argv[9], "True") == 0) {
    AutoLogger autolog(det_model_file, 
C
cuicheng01 已提交
575
                       runtime_device,
C
cuicheng01 已提交
576 577 578 579 580 581 582 583 584 585 586 587
                       std::stoi(num_threads),
                       std::stoi(batchsize), 
                       "dynamic", 
                       precision, 
                       time_info, 
                       cv_all_img_names.size());
    autolog.report();
  }
}

void rec(int argc, char **argv) {
  std::string rec_model_file = argv[2];
C
cuicheng01 已提交
588 589 590 591
  std::string runtime_device = argv[3];
  std::string precision = argv[4];
  std::string num_threads = argv[5];
  std::string batchsize = argv[6];
C
cuicheng01 已提交
592 593
  std::string img_dir = argv[7];
  std::string dict_path = argv[8];
L
LDOUBLEV 已提交
594
  std::string config_path = argv[9];
C
cuicheng01 已提交
595

C
cuicheng01 已提交
596
  if (strcmp(argv[4], "FP32") != 0 && strcmp(argv[4], "INT8") != 0) {
C
cuicheng01 已提交
597 598 599 600
      std::cerr << "Only support FP32 or INT8." << std::endl;
      exit(1);
  }

L
LDOUBLEV 已提交
601 602 603
  auto Config = LoadConfigTxt(config_path);
  int rec_image_height = int(Config["rec_image_height"]);

C
cuicheng01 已提交
604 605
  std::vector<cv::String> cv_all_img_names;
  cv::glob(img_dir, cv_all_img_names);
W
WenmuZhou 已提交
606 607 608 609

  auto charactor_dict = ReadDict(dict_path);
  charactor_dict.insert(charactor_dict.begin(), "#"); // blank char for ctc
  charactor_dict.push_back(" ");
W
WenmuZhou 已提交
610

C
cuicheng01 已提交
611
  auto rec_predictor = loadModel(rec_model_file, std::stoi(num_threads));
W
WenmuZhou 已提交
612

C
cuicheng01 已提交
613
  std::shared_ptr<PaddlePredictor> cls_predictor;
W
WenmuZhou 已提交
614

C
cuicheng01 已提交
615 616 617 618
  std::vector<double> time_info = {0, 0, 0};
  for (int i = 0; i < cv_all_img_names.size(); ++i) {
    std::cout << "The predict img: " << cv_all_img_names[i] << std::endl;
    cv::Mat srcimg = cv::imread(cv_all_img_names[i], cv::IMREAD_COLOR);
W
WenmuZhou 已提交
619

C
cuicheng01 已提交
620 621 622 623
    if (!srcimg.data) {
      std::cerr << "[ERROR] image read failed! image path: " << cv_all_img_names[i] << std::endl;
      exit(1);
    }
W
WenmuZhou 已提交
624

C
cuicheng01 已提交
625 626 627 628 629 630 631 632 633 634 635 636 637
    int width = srcimg.cols;
    int height = srcimg.rows;
    std::vector<int> upper_left = {0, 0};
    std::vector<int> upper_right = {width, 0};
    std::vector<int> lower_right = {width, height};
    std::vector<int> lower_left  = {0, height};
    std::vector<std::vector<int>> box = {upper_left, upper_right, lower_right, lower_left};
    std::vector<std::vector<std::vector<int>>> boxes = {box};

    std::vector<std::string> rec_text;
    std::vector<float> rec_text_score;
    std::vector<double> times;
    RunRecModel(boxes, srcimg, rec_predictor, rec_text, rec_text_score,
L
LDOUBLEV 已提交
638
                charactor_dict, cls_predictor, 0, &times, rec_image_height);
C
cuicheng01 已提交
639 640 641 642 643 644
  
    //// print recognized text
    for (int i = 0; i < rec_text.size(); i++) {
      std::cout << i << "\t" << rec_text[i] << "\t" << rec_text_score[i]
                << std::endl;
    }
645 646 647
    time_info[0] += times[0];
    time_info[1] += times[1];
    time_info[2] += times[2];
C
cuicheng01 已提交
648 649 650 651
  }
  // TODO: support autolog
  if (strcmp(argv[9], "True") == 0) {
    AutoLogger autolog(rec_model_file, 
C
cuicheng01 已提交
652
                       runtime_device,
C
cuicheng01 已提交
653 654 655 656 657 658 659 660 661 662 663 664 665
                       std::stoi(num_threads),
                       std::stoi(batchsize), 
                       "dynamic", 
                       precision, 
                       time_info, 
                       cv_all_img_names.size());
    autolog.report();
  }
}

int main(int argc, char **argv) {
  check_params(argc, argv);
  std::cout << "mode: " << argv[1] << endl;
W
WenmuZhou 已提交
666

C
cuicheng01 已提交
667 668
  if (strcmp(argv[1], "system") == 0) {
    system(argv);
W
WenmuZhou 已提交
669 670
  }

C
cuicheng01 已提交
671 672 673 674 675 676 677
  if (strcmp(argv[1], "det") == 0) {
    det(argc, argv);
  }

  if (strcmp(argv[1], "rec") == 0) {
    rec(argc, argv);
  }
W
WenmuZhou 已提交
678 679

  return 0;
C
cuicheng01 已提交
680
}