Qt uses VCPKG, CMake, OpenCV and Tesseract to implement Chinese and English OCR

Article directory

  • 1. Development platform
  • 2. Download files
    • 2.1 Download and install the OpenCV library
    • 2.2 Download and install the Tesseract-OCR library
    • 2.3 Download the trained language package
  • 3. CMakeLists.txt content
  • 4. Main.cpp
    • 4.1 Mixed Chinese and English OCR
  • 5. Set up CMake + vcpkg in Qt Creator
    • 5.1 Modify in the initialization configuration file
    • 5.2 Modify in build configuration
  • Note: CMake uses the library installed by vcpkg in the Qt project.
  • 6. Screenshot of the effect
  • 7. Summary

Qt uses VCPKG, CMake, OpenCV and Tesseract to implement Chinese and English OCR

Today I watched a tutorial on OpenCV, in pdf format. Because of some obsessive-compulsive habits, I like to add some bookmarks, but I am not willing to do it manually. Naturally, I thought of using OCR to achieve it.

If you want to code and implement OCR yourself, the simpler solution is Tesseract. You can also use Tesseract alone, but it is best to use OpenCV for image processing. There are a lot of methods for filtering and transformation, but it is annoying to constantly adjust parameters. Different pictures require different methods, which is not refreshing. . For the purpose of summary, record the process.

This project is just a demo. It is relatively simple. It is a parallel import on the Internet. It only uses Qt Creator, CMake and vcpkg. It has some reference value.

1. Development platform

  • os: win10 x64
  • Qt:6.6
  • compiler: msvc2022
  • Project management: cmake
  • Package management: vcpkg
  • Development library version:
    • OpenCV: 4.8, this is very friendly, there are compiled libraries, you don’t need to get it yourself
    • Tesseract-ocr: 5.3 huge pitfall, why does the binary file not contain lib?

2. Download files

  • Let me first talk about the pitfalls I have encountered. I hope someone with relevant experience can give me some advice.

    • The Pitfalls of Tesseract

      There is a binary package in Tesseract https://github.com/tesseract-ocr/tesseract#installing-tesseract. After downloading it excitedly, I saw that it was not a library file, but an executable file. How to encode this using process dialogue? to fulfill? What is the use of this package? It is not helpful for development.

      tesseract-ocr-w64-setup-5.3.3.20231005.exe (64 bit) After installation, there is no lib library, only the executable file

    • sw pit

      Since there is no ready-made library for Tesseract, you need to compile it yourself. I came into contact with sw on Tesseract. This is also a package management thing. The key is that it will automatically download and automatically solve the problem of package dependencies. It seems very convenient and exciting. Download and use it, and find that the network speed is just right, and cmake can also be used. Check out the documentation to download, add environment variables, and set dependent packages in cmake strong>, and then wait for success.

      find_package(SW REQUIRED)
      sw_add_package(
      org.sw.demo.glennrp.png
      )
      sw_execute()
      
      
      add_executable(mytarget ${MY_SOURCES})
      target_link_libraries(mytarget
      org.sw.demo.glennrp.png
      )
      

      It looks great, but the sw_execute() step takes a long time. As long as CMakeFiles.txt changes, this thing will take a long time.

      Is there anyone who knows how to do this? Can you give me some pointers? This thing looks good and deserves applause and anticipation. Hope it gets better.

      There is also a swgui client, which I struggled with for a while, but in the end I didn’t master it and couldn’t figure it out. So he abandoned it.

2.1 Download and install the OpenCV library

This step is very simple. There are many versions on the official website, so I found the latest one.

Releases-OpenCV

Download, install and add environment variables. You can omit the step of copying the dll to the generated directory.

2.2 Download and install the Tesseract-OCR library

After going through the pitfalls of SW, I still returned to VCPKG. Use IDM and Thunder to manually accelerate the network. The process is tedious, but not difficult. In addition, using PowerShell will be more convenient.

vcpkg install tesseract --triplet=x64-windows

Specific steps:

  • Execute vcpkg install tesseract --triplet=x64-windows
  • ctrl + c interrupt
  • Copy the download link, manually IDM or Thunder
  • Rename
  • Continue to execute vcpkg install tesseract --triplet=x64-windows

As you journey slowly, you should have a novel to accompany you. Good luck with your internet speed.

In addition, do not compile the Tesseract library manually. This library has many dependencies, including leptonica, archive.dll, bz2.dll, clang_rt.asan_dynamic-x86_64.dll, gif.dll, jpeg62.dll, libcrypto-3-x64.dll, libcurl.dll, liblzma.dll, libpng16.dll, libsharpyuv.dll, libwebp.dll, libwebpmux.dll, lz4.dll, openjp2.dll, tiff.dll, zlib1.dll, zstd.dll, so annoying.

2.3 Download the trained language pack

  • tessdata_best: https://github.com/tesseract-ocr

  • eng.traineddata and chi_sim.traineddata

  • Click to download raw

3. CMakeLists.txt content

cmake_minimum_required(VERSION 3.24)

project(36_Opencv4_Tesseract_OCR LANGUAGES CXX)

#set(CMAKE_CXX_STANDARD 17)
#set(CMAKE_CXX_STANDARD_REQUIRED ON)

##################### Setting up the QT library #####################
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

#Add include and source paths for custom code
#include_directories (D:/Project/qt_common_tools/global_define)
#aux_source_directory (D:/Project/qt_common_tools/global_define COMMON_TOOLS_LIST)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core )


##################### vcpkg library #####################
#Looking for TESSERACT library
FIND_PACKAGE(tesseract REQUIRED)
#LOOK FOR LEPTONICA LIBRARY
FIND_PACKAGE(leptonica REQUIRED)


##################### opencv library #####################
set(OpenCV_DIR C:/OpenCV/opencv/build)

#Find OpenCV library
FIND_PACKAGE(OpenCV REQUIRED)

##Print debugging information
#MESSAGE(STATUS "Project: ${PROJECT_NAME}")
#MESSAGE(STATUS "OpenCV library status:")
#MESSAGE(STATUS " version: ${OpenCV_VERSION}")
#MESSAGE(STATUS " libraries: ${OpenCV_LIBS}")
#MESSAGE(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")


##################### Modify entry point #####################
# Set the program as a windows program. Modify the entry point and do not display the console.
#set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /ENTRY:mainCRTStartup")

#################### Memory leak check #####################
#SET(CMAKE_CXX_FLAGS "-fsanitize=address")

#Get the code, in the project, put all the code in the src folder
AUX_SOURCE_DIRECTORY(.DIR_SRCS)
#MESSAGE(STATUS "Src file: ${DIR_SRCS}")



#################### Set source code encoding ####################
#add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/source-charset:utf-8>")
#add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/execution-charset:GBK>")


#{<!-- -->{<!-- -->{<!-- -->{<!-- -->{<!-- -->{<!-- -->{ <!-- -->{<!-- -->{<!-- -->{<!-- -->{<!-- -->{<!-- -->{<! -- -->{<!-- -->{<!-- -->{<!-- -->{<!-- -->{<!-- -->{ Compile executable program }}}}}}}}}}}}}}}}}##
add_executable( ${PROJECT_NAME}
#WIN32
    ${DIR_SRCS}
    ${COMMON_TOOLS_LIST}
)



#################### Add link library #####################
set(VCPKG_INCLUDE_DIR C:/vcpkg/installed/x64-windows/include)
set(VCPKG_LIB_DIR C:/vcpkg/installed/x64-windows/lib)

# Header file path
TARGET_INCLUDE_DIRECTORIES(${PROJECT_NAME} PUBLIC ${VCPKG_INCLUDE_DIR})

#lib file path
TARGET_LINK_DIRECTORIES(${PROJECT_NAME}
        PUBLIC
        ${VCPKG_LIB_DIR})

# lib file
TARGET_LINK_LIBRARIES(${PROJECT_NAME} PUBLIC
     ${OpenCV_LIBS}
     tesseract53
     leptonica
     Qt${QT_VERSION_MAJOR}::Core
)

#Set to open VCPKG by default
#set_target_properties(${PROJECT_NAME} PROPERTIES VS_GLOBAL_VcpkgEnabled true)

#################### Set app ico #####################
##set(app_icon_resource_windows ${CMAKE_CURRENT_SOURCE_DIR}/logo.rc)
##message(STATUS "${app_icon_resource_windows}")

4. Main.cpp

//#include "chinese.h"
#include "qdebug.h"

#include <iostream>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <tesseract/baseapi.h> // tesseract main header

#include <QBuffer>
using namespace cv;

int main()
{<!-- -->
    std::string image_name = "txt.jpg";
    Mat imageMat;
    imageMat = imread(image_name);
    // imshow(ANSI("original image"), imageMat);
    imshow("origin", imageMat);
    if (imageMat.empty()) {<!-- -->
        printf("No image data \\
");
        return -1;
    }
    // Rect ccomp;
    // floodFill(imageMat,Point(3,3),Scalar(255,255,255), & amp;ccomp,Scalar(10,10,10),Scalar(20,20,20));
    cv::cvtColor(imageMat, imageMat, cv::COLOR_BGR2GRAY);

    char *outText;
    tesseract::TessBaseAPI tessbaseApi;

    if (tessbaseApi.Init("./", "chi_sim + eng")) {<!-- --> // chi_sim + eng put the downloaded language pack and executable file together
        std::cout << stderr << std::endl;
        exit(1);
    }
    // tesseract sets the picture
    tessbaseApi.SetImage((uchar *) imageMat.data, imageMat.cols, imageMat.rows, 1, imageMat.cols);

    // Get ocr results
    outText = tessbaseApi.GetUTF8Text();
    if (outText == nullptr) {<!-- -->
        std::cout << "No data" << std::endl;
    }
    QBuffer buf;
    buf.setData(outText);
    buf.open(QIODevice::ReadOnly);
    while (!buf.atEnd()) {<!-- -->
        QString line = buf.readLine();
        // line = removedSpaceInterChinese(line); // Custom function, remove the spaces between Chinese characters, you can ignore it
        if (!line.trimmed().isEmpty())
            qDebug() << line;
    }

    delete[] outText;

    waitKey();
    return 0;
}

4.1 Mixed Chinese and English OCR

tessbaseApi.Init("./", "chi_sim + eng")

Just use the + sign to connect chi_sim and eng

5. Set up CMake + vcpkg in Qt Creator

cmake import library: You can write it manually, but since you can be lazy, why do it O(∩_∩)O

But the first step has to be done manually:

5.1 Modify in initialization configuration file


If you switch the compilation mode realease -> debug, you have to manually add /(ㄒoㄒ)/~~

5.2 Modify in build configuration

I just tinkered with another one, just add -DCMAKE_TOOLCHAIN_FILE:STRING=C:/vcpkg/scripts/buildsystems/vcpkg.cmake in the tool configuration.

Recommended:?

Note: In the Qt project, CMake uses the library installed by vcpkg

# Use the library installed by vcpkg in the Qt project and change it to your own vcpkg installation directory
1. Add qt to the project
CMAKE_TOOLCHAIN_FILE C:/vcpkg/scripts/buildsystems/vcpkg.cmake

2.CMakeList.txt
##################### vcpkg library #####################
#Looking for TESSERACT library
FIND_PACKAGE(tesseract REQUIRED)

#LOOK FOR LEPTONICA LIBRARY
FIND_PACKAGE(leptonica REQUIRED)

#################### Link library #####################
set(VCPKG_INCLUDE_DIR C:/vcpkg/installed/x64-windows/include) // This step can actually be modified without using the absolute path
set(VCPKG_LIB_DIR C:/vcpkg/installed/x64-windows/lib)

# Header file path
TARGET_INCLUDE_DIRECTORIES(${PROJECT_NAME} PUBLIC ${VCPKG_INCLUDE_DIR})

#lib file path
TARGET_LINK_DIRECTORIES(${PROJECT_NAME}
        PUBLIC
        ${VCPKG_LIB_DIR})

# lib file
TARGET_LINK_LIBRARIES(${PROJECT_NAME} PUBLIC
     tesseract53
     leptonica
)

6. Effect screenshot


7. Summary

Build kit: Desktop Qt 6.6 MSVC2019 64bit can use the MSVC2022 version of the c and c++ compilers, which is also convenient. If msvc adds a 142 generation tool, you can also manually add compilation and select msvc2019 mode.

If you don’t use Qt modules [for this program, you don’t need Qt at all] and use std::cout to output, you will find that the QC application output box is all garbled, but don’t panic!

Use cmd to execute the program and switch the code page to utf-8: chcp 65001

I’ve said what I wanted to say, and you’re done!