PaddleOCR+OpenCV realizes Chinese and English recognition

I. Overview

2. Compile and modify

2.1 Preparations

  • Download the source code of PaddleOCRv2.0 .
  • Download the officially compiled three-party library PaddleOCR prediction library 2.0.2 . If the computer has CUDA that supports NVIDIA, you can download the CPU and GPU versions at the same time. When downloading the GPU version, pay attention to the version of CUDA and cudnn. At the same time, pay attention to the version.txt in the library file after decompression , which contains all the environment and dependent libraries when the library was compiled. The CPU version does not need special attention, but the GPU version needs to pay attention to the TensorRT version.
  • If you need to use the GPU version, make sure that the graphics card supports GPU, and install the corresponding driver, CUDA, cudnn, and TensorRT library.
  • Find the directory in the PaddleOCR source code “path\PaddleOCR-release-2.0\deploy\cpp_infer”, where the include directory is all header files, src is the source code to be used (main, cpp is the main function), and there is a config.txt template in the tools directory. There is a font file ppocr_keys_v1.txt"pathPaddleOCR-release-2.0\ppocr\utils" in the file .

2.2 Problems in compilation

  • 1. I won’t say much about using cmake or directly building the project. It’s so simple, who wouldn’t know it.
  • 2. If it is a manually built project, you need to manually add the corresponding header files and library files, that is, the downloaded PaddleOCR prediction library and the compiled OpenCV.
  • 3. During the compilation process, it will appear that the dirent.h file cannot be found, it does not matter, you can refer to this blog, create a new one, and then add it to the project. At the end of the article, the compiled project will also be given for everyone to test and use.
  • 4. The glog library may not be needed, because I reported an error when compiling, so I commented it out directly.
  • 5. There is a string allocation memory syntax error in config.cpp in VS017, which can be modified as follows:
  char* strs = new char[str.length() + 1];
  //char strs[str.length() + 1];
  std::strcpy(strs, str.c_str());

  char* d = new char[delim.length() + 1];
  //char d[delim.length() + 1];
  std::strcpy(d, delim.c_str());
  • 6. The lstat undefined identifier in utility.cpp in VS017 can be modified as follows:
  struct stat s;
  stat(dir_name, &s);
  • 7. The character recognition result output function of the code is simply modified, and the result is saved in txt.

3. Operation and results

For CPU version

  • First, you need to download the corresponding model file , either lightweight or normal. After decompression, put it in a directory you can specify at will. Note that each model can decompress three files.
  • The previous font file ppocr_keys_v1.txt and configuration file config.txt can also be placed in this directory for easy search. Pay attention to modify the path and settings of the model file in config.txt use_gpu 0.
  • Set the command parameters in the debug option in the project properties, such as:./cfg/config.txt ./imgs
  • Set the path of the dll in the environment option in the project properties, such as: path=../3rd_party/paddle_inference_install_cpu/bin;../3rd_party/opencv/bin;don’t forget the semicolon, otherwise the dll file will not be found.
  • When recognizing characters, if you use exe to run garbled characters, you can enter it in the terminal before executing exe CHCP 65001, and change the encoding method of the terminal from GBK encoding (default) to UTF-8 encoding.
  • If the garbled characters are output through the VS2017 console, I have tried many methods, but I don’t have a good solution so far, I just save the results in txt for comparison. Anyone who knows how to modify it can leave a message in the comment area for guidance.
  • Recognition result picture:
    insert image description here
    insert image description here
    insert image description here
    insert image description here
    insert image description here
    insert image description here
    insert image description here
    insert image description here
    insert image description here
  • The recognition effect of the text saved in txt is still very good, but the order of the output may not be consistent, not in the order from top to bottom. This needs to be modified according to the position of the rectangular frame.
    insert image description here
    For the program code test of GPU:
  • In fact, it is generally the same as the CPU, but the following points are needed:
  • 1. The main problem is explained here: https://github.com/PaddlePaddle/PaddleOCR/issues/2355
  • 2. Add CUDA, cudnn and tensorRT libraries.
  • 3. Set in config.txt use_gpu 1, use_tensorrt 0(it seems that there is a problem with opening this fast, and the official does not seem to support it, but this library is necessary)
  • 4. Set according to the memory size of your own GPU gpu_mem 1000(my GPU is 2G, which is relatively small, so set it to 1000)
  • 5. If the running result is consistent with the above, it will not be displayed.

4. Complete engineering code and tripartite library

Guess you like

Origin blog.csdn.net/qq_38589460/article/details/119742105