In python, tesseract is called as an api to identify image verification codes,

Source: Internet
Author: User
Tags tesseract ocr

In python, tesseract is called as an api to identify image verification codes,
I. background

Previously, I introduced how to call the tesseract ocr engine in python. At that time, I mainly introduced the shell mode. In shell mode, the tesseract program needs to be installed, and the efficiency is relatively low.

Today we will introduce how to call the api. Because the bloggers mainly develop the api Based on the windows environment, the api calls here mainly refer to dll calls (such as. so calls in linux)

 

Ii. tesseract dll download URL

Https://github.com/charlesw/tesseract this URL contains the compiled exe and dll files, and x86, x64 two architectures have.

(Note: x86 or x64 dll depends only on your python architecture, not the operating system architecture, even in a 64-bit operating system, if your python is a 32-bit version, select the x86 version dll ).

The blogger uses 64-bit Python, so it enters tesseract/src/lib/TesseractOcr/X64/Directory. Download The liblept172.dll and libtesseract304.dll. (If you want to use shellform, you can also download tesseract.exe. To improve the performance of previous blog posts, you can skip tesseract installation .)

 

Iii. Language Pack download URL (tessdata)

Note that You will also need to download the language data files for tesseract 3.04 from tesseract-ocr.

URL: https://github.com/tesseract-ocr/tesseract, download the tessdata directory, and dll to the same directory.

 

4. Download the vc 2015 + release package

Note:

Since tesseract and leptonica binaries are compiled with Visual Studio 2015 you'll need to ensure you have the Visual Studio 2015 Runtime installed.

This tesseract dll is compiled using vs 2015, so it must be installed with its own line package, which is also divided into X64 and X86 versions, or depends on your development environment, does not depend on the operating system.

5. Install the pyocr for python package

With pip can install: pip install pyocr, You can also download the source code to the official website, manual installation: https://github.com/jflesch/pyocr

The latest pyocr is 0.4.1, and its source code is based on tesseract 3.0.2. The tesseract downloaded earlier is already version 3.0.4, so you need to change the pyocr source code. (It should be noted that the pyocr package is more complex than the pytesseract package mentioned in the previous blog. It also supports shell, api, and Cuneiform .)

Modify the source code file C: \ Python27 \ Lib \ site-packages \ pyocr \ libtesseract \ libtesseract_raw.py.

As shown in the following figure: the line in red is changed to 304dll.

 

TESSDATA_PREFIX = os.getenv('TESSDATA_PREFIX', None)if sys.platform[:3] == "win":    libnames = [        # Jflesch> Don't they have the equivalent of LD_LIBRARY_PATH on        # Windows ?        "../vs2010/DLL_Release/libtesseract302.dll",        "libtesseract304.dll", #libtesseract302.dll    ]else:    libnames = [        "libtesseract.so.3",    ]

 

  

 

6. Compile the test program

Directly put the source pyocr-test.py:

# Coding = UTF-8 ''' the current directory must contain tessdata, libtesseract304.dll, liblept172.dll ''' import OS, essdir = OS. getenv ('tessdata _ prefix', None) if tessdir is None: tessdir = OS. path. split (OS. path. realpath (_ file _) [0] OS. environ ['tessdata _ prefix'] = tessdirif tessdir not in OS. environ ['path']: OS. environ ['path'] = tessdir + ';' + OS. environ ['path'] # sys. path. append (tessdir) print OS. environ ['path'] print OS. environ ['tessdata _ prefix'] from pyocr import libtesseractfrom pyocr. builders import TextBuilderfrom PIL import Imagefilename require '1.png 'img = Image. open (filename) # Do not set to single-row mode, no output bu = TextBuilder (tesseract_layout = 7) # lang is the language, default use enuplint libtesseract. image_to_string (img, lang = 'fontet', builder = bu)

Notes:

1. The dll and tessdata Language Pack folder downloaded above, which is put in the same directory of the pyocr-test.py by default;

2. You must install the vc ++ 2015 release package (vc_redist.x86.exe or vc_redist.x64.exe). Otherwise, the ctypes. cdll. LoadLibrary (libname) fails to call the dll.

3. The first section in the source code is mainly used to solve the problem that python cannot find the dll. The main function of this Code is to add the current directory to the system PATH environment variable, to ensure that the dll can be found.

This location is prone to problems, mainly OS. path. the internal functions and constants realpath and _ file _ may have different results in different development environments or even different development tools, regardless of pyocr, it is mainly about python.

import ostessdir = os.getenv('TESSDATA_PREFIX', None)if tessdir is None  :    tessdir = os.path.split(os.path.realpath(__file__))[0]     os.environ['TESSDATA_PREFIX'] = tessdirif tessdir not in os.environ['PATH']:    os.environ['PATH']= tessdir+';' +os.environ['PATH']

4. During the actual test, it is found that if the bu = TextBuilder (tesseract_layout = 7) is not written, it will lead to an error in recognition. This is the same as the-psm 7 function mentioned in the previous blog post, that is, the single row mode.

Above

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.