main.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright (c) 2020 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 "opencv2/core.hpp"
  15. #include "opencv2/imgcodecs.hpp"
  16. #include "opencv2/imgproc.hpp"
  17. #include <iostream>
  18. #include <vector>
  19. #include <include/args.h>
  20. #include <include/paddleocr.h>
  21. #include <include/paddlestructure.h>
  22. using namespace PaddleOCR;
  23. void check_params() {
  24. if (FLAGS_det) {
  25. if (FLAGS_det_model_dir.empty() || FLAGS_image_dir.empty()) {
  26. std::cout << "Usage[det]: ./ppocr "
  27. "--det_model_dir=/PATH/TO/DET_INFERENCE_MODEL/ "
  28. << "--image_dir=/PATH/TO/INPUT/IMAGE/" << std::endl;
  29. exit(1);
  30. }
  31. }
  32. if (FLAGS_rec) {
  33. std::cout
  34. << "In PP-OCRv3, rec_image_shape parameter defaults to '3, 48, 320',"
  35. "if you are using recognition model with PP-OCRv2 or an older "
  36. "version, "
  37. "please set --rec_image_shape='3,32,320"
  38. << std::endl;
  39. if (FLAGS_rec_model_dir.empty() || FLAGS_image_dir.empty()) {
  40. std::cout << "Usage[rec]: ./ppocr "
  41. "--rec_model_dir=/PATH/TO/REC_INFERENCE_MODEL/ "
  42. << "--image_dir=/PATH/TO/INPUT/IMAGE/" << std::endl;
  43. exit(1);
  44. }
  45. }
  46. if (FLAGS_cls && FLAGS_use_angle_cls) {
  47. if (FLAGS_cls_model_dir.empty() || FLAGS_image_dir.empty()) {
  48. std::cout << "Usage[cls]: ./ppocr "
  49. << "--cls_model_dir=/PATH/TO/REC_INFERENCE_MODEL/ "
  50. << "--image_dir=/PATH/TO/INPUT/IMAGE/" << std::endl;
  51. exit(1);
  52. }
  53. }
  54. if (FLAGS_table) {
  55. if (FLAGS_table_model_dir.empty() || FLAGS_det_model_dir.empty() ||
  56. FLAGS_rec_model_dir.empty() || FLAGS_image_dir.empty()) {
  57. std::cout << "Usage[table]: ./ppocr "
  58. << "--det_model_dir=/PATH/TO/DET_INFERENCE_MODEL/ "
  59. << "--rec_model_dir=/PATH/TO/REC_INFERENCE_MODEL/ "
  60. << "--table_model_dir=/PATH/TO/TABLE_INFERENCE_MODEL/ "
  61. << "--image_dir=/PATH/TO/INPUT/IMAGE/" << std::endl;
  62. exit(1);
  63. }
  64. }
  65. if (FLAGS_layout) {
  66. if (FLAGS_layout_model_dir.empty() || FLAGS_image_dir.empty()) {
  67. std::cout << "Usage[layout]: ./ppocr "
  68. << "--layout_model_dir=/PATH/TO/LAYOUT_INFERENCE_MODEL/ "
  69. << "--image_dir=/PATH/TO/INPUT/IMAGE/" << std::endl;
  70. exit(1);
  71. }
  72. }
  73. if (FLAGS_precision != "fp32" && FLAGS_precision != "fp16" &&
  74. FLAGS_precision != "int8") {
  75. std::cout << "precison should be 'fp32'(default), 'fp16' or 'int8'. "
  76. << std::endl;
  77. exit(1);
  78. }
  79. }
  80. void ocr(std::vector<cv::String> &cv_all_img_names) {
  81. PPOCR ocr = PPOCR();
  82. if (FLAGS_benchmark) {
  83. ocr.reset_timer();
  84. }
  85. std::vector<cv::Mat> img_list;
  86. std::vector<cv::String> img_names;
  87. for (int i = 0; i < cv_all_img_names.size(); ++i) {
  88. cv::Mat img = cv::imread(cv_all_img_names[i], cv::IMREAD_COLOR);
  89. if (!img.data) {
  90. std::cerr << "[ERROR] image read failed! image path: "
  91. << cv_all_img_names[i] << std::endl;
  92. continue;
  93. }
  94. img_list.push_back(img);
  95. img_names.push_back(cv_all_img_names[i]);
  96. }
  97. std::vector<std::vector<OCRPredictResult>> ocr_results =
  98. ocr.ocr(img_list, FLAGS_det, FLAGS_rec, FLAGS_cls);
  99. for (int i = 0; i < img_names.size(); ++i) {
  100. std::cout << "predict img: " << cv_all_img_names[i] << std::endl;
  101. Utility::print_result(ocr_results[i]);
  102. if (FLAGS_visualize && FLAGS_det) {
  103. std::string file_name = Utility::basename(img_names[i]);
  104. cv::Mat srcimg = img_list[i];
  105. Utility::VisualizeBboxes(srcimg, ocr_results[i],
  106. FLAGS_output + "/" + file_name);
  107. }
  108. }
  109. if (FLAGS_benchmark) {
  110. ocr.benchmark_log(cv_all_img_names.size());
  111. }
  112. }
  113. void structure(std::vector<cv::String> &cv_all_img_names) {
  114. PaddleOCR::PaddleStructure engine = PaddleOCR::PaddleStructure();
  115. if (FLAGS_benchmark) {
  116. engine.reset_timer();
  117. }
  118. for (int i = 0; i < cv_all_img_names.size(); i++) {
  119. std::cout << "predict img: " << cv_all_img_names[i] << std::endl;
  120. cv::Mat img = cv::imread(cv_all_img_names[i], cv::IMREAD_COLOR);
  121. if (!img.data) {
  122. std::cerr << "[ERROR] image read failed! image path: "
  123. << cv_all_img_names[i] << std::endl;
  124. continue;
  125. }
  126. std::vector<StructurePredictResult> structure_results = engine.structure(
  127. img, FLAGS_layout, FLAGS_table, FLAGS_det && FLAGS_rec);
  128. for (int j = 0; j < structure_results.size(); j++) {
  129. std::cout << j << "\ttype: " << structure_results[j].type
  130. << ", region: [";
  131. std::cout << structure_results[j].box[0] << ","
  132. << structure_results[j].box[1] << ","
  133. << structure_results[j].box[2] << ","
  134. << structure_results[j].box[3] << "], score: ";
  135. std::cout << structure_results[j].confidence << ", res: ";
  136. if (structure_results[j].type == "table") {
  137. std::cout << structure_results[j].html << std::endl;
  138. if (structure_results[j].cell_box.size() > 0 && FLAGS_visualize) {
  139. std::string file_name = Utility::basename(cv_all_img_names[i]);
  140. Utility::VisualizeBboxes(img, structure_results[j],
  141. FLAGS_output + "/" + std::to_string(j) +
  142. "_" + file_name);
  143. }
  144. } else {
  145. std::cout << "count of ocr result is : "
  146. << structure_results[j].text_res.size() << std::endl;
  147. if (structure_results[j].text_res.size() > 0) {
  148. std::cout << "********** print ocr result "
  149. << "**********" << std::endl;
  150. Utility::print_result(structure_results[j].text_res);
  151. std::cout << "********** end print ocr result "
  152. << "**********" << std::endl;
  153. }
  154. }
  155. }
  156. }
  157. if (FLAGS_benchmark) {
  158. engine.benchmark_log(cv_all_img_names.size());
  159. }
  160. }
  161. int main(int argc, char **argv) {
  162. // Parsing command-line
  163. google::ParseCommandLineFlags(&argc, &argv, true);
  164. check_params();
  165. if (!Utility::PathExists(FLAGS_image_dir)) {
  166. std::cerr << "[ERROR] image path not exist! image_dir: " << FLAGS_image_dir
  167. << std::endl;
  168. exit(1);
  169. }
  170. std::vector<cv::String> cv_all_img_names;
  171. cv::glob(FLAGS_image_dir, cv_all_img_names);
  172. std::cout << "total images num: " << cv_all_img_names.size() << std::endl;
  173. if (!Utility::PathExists(FLAGS_output)) {
  174. Utility::CreateDir(FLAGS_output);
  175. }
  176. if (FLAGS_type == "ocr") {
  177. ocr(cv_all_img_names);
  178. } else if (FLAGS_type == "structure") {
  179. structure(cv_all_img_names);
  180. } else {
  181. std::cout << "only value in ['ocr','structure'] is supported" << std::endl;
  182. }
  183. }