欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Tesseract API Basic Example项目

程序员文章站 2023-12-27 09:41:39
...

参考链接:
https://www.jianshu.com/p/a53c732d8da3
https://github.com/tesseract-ocr/tesseract/wiki/APIExample

tesseract项目的生成见之前的文章:手把手教你win7_64位+cmake+vs2015编译tesseract5.0.0

【集中存放tesseract库内容】

1.vs2015打开tesseract.sln。右键install,生成——仅生成install
2.将C:\Program Files (x86)下的tesseract文件夹剪切到桌面,此时里面已经有bin、include、lib、cmake文件夹
3.vs2015打开leptonica.sln。右键install,生成——仅生成install
4.将C:\Program Files (x86)下的leptonica文件夹剪切到桌面,此时里面已经有bin、include、lib、cmake文件夹
bin:
1.去原来的leptonica文件夹里搜索.dll,将两个leptonica的.dll复制粘贴进来
2.去原来的tesseract文件夹里搜索.dll,将两个tesseract的.dll复制粘贴进来
3.去原来的tesseract文件夹里搜索.pdb,将两个tesseract的.pdb复制粘贴进来
4.去原来的png文件夹里搜索.dll,将两个png的.dll复制粘贴进来
5.去原来的zlib文件夹里搜索.dll,将两个zlib的.dll复制粘贴进来
6.去原来的tiff文件夹里搜索.dll,将四个tiff的.dll复制粘贴进来
注:原来就有的tesseract.exe不要删掉
注:4、5、6按照教程本来没有,但是最后运行的时候提醒我需要,所以复制粘贴了进来
include:
1.把桌面上leptonica文件夹里include文件夹里的leptonica文件夹复制粘贴到桌面上tesseract下的include文件夹里
lib:
1.去原来的leptonica文件夹里搜索.lib,将两个leptonica的.lib复制粘贴进来
2.去原来的tesseract文件夹里搜索.lib,将两个tesseract的.lib复制粘贴进来
3.去原来的tiff文件夹里搜索.lib,将两个tiff的.lib复制粘贴进来
tessdata:
1.在桌面上的tesseract里新建tessdata文件夹
2.在tessdata文件夹里放置中英文语言包
cmake:
1.把桌面上leptonica下的cmake文件里的复制到桌面上tesseract下的cmake文件夹里

【cmake相关文件编写】

1.在桌面上的tesseract文件夹里新建TesseractConfig.cmake,内容如下:

# ===================================================================================
#  The Tesseract CMake configuration file
#
#  Usage from an external project:
#    In your CMakeLists.txt, add these lines:
#
#    FIND_PACKAGE(Tesseract REQUIRED)
#    TARGET_LINK_LIBRARIES(MY_TARGET_NAME ${Tesseract_LIBS})
#
#    This file will define the following variables:
#      - Tesseract_LIBS                     : The list of libraries to link against.
#      - Tesseract_LIB_DIR                  : The directory(es) where lib files are. Calling 
#                                             LINK_DIRECTORIES with this path is NOT needed.
#      - Tesseract_INCLUDE_DIRS             : The Tesseract include directories.
#      - Tesseract_VERSION                  : The version of this Tesseract build. Example: "2.4.0"
#      - Tesseract_VERSION_MAJOR            : Major version part of Tesseract_VERSION. Example: "2"
#      - Tesseract_VERSION_MINOR            : Minor version part of Tesseract_VERSION. Example: "4"
#      - Tesseract_VERSION_PATCH            : Patch version part of Tesseract_VERSION. Example: "0"
#
#    Advanced variables:
#      - Tesseract_CONFIG_PATH
#
# ===================================================================================

set(Tesseract_VERSION_MAJOR 5)
set(Tesseract_VERSION_MINOR 0)
set(Tesseract_VERSION_PATCH 0)
set(Tesseract_VERSION ${Tesseract_VERSION_MAJOR}.${Tesseract_VERSION_MINOR}.${Tesseract_VERSION_PATCH})

get_filename_component(Tesseract_CONFIG_PATH "${CMAKE_CURRENT_LIST_FILE}" PATH CACHE)
set(Tesseract_LIB_DIR "${Tesseract_CONFIG_PATH}/lib")
set(Tesseract_INCLUDE_DIRS "${Tesseract_CONFIG_PATH}/include")

set(Tesseract_LIBS_DBG "leptonica-1.76.0d.lib" "tesseract50d.lib")
set(Tesseract_LIBS_OPT "leptonica-1.76.0.lib" "tesseract50.lib")
foreach(__tesslib ${Tesseract_LIBS_DBG})
  list(APPEND Tesseract_LIBS debug "${Tesseract_LIB_DIR}/${__tesslib}")
endforeach()
foreach(__tesslib ${Tesseract_LIBS_OPT})
  list(APPEND Tesseract_LIBS optimized "${Tesseract_LIB_DIR}/${__tesslib}")
endforeach()

set(Tesseract_FOUND TRUE CACHE BOOL "" FORCE)

2.桌面上新建文件夹samples,里面新建文件夹Basic-example和txt文件CMakeLists.txt
3.打开CMakeLists.txt,设置内容为

cmake_minimum_required(VERSION 3.0)
project(tesseract-api-examples)
add_subdirectory(Basic-example)

4.进入文件夹Basic-example,新建文件Basic-example.cpp和CMakeLists.txt
5.打开Basic-example.cpp,设置内容为

#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>

int main()
{
    char *outText;
    
    tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
    // Initialize tesseract-ocr with English, without specifying tessdata path
    if (api->Init(NULL, "eng")){
        fprintf(stderr, "Could not initialize tesseract.\n");
        exit(1);
    }
    
    // Open input image with leptonica library
    Pix *image = pixRead("C:\\Users\\Administrator.WIN-R4NP32A0422\\Desktop\\pics\\phototest.tif");
    api->SetImage(image);
    // Get OCR result
    outText = api->GetUTF8Text();
    printf("OCR output:\n%s", outText);
    
    // Destroy used object and release memory
    api->End();
    delete [] outText;
    pixDestroy(&image);
    
    return 0;
}

6.打开CMakeLists.txt,设置内容为

set(the_target "Basic-example")
find_package(Tesseract REQUIRED)
aux_source_directory(. SRC_LIST)
include_directories(${Tesseract_INCLUDE_DIRS})
add_executable(${the_target} ${SRC_LIST})
target_link_libraries(${the_target} ${Tesseract_LIBS})

【构建】

1.在samples文件夹下新建build文件夹
2.打开D:\studytesseractGit\0228_tesseract5.0\cmake-3.16.2-win64-x64\bin\cmake-gui.exe
设置:
源路径:C:/Users/Administrator.WIN-R4NP32A0422/Desktop/samples
目标路径:C:/Users/Administrator.WIN-R4NP32A0422/Desktop/samples/build
3.configure,选择vs2015,finish,等待
4.找到Tesseract_DIR那一行和Leptonica_DIR那一行,将桌面上tesseract的路径C:\Users\Administrator.WIN-R4NP32A0422\Desktop\tesseract放进去
5.configure,generate
6.打开C:\Users\Administrator.WIN-R4NP32A0422\Desktop\samples\build\tesseract-api-examples.sln
7.将Basic-example设定为启动项
8.生成——生成解决方案
9.将桌面上的tesseract的bin文件夹的路径C:\Users\Administrator.WIN-R4NP32A0422\Desktop\tesseract\bin放到环境变量path里面
10.新建环境变量TESSDATA_PREFIX,将tessdata的路径C:\Users\Administrator.WIN-R4NP32A0422\Desktop\tesseract\tessdata加进去
11.在Basic-example.exe处打开命令行,运行Basic-example
显示如下图:
Tesseract API Basic Example项目

相关标签: tesseract

上一篇:

下一篇: