opencv learning twenty-four: digital verification code recognition case

Install Tesseract-OCR and python integration in windows 10 environment
My environment win10+python3.7 +opencv3.4

Preface

Tesseract is an open source ocr engine that can be used out of the box. The project was originally supported by HP Labs. It was ported to Windows in 1996 and C++ was implemented in 1998. Tesseract was announced as open source by Hewlett-Packard in 2005. From 2006 to the present, it has been developed by Google.

The official website currently supports the recognition of more than 100 languages. According to my test, I currently feel that the accuracy of the machine-printed English or Arabic numerals is quite high, but the effect is very good for anything handwritten. Normal, but this is pretty good.

Tesseract installation

(1) Tesseract itself does not have a windows installation package, but it specifies a third-party packaged windows installation package, which is explained on its wiki. You can download it directly from this address: https://digi.bib.uni -mannheim.de/tesseract/ After
downloading, it is an exe installation package. Just right-click and install. After the installation is complete, configure the environment variables, edit the path in the system variables, and add the following installation path:

C:\Program Files (x86)\Tesseract-OCR

After the installation is complete, directly cmd input:

命令:
tesseract -v

输出如下,即代表成功:

tesseract 4.0.0-beta.1-108-gf291
 leptonica-1.76.0
  libgif 5.1.4 : libjpeg 8d (libjpeg-turbo 1.5.3) : libpng 1.6.34 : libtiff 4.0.9 : zlib 1.2.11 : libwebp 0.6.1 : libopenjp2 2.2.0

Note that this step must be installed on windows, otherwise an exception will be thrown when running the program
(2) Install the python package interface:

pip install pillow  #一个python的图像处理库,pytesseract依赖
pip install pytesseract

When pip install pillow fails to install, try this:

pip install Pillow-PIL

Note that the first step must be installed successfully, and the environment variables must be configured at the same time, otherwise an error will be reported in the second step, because the second step is an interface, and the original C++ class library written in the first step will be called when running.

Digital verification code identification (wire segment interference)

import cv2 as cv
import numpy as np
from PIL import Image
import pytesseract as tess
def recognize_text():
    #首先灰值处理 然后二值化
    gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
    ret, binary= cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
    #去除干扰线 获取结构元素,进行开操作处理
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (1, 3))
    bin1 = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 1))
    openout = cv.morphologyEx(bin1, cv.MORPH_OPEN, kernel)

    cv.imshow("binary-image", binary)
    cv.imshow("openout-image", openout)

    cv.bitwise_not(openout, openout)
    textImage = Image.fromarray(openout)
    text = tess.image_to_string(textImage)
    print("识别结果:%s" % text)


src = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/yanzhengma2.png")  #读取图片位置
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
recognize_text()
cv.waitKey(0)
cv.destroyAllWindows()

Run screenshot:
Insert picture description here
You may get an error the first time you run:

pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path

It means that there is a problem with your tesseract environment variable and an
error: pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path

Digital verification code identification (wireless segment interference, normal verification code)

import cv2 as cv
import numpy as np
from PIL import Image
import pytesseract as tess
def recognize_text():
    #首先灰值处理 然后二值化
    gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
    ret, openout = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
    cv.imshow("openout-image", openout)
    
    cv.bitwise_not(openout, openout)
    textImage = Image.fromarray(openout)
    text = tess.image_to_string(textImage)
    print("识别结果:%s" % text)


src = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/yanzhengma3.png")  #读取图片位置
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
recognize_text()
cv.waitKey(0)
cv.destroyAllWindows()

Run screenshot:
Insert picture description here
Tesseract recognizes relatively regular characters printed by the machine. As for handwritten characters, the recognition effect is relatively poor. You can see that the above handwritten digits are recognized incorrectly. Of course, there are also tunings. For example, grayscale, blur, de-darkness, binarization, etc. may result in slightly better results.

Guess you like

Origin blog.csdn.net/weixin_44145452/article/details/112856981