├── .gitignore ├── phototest.tif ├── with_cmake ├── cppan.yml ├── CMakeLists.txt ├── README.md └── src │ └── main.cpp ├── README.md └── with_cppan ├── README.md └── main.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | build* 2 | bin* 3 | cppan 4 | win* 5 | *.lnk 6 | *.exe 7 | .cppan 8 | -------------------------------------------------------------------------------- /phototest.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cppan/tesseract_example/HEAD/phototest.tif -------------------------------------------------------------------------------- /with_cmake/cppan.yml: -------------------------------------------------------------------------------- 1 | dependencies: 2 | pvt.cppan.demo.google.tesseract.libtesseract: master 3 | pvt.cppan.demo.danbloomberg.leptonica: 1 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tesseract Example 2 | 3 | A very basic Tesseract-OCR example with C++ Archive Network building. 4 | 5 | You can [try](https://github.com/cppan/tesseract_example/tree/master/with_cmake) it inside CMake project (see `with_cmake` dir) or [without](https://github.com/cppan/tesseract_example/tree/master/with_cppan) explicit CMake project (see `with_cppan` dir). 6 | -------------------------------------------------------------------------------- /with_cppan/README.md: -------------------------------------------------------------------------------- 1 | # With CPPAN 2 | 3 | 4 | # Tesseract Example 5 | 6 | A very basic Tesseract-OCR example with C++ Archive Network building. 7 | 8 | ## Building 9 | 10 | Prerequisites: 11 | 12 | 1. Download git, cmake and add them to PATH 13 | 2. Download the latest CPPAN (https://cppan.org/) client from https://cppan.org/client/ 14 | 3. Add cppan to PATH too. 15 | 4. Run 16 | 17 | ``` 18 | 19 | Run `cppan --build main.cpp` to build your simple application that uses tesseract. 20 | ``` 21 | 22 | ## Testing 23 | 24 | 1. Download tesseract english data to `tessdata` dir near the `main` binary. 25 | 2. Copy test image `img/phototest.tif` 26 | 3. Run ``main phototest.tif`` 27 | -------------------------------------------------------------------------------- /with_cmake/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # 2 | # demo_project 3 | # 4 | 5 | cmake_minimum_required(VERSION 2.8.12) 6 | 7 | # Use solution folders. 8 | set_property(GLOBAL PROPERTY USE_FOLDERS ON) 9 | set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "CMake Targets") 10 | 11 | project(demo_project C CXX) 12 | 13 | # Output directory settings 14 | set(output_dir ${CMAKE_BINARY_DIR}/bin) 15 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${output_dir}) 16 | 17 | if (MSVC) 18 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP") 19 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP") 20 | else() 21 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 22 | endif() 23 | 24 | #set(CPPAN_BUILD_SHARED_LIBS ON) 25 | add_subdirectory(.cppan) 26 | 27 | add_executable(main src/main.cpp) 28 | target_link_libraries(main 29 | pvt.cppan.demo.google.tesseract.libtesseract 30 | pvt.cppan.demo.danbloomberg.leptonica 31 | ) 32 | -------------------------------------------------------------------------------- /with_cmake/README.md: -------------------------------------------------------------------------------- 1 | # Tesseract Example 2 | 3 | ## Building 4 | 5 | Before building take a look at `cppan.yml` file. Create a local copy of it in your project structure. 6 | 7 | ### Windows 8 | 9 | Prerequisites: 10 | 11 | 1. Download git, cmake and add them to PATH 12 | 2. Download the latest CPPAN (https://cppan.org/) client from https://cppan.org/client/ 13 | 3. Add cppan to PATH too. 14 | 15 | ``` 16 | git clone https://github.com/cppan/tesseract_example tesseract_example 17 | cd tesseract_example/with_cmake 18 | cppan 19 | mkdir build && cd build 20 | cmake .. 21 | cmake --build . --config Release 22 | ``` 23 | 24 | ### Linux 25 | 26 | Prerequisites: 27 | 28 | 1. Install git, cmake, the latest CPPAN (https://cppan.org/) client. 29 | 2. Run 30 | 31 | ``` 32 | git clone https://github.com/cppan/tesseract_example tesseract_example 33 | cd tesseract_example/with_cmake 34 | cppan 35 | mkdir build && cd build 36 | cmake .. 37 | make -j4 38 | ``` 39 | 40 | ## Testing 41 | 42 | 1. Download tesseract english data to `tessdata` dir near the `main` binary. 43 | 2. Copy test image `phototest.tif` to working dir. 44 | 3. Run ``main phototest.tif`` 45 | -------------------------------------------------------------------------------- /with_cmake/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include // leptonica main header for image io 5 | #include // tesseract main header 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | if (argc == 1) 10 | return 1; 11 | 12 | tesseract::TessBaseAPI tess; 13 | 14 | if (tess.Init("./tessdata", "eng")) 15 | { 16 | std::cout << "OCRTesseract: Could not initialize tesseract." << std::endl; 17 | return 1; 18 | } 19 | 20 | // setup 21 | tess.SetPageSegMode(tesseract::PageSegMode::PSM_AUTO); 22 | tess.SetVariable("save_best_choices", "T"); 23 | 24 | // read image 25 | auto pixs = pixRead(argv[1]); 26 | if (!pixs) 27 | { 28 | std::cout << "Cannot open input file: " << argv[1] << std::endl; 29 | return 1; 30 | } 31 | 32 | // recognize 33 | tess.SetImage(pixs); 34 | tess.Recognize(0); 35 | 36 | // get result and delete[] returned char* string 37 | std::cout << std::unique_ptr(tess.GetUTF8Text()).get() << std::endl; 38 | 39 | // cleanup 40 | tess.Clear(); 41 | pixDestroy(&pixs); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /with_cppan/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | dependencies: 3 | pvt.cppan.demo.google.tesseract.libtesseract: master 4 | pvt.cppan.demo.danbloomberg.leptonica: 1 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | #include // leptonica main header for image io 11 | #include // tesseract main header 12 | 13 | int main(int argc, char *argv[]) 14 | { 15 | if (argc == 1) 16 | return 1; 17 | 18 | tesseract::TessBaseAPI tess; 19 | 20 | if (tess.Init("./tessdata", "eng")) 21 | { 22 | std::cout << "OCRTesseract: Could not initialize tesseract." << std::endl; 23 | return 1; 24 | } 25 | 26 | // setup 27 | tess.SetPageSegMode(tesseract::PageSegMode::PSM_AUTO); 28 | tess.SetVariable("save_best_choices", "T"); 29 | 30 | // read image 31 | auto pixs = pixRead(argv[1]); 32 | if (!pixs) 33 | { 34 | std::cout << "Cannot open input file: " << argv[1] << std::endl; 35 | return 1; 36 | } 37 | 38 | // recognize 39 | tess.SetImage(pixs); 40 | tess.Recognize(0); 41 | 42 | // get result and delete[] returned char* string 43 | std::cout << std::unique_ptr(tess.GetUTF8Text()).get() << std::endl; 44 | 45 | // cleanup 46 | tess.Clear(); 47 | pixDestroy(&pixs); 48 | 49 | return 0; 50 | } 51 | --------------------------------------------------------------------------------