Leptonica --> tesseract --> OpenCV

来源:互联网 发布:linux中怎么复制文件 编辑:程序博客网 时间:2024/06/11 22:28

=== 02/02/2017更 ===
在测试APIExample时居然遇到问题

1). Example of iterator over the classifier choices for a single symbol
unresolved external symbol "public: float thiscall tesseract::ChoiceIterator::Confidence(void)const

Solution
必须用静态编译的 libtesseract304-static.lib

参考 error LNK2019 #36 (提供答案的matthill(Matthew Hill)是OpenALPR的作者)

The ChoiceIterator is not included in the Tesseract 3.02 DLL. Tesseract 3.03 doesn't have that problem -- but it's not supported on Windows yet.

2). 静态编译/链接通过后又出现:
unresolved external symbol closesocket/connect/gethostbyname/recv/socket 等
Solution 链接 Ws2_32.lib
Unresolved external symbol LNK2019
C++ WinSock2 Errors

=== 14/12/16 ===
Visual C++ Compiler Options /MD, /MT, /LD (Use Run-Time Library)
How to link with the correct C Run-Time (CRT) library
C Run-Time Libraries
C Run-Time Library Reference
/clr (Common Language Runtime Compilation)

There are six types of reusable libraries:

  • Static Single Threaded Library (Debug/Release)
  • Static Multithreaded Library (Debug/Release)
  • Dynamic Link Library (DLL)(Debug/Release)
    Note Each library has a debug version and a release version.

The DLL is multithread-safe and a single-thread version of the CRT library is not provided for DLLs. If the reusable library or any user of the library is using multiple threads, then the library needs to be a multithread-safe library type.

=== 13/12/16 更 ===
Tesseract ViewerDebugging
Tried 3 hours!!

  1. Both tesseract-ocr-setup-3.02.02.exe and tesseract-ocr-setup-3.05.00dev.exe has to run as administrator in command windows otherwise not working - at least 3.02 will output an error message where 3.05 is deadly silent (so this is meant to be dev version huh?).
  2. Have to rename the java file
    • piccolo2d-core-3.0.jar —> piccolo-1.2.jar
    • piccolo2d-extras-3.0.jar —> piccolox-1.2.jar
      the order doesn’t seem to matter
  3. SCROLLVIEW_PATH cannot include any space. C:\Program Files (x86)\Tesseract-OCR\java not working even if MANY MANY other environmental variables DO work with this kind of path, so it’s Tesseract’s ScrollView.jar not handling things properly
    Copy to C:\piccolo so it will work
  4. The MOST CRAZY thing is:
    • ScrollView.jar under 3.05 not working at all
    • 3.02 has to use ScrollView.jar from 3.05 to work - WTF?!!!

=== 09/12/16 再小更 ===
It’s really been a while back and forth with trying, give up, trying again…
Now picked this lead up again, due to a recent small project. Check the solution part!!

=== 16/11/16 小更 ===
注意32BIT 和 64 bit DLL 的调用
The application was unable to start correctly (0xc000007b)' error
ImageProcessingTest 编译为32 bit,调用时必须保证对应的DLL 被使用,否则出现上述错误

Tesseract-OCR## Dependency

Very useful links nicely contains lots lots MS related information.
Downloading liblept pre-built binaries and header files for Windows
(Optional) Building the liblept library
(Optional) Building zlib, libjpeg, libpng, libtiff and giflib
Source Downloads

Image Library Versions

as of 24/08/2016

Library Version From zlib 1.2.8 zlib128-dll.zip libpng 1.6.24 lpng1624.7z libjpeg 9b jpegsr9b.zip libtiff 3.8.2 tiff-3.8.2-1-[bin | lib | dep] (GnuWin32) giflib N/A

NOTE

  • libpng:
  • libtiff: a bit surprise that GunWin32 version of TIFF libraries actually works! Even though being the real old version of 3.8.2 - older than the one in Leptonica’s developer package notes

Libraries not quite work

Library From Error libpng libpng-1.2.37-[bin | lib | dep] (GnuWin32) Compiled but crash libjpeg jpeg-6b-4-[bin | lib | dep] (GnuWin32) Compiled but crash libjpeg-turbo64 libjpeg-turbo Compiled but crash

========================

tesseract

It turned out to be quite tricky to get correct tesseract and ALL its dependencies working together, On Windows.

Initially I was looking for an installer for Windows so thought should be rather simple - it’s Windows anyway!
I’ve tried:

  • tesseract-3.02.02-win32-lib-include-dirs.zip from Old Downloads from official site.
    • it should compiled with lept168.[lib | dll ] from the error message when invoke the library
    • Currently, there is no official Windows installer for newer versions.
    • **NO IDEA THOUGH which version of Visual Studio it uses to build. Shouldn’t be VS2013 since it crash!
  • tesseract-core-20160524.exe
    • However, it’s not development kit.
    • It’s a running application, with many DLLs
    • It should be build by Cygwin, not usable to VS 2013
  • tesseract-ocr-setup-3.02.02.exe
    • Installed into C:\Program Files (x86)\Tesseract-OCR
    • Again, built with lept168 but included all dependency files (libpng, tiff, jpeg, figlib, zlib) in \lib folder
    • Again, not working with VS 2013.
    • There is a vs2008 folder under Tesseract-OCR\tesseract-ocr

Solution

Following How to build Tesseract 3.03 with Visual Studio 2013 by Paul Vorbach, with his fork at tesseract4java/tesseract-vs2013

  • I actually checkout and follows the link tesseract-vs2012 by Charles Weld, as it looks like the repository has also been updated to VS 2013 support, both 64 and 32 bit. and for newer tesseract 3.04 version too.
  • tesseract-vs2012 has instructions of applying patches to git checkout.
  • vs2013+64bit_support-git.patch not working. Some email format error.
  • Just follow the instructions to use vs2013+64bit_support.patch
  • Very strange error of src\boxfunc1.c(90): error C2275: 'lint32' : illegal use of this type as an expression where lint32 is just typedef of int and been used absolutely everywhere.
  • As some guy suggested, just replace with int get it all compiled!!

Behind the curtain

What happens?

  • Basically, there are two VS solutions/projects: tesseract itself and its dependencies
  • The above tesseract4java/tesseract-vs2013 by Paul Vorbach and tesseract-vs2012 by Charles Weld are both FOR BUILDING DEPENDENCIES!
  • Tesseract is checked out from official site and patched and built against these dependencies

Top level folder

  • tesseract-build\, or could be any folder

Dependency folder

  • tesseract-build\tesseract-vs2012 or tesseract-build\tesseract-vs2013
  • Once built, the build libraries and headers will be copied to ~\release.
  • Copy the previously built dependencies to the parent directory (e.g. C:\tesseract-build)

Tesseract folder

  • Check out from official repo to C:\tesseract-build\tesseract\
  • Apply patches, then build!

Once all done, the patched solution will copy the built libraries (e.g. libtesseract304.lib, libtesseract304.dll, libtesseract304-static.lib, libtesseract304-static.dll etc.) to tesseract-build\lib\

32 bit vs. 64 bit version of tesseract and corresponding dependencies

  • 32 bit
    • OpenALPR
    • ImageProcessingTest
      • 注意不要调用64位的DLL 库(libtesseract304.dllliblept171.dll),否则出错
      • 见文首更新 (0xc000007b) error
      • OpenCV 链接的是2.4.13 版本,32位
      • 将 32位 libtesseract304.dllliblept171.dll 直接拷贝到可执行文件同目录下!
  • 64 bit
    • OpenCV 3.1.0 (by default built with CUDA 7.0, which only has 64 bit version available on my machine)
    • opencv_text module
    • TextInTheWild

Mix of libraries

  • Headers
    • For leptonica, use the header files that come with the above built version under tesseract-build\include\leptonica
    • For tesseract itself, use the pre-compiled version tesseract-3.02.02-win32-lib-include-dirs\include
  • Libraries
    • For leptonica library, use the one (liblept171.lib/dll) come with the above built version under tesseract-build\lib\Win32\
    • For tesseract, the newly built version of course tesseract-build\tesseract\vs2013\bin\Win32\DLL_Release

Now work as a charm!

OpenALPR

Not working

  • Pre-compiled Windows binaries openalpr-2.3.0-win-32bit.zip and openalpr-2.2.0-win-64bit.zip
  • Both compiled with VS 2015 (looking for MSVCP140.dll at startup)

I choose the The Harder Way from Compilation instructions (Windows) since I’ve got ALL dependency compiled except OpenALPR itself.

Working version

  • The OpenALPR src code provides FindTesseract.cmake under src\cmake_modules. Modify the contents accordingly
  • Again, use the include path from tesseract-3.02.02-win32-lib-include-dirs\include rather than tesseract-build\tesseract\api as the latter does not include all neccessary header files (e.g. publictypes.h, thresholder.h, platform.h) - how strang!
  • CMake Myth: FindTesseract.cmake automatically figured out the correct path when only providing the library names: libtesseract304 and liblept171
  • CMAKE_MODULE_PATH is for Find_package() hence FindXXX.cmake
  • CMAKE_PREFIX_PATH or OpenCV_DIR is for OpenCVConfig.cmake or opencv-config.cmake

OpenALPR on OS X

  • Deadly simply!!
  • Just follow Compilation instructions (OS X)
  • Install log4cplus via MacPorts
  • Done!

OpenALPR on Windows!!!!!!

ooooh my my, why things are soooooo complicated on Windows!

Two lessons mainly, two new major knowledge points:

  • CMakeFiles in OpenALPR are well organised, standard, could be useful for educational purpose
    • Now understand the difference between Module Mode and Config Mode
    • The former uses Find<package>.cmake file located within your project
    • The latter uses <package>Config.cmake file located outside and produced by install command of other project
    • CMake: Of what use is find_package() if you need to specify CMAKE_MODULE_PATH anyway?
    • Library headers and #define
    • Logic is easy now: CMakeLists.txt —> FindTesseract.cmake —> LibFindMacros.cmake
    • LibFindMacros.cmake is for libfind_process MACRO which sets necessary variables, e.g. ${PREFIX}_FOUND, ${PREFIX}_INCLUDE_DIRS, ${PREFIX}_LIBRARIES
  • For some reason, the man behind OpenALPR decides that for Windows system, it should use static library rather than DLL.
    • alpr.exe is configured to link with openalpr-static
    • which in turn, requires the static version of tesseract - thank GOD I’ve decide to build my own version of tesseract from the scatch!

Run alpr.exe on Windows

$HOME\Software\openalpr\src\build-win[Release | Debug]

$HOME\Software\openalpr\config\ –config openalpr.conf.win

runtime_dir = $HOME/Software/openalpr/runtime_data

Problems

  • Main problem
  • Config file
  • wrong paths in the provided windows release
  • Feed program with openalpr.conf.defaults, remember to modify runtime_dir to point to openalpr/runtime_data
  • Debugging Issue
  • Crashed on cv::findContours() - no idea why!
  • Debug Assertion Failed! Expression: _pFirstBlock == pHead
  • Debug Assertion Failed! Expression: _pFirstBlock == pHead

Run alpr on Mac - Easy Peasy!

Manual call graph

  • detectandshow( ) —> Alpr::recognize(…) —> AlprImpl::recognize( unsigned char* pixelData, int bytesPerPixel, …) —> AlprImpl::recognize(cv::Mat img, std::vector regionsOfInterest) —> AlprImpl::recognizeFullDetails(…) —> AlprImpl::analyzeSingleCountry(…) —> Detector::detect(…) —> LicensePlateCandidate lp(&pipeline_data); —> LicensePlateCandidate::recognize() —> CharacterAnalysis::analyze() —> TextContours::load(cv::Mat threshold)

About OpenALPR detector

License Plate Recognition - Determining Color Range For Pixel Comparison

Check out OpenALPR (http://www.openalpr.com). It takes a different approach for plate localization -- it uses trained LBP patterns. You may want to use this library in addition to your color-based detection for even greater accuracy. For example, OpenALPR can detect potential plate regions, and then simply verify that a certain percentage of the area is yellow-ish.

0 0
原创粉丝点击