"git@developer.sourcefind.cn:chenpangpang/open-webui.git" did not exist on "bb979c9a78d055a928abfa34ace64f7a2501c7e6"
main.cpp 3.53 KB
Newer Older
MissPenguin's avatar
MissPenguin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 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 "opencv2/core.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <vector>

WenmuZhou's avatar
WenmuZhou committed
20
#include <include/args.h>
21
#include <include/paddleocr.h>
MissPenguin's avatar
MissPenguin committed
22
23
24

using namespace PaddleOCR;

25
26
void check_params() {
  if (FLAGS_det) {
WenmuZhou's avatar
WenmuZhou committed
27
28
29
30
31
    if (FLAGS_det_model_dir.empty() || FLAGS_image_dir.empty()) {
      std::cout << "Usage[det]: ./ppocr "
                   "--det_model_dir=/PATH/TO/DET_INFERENCE_MODEL/ "
                << "--image_dir=/PATH/TO/INPUT/IMAGE/" << std::endl;
      exit(1);
MissPenguin's avatar
MissPenguin committed
32
    }
WenmuZhou's avatar
WenmuZhou committed
33
  }
34
  if (FLAGS_rec) {
WenmuZhou's avatar
WenmuZhou committed
35
36
37
38
39
    if (FLAGS_rec_model_dir.empty() || FLAGS_image_dir.empty()) {
      std::cout << "Usage[rec]: ./ppocr "
                   "--rec_model_dir=/PATH/TO/REC_INFERENCE_MODEL/ "
                << "--image_dir=/PATH/TO/INPUT/IMAGE/" << std::endl;
      exit(1);
MissPenguin's avatar
MissPenguin committed
40
    }
WenmuZhou's avatar
WenmuZhou committed
41
  }
42
43
44
45
  if (FLAGS_cls && FLAGS_use_angle_cls) {
    if (FLAGS_cls_model_dir.empty() || FLAGS_image_dir.empty()) {
      std::cout << "Usage[cls]: ./ppocr "
                << "--cls_model_dir=/PATH/TO/REC_INFERENCE_MODEL/ "
WenmuZhou's avatar
WenmuZhou committed
46
47
                << "--image_dir=/PATH/TO/INPUT/IMAGE/" << std::endl;
      exit(1);
MissPenguin's avatar
MissPenguin committed
48
    }
WenmuZhou's avatar
WenmuZhou committed
49
50
51
52
53
54
  }
  if (FLAGS_precision != "fp32" && FLAGS_precision != "fp16" &&
      FLAGS_precision != "int8") {
    cout << "precison should be 'fp32'(default), 'fp16' or 'int8'. " << endl;
    exit(1);
  }
MissPenguin's avatar
MissPenguin committed
55
56
}

MissPenguin's avatar
MissPenguin committed
57
int main(int argc, char **argv) {
WenmuZhou's avatar
WenmuZhou committed
58
59
  // Parsing command-line
  google::ParseCommandLineFlags(&argc, &argv, true);
60
  check_params();
WenmuZhou's avatar
WenmuZhou committed
61

62
  if (!Utility::PathExists(FLAGS_image_dir)) {
WenmuZhou's avatar
WenmuZhou committed
63
64
65
66
67
68
69
70
    std::cerr << "[ERROR] image path not exist! image_dir: " << FLAGS_image_dir
              << endl;
    exit(1);
  }

  std::vector<cv::String> cv_all_img_names;
  cv::glob(FLAGS_image_dir, cv_all_img_names);
  std::cout << "total images num: " << cv_all_img_names.size() << endl;
MissPenguin's avatar
MissPenguin committed
71

WenmuZhou's avatar
WenmuZhou committed
72
  PPOCR ocr = PPOCR();
73
74
75
76
77

  std::vector<std::vector<OCRPredictResult>> ocr_results =
      ocr.ocr(cv_all_img_names, FLAGS_det, FLAGS_rec, FLAGS_cls);

  for (int i = 0; i < cv_all_img_names.size(); ++i) {
WenmuZhou's avatar
WenmuZhou committed
78
79
80
81
82
83
84
    if (FLAGS_benchmark) {
      cout << cv_all_img_names[i] << '\t';
      for (int n = 0; n < ocr_results[i].size(); n++) {
        for (int m = 0; m < ocr_results[i][n].box.size(); m++) {
          cout << ocr_results[i][n].box[m][0] << ' '
               << ocr_results[i][n].box[m][1] << ' ';
        }
85
      }
WenmuZhou's avatar
WenmuZhou committed
86
87
88
89
90
91
92
93
94
95
96
97
      cout << endl;
    } else {
      cout << cv_all_img_names[i] << "\n";
      Utility::print_result(ocr_results[i]);
      if (FLAGS_visualize && FLAGS_det) {
        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] << endl;
          exit(1);
        }
        std::string file_name = Utility::basename(cv_all_img_names[i]);
98

WenmuZhou's avatar
WenmuZhou committed
99
100
101
102
        Utility::VisualizeBboxes(srcimg, ocr_results[i],
                                 FLAGS_output + "/" + file_name);
      }
      cout << "***************************" << endl;
103
    }
WenmuZhou's avatar
WenmuZhou committed
104
  }
MissPenguin's avatar
MissPenguin committed
105
}