【Opencv实战】 识别验证码

环境说明

opencv-python          3.4.4.19

pytesseract            0.2.6

tesseract              0.1.3

安装

第一步:安装Tesseract-OCR,下载地址:tesseract-ocr,请记住自己的安装位置,一会儿要用。

第二步:安装tesseract,直接在cmd,命令行输入

pip install tesseract

进行进行自动安装,由于网络问题,这里下载的速度会非常慢,这里给出下载链接。点这里哦

第三步:安装pytesseract,在命令行模式输入:

pip install pytesseract

这个安装的很快。之后通过

pip list

查看是否安装成功

测试

import cv2 as cv
from PIL import Image
import pytesseract 
 
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, 6))
    binl = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 1))
    open_out = cv.morphologyEx(binl, cv.MORPH_OPEN, kernel)
    cv.bitwise_not(open_out, open_out)  # 背景变为白色
    cv.imshow("dstImage", open_out)
    textImage = Image.fromarray(open_out)
    text = pytesseract.image_to_string(textImage)
    print("Result:%s"%text) 
 
src = cv.imread("yzm.jpg")
cv.imshow("srcImage", src)
recognize_text()
cv.waitKey(0)
cv.destroyAllWindows()

若出现:TesseractNotFoundError: tesseract is not installed or it's not in your path,报错

请将路径:“C:\Program Files\Python36\Lib\site-packages\pytesseract”下的pytesseract.py进行修改:

# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
tesseract_cmd = 'tesseract'

请替换为

# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
tesseract_cmd = r'D:\Program Files (x86)\Tesseract-OCR\tesseract.exe'

因为这里要更换为自己路径。就是第一步安装Tesseract-OCR的路径。

测试效果

 

测试图片

【Opencv实战】 识别验证码_第1张图片

结果:

【Opencv实战】 识别验证码_第2张图片

【Opencv实战】 识别验证码_第3张图片

【Opencv实战】 识别验证码_第4张图片


★finished by songpl,2019.1.15

 

你可能感兴趣的:(OpenCV学习)