podpora slovenčiny v Open Source programoch

building tesserocr python package on windows 64bit   

posledná zmena: 20. March 2021

back to tesseract-ocr-en


Tesseract 4.1.1 Windows installation (64bit) in command line

Initialisation of project structure

Destination for dependencies
mkdir F:\win64
set INSTALL_DIR=F:\win64
Build tree:
mkdir F:\Project & cd Project
Initialize VS environment:
call "c:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat" x64

zlib build and installation

"c:\Program Files\Git\usr\bin\unzip.exe"
cd zlib-1.2.11
mkdir build.msvs && cd build.msvs
cmake --build . --config Release --target install
cd ..\..

libpng build and installation

"c:\Program Files\Git\usr\bin\unzip.exe"
cd lpng1637
mkdir build.msvs && cd build.msvs
cmake --build . --config Release --target install
cd ..\..

leptonica build and installation

Note: tesseract 4.1.1 cmake build needs release and debug version of leptonica
git clone --depth 1
cd leptonica
mkdir build.msvs && cd build.msvs
cmake --build . --config Debug --target install
cmake --build . --config Release --target install
cd ..\..

tesseract build and installation

git clone -b 4.1.1 --depth 1
cd tesseract
    -DLeptonica_DIR=%INSTALL_DIR%\lib\cmake  ^
cmake --build . --config Release --target install
cd ..\..

Post installation

cd F:\Project
git clone --depth 1 tessdata
curl -L ^
    --output F:\Project\tessdata\eng.traineddata
curl -L ^
    --output F:\Project\tessdata\osd.traineddata
SET TESSDATA_PREFIX=F:\Project\tessdata


%INSTALL_DIR%\bin\tesseract -v
tesseract 4.1.1
 leptonica-1.81.0 (Mar 17 2021, 20:26:26) [MSC v.1928 LIB Release x64]
  libpng 1.6.37 : zlib 1.2.11
 Found AVX2
 Found AVX
 Found FMA
 Found SSE

tesserocr build

git clone
cd tesserocr
git checkout window_build

python build
python bdist_wheel
pip uninstall tesserocr
pip install dist\tesserocr-2.5.2b0-cp38-cp38-win_amd64.whl

Post installation

Note: adjust to you Python instalation
copy F:\win64\bin\*.dll "C:\Program Files\Python38\Lib\site-packages\"


cd F:\Project\tesserocr
>>> import tesserocr
>>> tesserocr.PyTessBaseAPI.Version()
>>> tesserocr.get_languages()
('F:\\Project\\tessdata/', ['eng', 'osd'])
>>> from PIL import Image
>>> image ='F:\Project\tesserocr\tests\eurotext.png')
>>> with tesserocr.PyTessBaseAPI() as api:
...     api.SetImage(image)
...     print(api.GetUTF8Text())
The (quick) [brown] {fox} jumps!
Over the $43,456.78 <lazy> #90 dog
& duck/goose, as 12.5% of E-mail
from is spam.
Der ,schnelle” braune Fuchs springt
iiber den faulen Hund. Le renard brun
«rapide» saute par-dessus le chien
paresseux. La volpe marrone rapida
salta sopra il cane pigro. El zorro
marron ripido salta sobre el perro
perezoso. A raposa marrom ripida
salta sobre o cdo preguigoso.

Building minimalistic tesseract   

posledná zmena: 7. January 2021

back to tesseract-ocr-en

If you plan to use OCR (no training or OCR debugging) in your project you need only minimal tesseract build (and leptonica): for opening files you can use other available libraries e.g. opencv, python PIL, QT (or other framework) native functions.

With minimal tesseract distribution you can avoid redundant libraries distributions, easier upgrade and distribution.

Here is example for windows build.



NOTE: mkdir is tool from git ( located e.g. c:\Program Files\Git\usr\bin\mkdir.exe)

You can adjust installation path to your needs.
mkdir -p F:\win64_msvc_min\share\tessdata_best\tessdata
set INSTALL_DIR=F:\win64_msvc_min
set TESSDATA_PREFIX=F:\win64_msvc_min\share\tessdata_best\tessdata
"c:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat" x64


 mkdir mininalistic && cd mininalistic


git clone --depth 1
cd leptonica
mkdir build64.msvc && cd build64.msvc
Successful configuration will looks like this:
-- General configuration for Leptonica 1.81.0
-- --------------------------------------------------------
-- Build type: Release
-- Compiler: MSVC
-- C compiler options:  /DWIN32 /D_WINDOWS /W3
-- Linker options: /machine:x64
-- Install directory: F:/win64_msvc_min
-- Build with sw [SW_BUILD]: OFF
-- Build utility programs [BUILD_PROG]: OFF
-- Used PNG library:
-- Used JP2K library:
-- Used GIF library:
-- Used WEBP library:
-- --------------------------------------------------------
As you can see no external dependency was find (which is desired) Now we can build and install leptonica with command:
cmake --build . --config Release --target install


cd ..\..\ 
git clone -b 5.0.0-alpha-20201224 --depth 1
cd tesseract
mkdir build64.msvc && cd build64.msvc
Successful configuration will looks like this:
-- General configuration for Tesseract 5.0.0-alpha
-- --------------------------------------------------------
-- Build type: Release
-- Compiler: MSVC
-- Used standard: C++17
-- CXX compiler options: /DWIN32 /D_WINDOWS /W3 /GR /EHsc /utf-8 /MP /O2 /Ob2 /DNDEBUG /wd4244 /wd4305 /wd4267
-- Linker options: /machine:x64
-- Install directory: F:/win64_msvc_min
-- Architecture flags: /arch:AVX2
-- Vector unit list: sse2;sse3;ssse3;sse4.1;sse4.2;avx;fma;bmi2;avx2
-- HAVE_SSE4_1: ON
-- Link-time optimization: TRUE
-- --------------------------------------------------------
-- Build with sw [SW_BUILD]: OFF
-- Build with openmp support [OPENMP_BUILD]: OFF
-- Disable disable graphics (ScrollView) [GRAPHICS_DISABLED]: ON
-- Disable the legacy OCR engine [DISABLED_LEGACY_ENGINE]: OFF
-- Build training tools [BUILD_TRAINING_TOOLS]: OFF
-- Build tests [BUILD_TESTS]: OFF
-- Use system ICU Library [USE_SYSTEM_ICU]: OFF
-- --------------------------------------------------------
Now we can build and install tesseract with command:
cmake --build . --config Release --target install

For unknown reason msvc build fails (error MSB3073: The command “setlocal”…) for first time build. But if you run above command once again, it will be successful:

 -- Install configuration: "Release"
  -- Installing: F:/win64_msvc_min/lib/pkgconfig/tesseract.pc
  -- Installing: F:/win64_msvc_min/bin/tesseract.exe
  -- Installing: F:/win64_msvc_min/lib/tesseract50.lib
  -- Installing: F:/win64_msvc_min/bin/tesseract50.dll
  -- Installing: F:/win64_msvc_min/lib/cmake/tesseract/TesseractTargets.cmake
  -- Installing: F:/win64_msvc_min/lib/cmake/tesseract/TesseractTargets-release.cmake
  -- Up-to-date: F:/win64_msvc_min/lib/cmake
  -- Installing: F:/win64_msvc_min/lib/cmake/TesseractConfig.cmake
  -- Installing: F:/win64_msvc_min/lib/cmake/TesseractConfigVersion.cmake
  -- Installing: F:/win64_msvc_min/include/tesseract/apitypes.h
  -- Installing: F:/win64_msvc_min/include/tesseract/baseapi.h
  -- Installing: F:/win64_msvc_min/include/tesseract/capi.h
  -- Installing: F:/win64_msvc_min/include/tesseract/renderer.h
  -- Installing: F:/win64_msvc_min/include/tesseract/version.h
  -- Installing: F:/win64_msvc_min/include/tesseract/thresholder.h
  -- Installing: F:/win64_msvc_min/include/tesseract/ltrresultiterator.h
  -- Installing: F:/win64_msvc_min/include/tesseract/pageiterator.h
  -- Installing: F:/win64_msvc_min/include/tesseract/resultiterator.h
  -- Installing: F:/win64_msvc_min/include/tesseract/osdetect.h
  -- Installing: F:/win64_msvc_min/include/tesseract/publictypes.h
  -- Installing: F:/win64_msvc_min/include/tesseract/genericvector.h
  -- Installing: F:/win64_msvc_min/include/tesseract/helpers.h
  -- Installing: F:/win64_msvc_min/include/tesseract/ocrclass.h
  -- Installing: F:/win64_msvc_min/include/tesseract/platform.h
  -- Installing: F:/win64_msvc_min/include/tesseract/serialis.h
  -- Installing: F:/win64_msvc_min/include/tesseract/strngs.h
  -- Installing: F:/win64_msvc_min/include/tesseract/unichar.h

Now you have build minimalistic tesseract build for using in your project like simple tesseract wrapper in Python


If you plan to use tesseract pdf output, you will need leptonica with libpng, libz, libjpeg, libtiff support.


Test tesseract verion:
tesseract --version
tesseract 5.0.0-alpha
 leptonica-1.81.0 (Nov  1 2020, 19:13:26) [MSC v.1927 LIB Release x64]
 Found AVX2
 Found AVX
 Found FMA
 Found SSE 

As you can see there no support for any (external) image libraries. Anyway tesseract able to use some simple image formats like pnm, ppm, bmp, spix.

So lets try to use some simple image:
tesseract line.ppm - --dpi 300
It will produce:
Error in pixReadMemTiff: function not present
Error in pixReadMem: tiff: no pix returned
Error in pixaGenerateFontFromString: pix not made
Error in bmfCreate: font pixa not made
N27 26 10 04 03 01

Error messages are produced by leptonica, because tesseract is trying to use some function that
requires (unavailable external libraries). This messages can be suppress by leptonica function setMsgSeverity OCR result is in last line.

Simple python example usage

Code could be downloaded from PasteBin

#!/usr/bin/env python3

import ctypes
import locale
import os
import platform
from ctypes.util import find_library

import cffi
from PIL import Image, ImageDraw, ImageFont

ffi = cffi.FFI
ffi.cdef( “”“
typedef signed char l_int8;
typedef unsigned char l_uint8;
typedef short l_int16;
typedef unsigned short l_uint16;
typedef int l_int32;
typedef unsigned int l_uint32;
typedef float l_float32;
typedef double l_float64;
typedef long long l_int64;
typedef unsigned long long l_uint64;
typedef int l_ok; /*!< return type 0 if OK, 1 on error */

struct Pix;
typedef struct Pix PIX;
typedef enum lept_img_format { IFF_UNKNOWN = 0, IFF_BMP = 1, IFF_JFIF_JPEG = 2, IFF_PNG = 3, IFF_TIFF = 4, IFF_TIFF_PACKBITS = 5, IFF_TIFF_RLE = 6, IFF_TIFF_G3 = 7, IFF_TIFF_G4 = 8, IFF_TIFF_LZW = 9, IFF_TIFF_ZIP = 10, IFF_PNM = 11, IFF_PS = 12, IFF_GIF = 13, IFF_JP2 = 14, IFF_WEBP = 15, IFF_LPDF = 16, IFF_TIFF_JPEG = 17, IFF_DEFAULT = 18, IFF_SPIX = 19

typedef enum newsev { L_SEVERITY_EXTERNAL = 0, /* Get the severity from the environment */ L_SEVERITY_ALL = 1, /* Lowest severity: print all messages */ L_SEVERITY_DEBUG = 2, /* Print debugging and higher messages */ L_SEVERITY_INFO = 3, /* Print informational and higher messages */ L_SEVERITY_WARNING = 4, /* Print warning and higher messages */ L_SEVERITY_ERROR = 5, /* Print error and higher messages */ L_SEVERITY_NONE = 6 /* Highest severity: print no messages */

char * getLeptonicaVersion ( );
PIX * pixRead ( const char *filename );
PIX * pixCreate ( int width, int height, int depth );
PIX * pixEndianByteSwapNew(PIX *pixs);
l_int32 pixSetData ( PIX *pix, l_uint32 *data );
l_ok pixSetPixel ( PIX *pix, l_int32 x, l_int32 y, l_uint32 val );
l_ok pixWrite ( const char *fname, PIX *pix, l_int32 format );
l_int32 pixFindSkew ( PIX *pixs, l_float32 *pangle, l_float32 *pconf );
PIX * pixDeskew ( PIX *pixs, l_int32 redsearch );
void pixDestroy ( PIX **ppix );
l_ok pixGetResolution ( const PIX *pix, l_int32 *pxres, l_int32 *pyres );
l_ok pixSetResolution ( PIX *pix, l_int32 xres, l_int32 yres );
l_int32 pixGetWidth ( const PIX *pix );
l_int32 setMsgSeverity ( l_int32 newsev );

typedef struct TessBaseAPI TessBaseAPI;
typedef struct ETEXT_DESC ETEXT_DESC;
typedef struct TessPageIterator TessPageIterator;
typedef struct TessResultIterator TessResultIterator;
typedef int BOOL;

typedef enum TessOcrEngineMode { OEM_TESSERACT_ONLY = 0, OEM_LSTM_ONLY = 1, OEM_TESSERACT_LSTM_COMBINED = 2, OEM_DEFAULT = 3} TessOcrEngineMode;


typedef enum TessPageIteratorLevel { RIL_BLOCK = 0, RIL_PARA = 1, RIL_TEXTLINE = 2, RIL_WORD = 3, RIL_SYMBOL = 4} TessPageIteratorLevel;

TessPageIterator* TessBaseAPIAnalyseLayout(TessBaseAPI* handle);
TessPageIterator* TessResultIteratorGetPageIterator(TessResultIterator* handle);

BOOL TessPageIteratorNext(TessPageIterator* handle, TessPageIteratorLevel level);
BOOL TessPageIteratorBoundingBox(const TessPageIterator* handle, TessPageIteratorLevel level, int* left, int* top, int* right, int* bottom);

const char* TessVersion();

TessBaseAPI* TessBaseAPICreate();
int TessBaseAPIInit3(TessBaseAPI* handle, const char* datapath, const char* language);
int TessBaseAPIInit2(TessBaseAPI* handle, const char* datapath, const char* language, TessOcrEngineMode oem);
void TessBaseAPISetPageSegMode(TessBaseAPI* handle, TessPageSegMode mode);
void TessBaseAPISetImage(TessBaseAPI* handle, const unsigned char* imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line);
void TessBaseAPISetImage2(TessBaseAPI* handle, struct Pix* pix);

BOOL TessBaseAPISetVariable(TessBaseAPI* handle, const char* name, const char* value);
BOOL TessBaseAPIDetectOrientationScript(TessBaseAPI* handle, char** best_script_name, int* best_orientation_deg, float* script_confidence, float* orientation_confidence);
int TessBaseAPIRecognize(TessBaseAPI* handle, ETEXT_DESC* monitor);
TessResultIterator* TessBaseAPIGetIterator(TessBaseAPI* handle);
BOOL TessResultIteratorNext(TessResultIterator* handle, TessPageIteratorLevel level);
char* TessResultIteratorGetUTF8Text(const TessResultIterator* handle, TessPageIteratorLevel level);
float TessResultIteratorConfidence(const TessResultIterator* handle, TessPageIteratorLevel level);
char* TessBaseAPIGetUTF8Text(TessBaseAPI* handle);
const char* TessResultIteratorWordFontAttributes(const TessResultIterator* handle, BOOL* is_bold, BOOL* is_italic, BOOL* is_underlined, BOOL* is_monospace, BOOL* is_serif, BOOL* is_smallcaps, int* pointsize, int* font_id);
void TessBaseAPIEnd(TessBaseAPI* handle);
void TessBaseAPIDelete(TessBaseAPI* handle);

def get_abs_path_of_library(library): “”“Get absolute path of library.“
abs_path = None
lib_name = find_library(library)
if os.path.exists(lib_name):
abs_path = os.path.abspath(lib_name)
return abs_path
libdl = ctypes.CDLL
if not libdl:
return abs_path # None
dlinfo = libdl.dlinfos
except AttributeError as err:
# Workaroung for linux
abs_path = str(err).split(
”)0 return abs_path

def pil2PIX32(im, leptonica): “”“Convert PIL to leptonica PIX.”“” # At the moment we handle everything as RGBA image if im.mode != “RGBA”: im = im.convert(“RGBA”) depth = 32 width, height = im.size data = im.tobytes(“raw”, “RGBA”) pixs = leptonica.pixCreate(width, height, depth) leptonica.pixSetData(pixs, ffi.from_buffer(“l_uint32[]”, data))

try: resolutionX =[“resolution”]0 resolutionY =[“resolution”]1 leptonica.pixSetResolution(pixs, resolutionX, resolutionY) except KeyError: pass try: resolutionX =[“dpi”]0 resolutionY =[“dpi”]1 leptonica.pixSetResolution(pixs, resolutionX, resolutionY) except KeyError: pass return leptonica.pixEndianByteSwapNew(pixs)

def img_lepto_to_pil(pix): “”“Convert leptonica pix to PIL Source: “”“ cdata_ptr =“l_uint8**”) size_ptr =“size_t*”) leptonica.pixWriteMem(cdata_ptr, size_ptr, pix, IFF_TIFF) cdata = cdata_ptr0 size = size_ptr0

tiff_bytes = bytes(ffi.buffer(cdata, size)) with BytesIO(tiff_bytes) as bytesio: pilimage = return pilimag

def main(): ‘’‘Main loop.’‘’ # Settings tess_libname = r“F:/win64_msvc_min/bin/tesseract50.dll” lept_libname = r“F:/win64_msvc_min/bin/leptonica-1.81.0.dll” filename = “line.ppm” lang = “eng”

tessdata = os.environ.get(“TESSDATA_PREFIX”) if not tessdata: # Use project tessdata tessdata = os.path.join(os.getcwd(), “tessdata”) os.environ[“TESSDATA_PREFIX”] = tessdata # Load libraries in ABI mode if os.path.exists(tess_libname): tesseract = ffi.dlopen(tess_libname) else: print(f”’{tess_libname}’ does not exists!”) tesseract_version = ffi.string(tesseract.TessVersion()) print(“Tesseract-ocr version”, tesseract_version.decode(“utf-8”)) if os.path.exists(lept_libname): leptonica = ffi.dlopen(lept_libname) else: print(f”’{lept_libname}’ does not exists!”) leptonica_version = ffi.string(leptonica.getLeptonicaVersion()) print(leptonica_version.decode(“utf-8”)) api = None # Read image to pix im = pix = pil2PIX32(im, leptonica) # Turn off leptonica warnings leptonica.setMsgSeverity(leptonica.L_SEVERITY_EXTERNAL) # Create tesseract API if api: tesseract.TessBaseAPIEnd(api) tesseract.TessBaseAPIDelete(api) api = tesseract.TessBaseAPICreate() oem = tesseract.OEM_DEFAULT tesseract.TessBaseAPIInit2(api, tessdata.encode(), lang.encode(), oem) tesseract.TessBaseAPISetPageSegMode(api, tesseract.PSM_AUTO) tesseract.TessBaseAPISetImage2(api, pix) # recognize is needed to get result iterator tesseract.TessBaseAPIRecognize(api, ffi.NULL) utf8_text = ffi.string(tesseract.TessBaseAPIGetUTF8Text(api)).decode(“utf-8”) print(utf8_text) # Delete api and pix if api: tesseract.TessBaseAPIEnd(api) tesseract.TessBaseAPIDelete(api) result =“PIX**”) result0 = pix leptonica.pixDestroy(result) del pix del result api = None

if name == “main”: main()

2020-44. týždeň :: Novinky   

posledná zmena: 1. November 2020

Vydaná nová verzia Malého Anglicko-Slovenského Slovníka pre GoldenDict [238 kB] (stiahnuté: 475x) — obsahuje 14979 preložených slov, fráz, tvarov vo formáte dsl (ABBYY Lingvo .dsl dictionary).

PS: Hľadáme php programátora, ktorý by fixol chyby v aplikácii Glossword .

How to create simple tesseract wrapper in Python   

posledná zmena: 25. December 2020

Do you need to access some tesseract feature not available from command line tool?

continue reading »

Building tesseract and leptonica with CMake and Clang on Windows   

posledná zmena: 5. January 2020

Is it possible to build tesseract on windows with clang with most(all) of its dependencies?

continue reading »

Leptonica With Static Linked Dependencies (on Windows)   

posledná zmena: 1. February 2019

Have you ever wonder how to build leptonica without need to distribute its dependencies like shared libs?

continue reading »

Tesseract Project Dependencies   

posledná zmena: 1. January 2019

This article is description of my findings of tesseract dependencies on Windows

continue reading »

less is better   

posledná zmena: 1. January 2019

Nowadays file-size is not a issue usually. But there are situations when size of library matters…

continue reading »

Update of language files for tesseract-ocr 3.04   

posledná zmena: 30. June 2015

New version of tesseract-ocr language file where uploaded!

continue reading »


posledná zmena: 29. June 2015

Did you ever want to see tesseract boxes? Or effect of different page segmentation modes? Or result iterator mode?

continue reading »

© projekt sk-spell

RSS [opensource] [w3c] [firefox] [textpattern]