【python】图像识别之图片转文字(验证码)

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

1.安装pip install pytesseract

2..安装tesseract-ocr,下载地址:https://github.com/UB-Mannheim/tesseract/wiki

安装的时候选择把chi_sim(中文简体)和chi_tra(中文繁体)数据库安装上

3.设置环境变量

问题:pytesseract.pytesseract.TesseractError: (1, 'Error opening data file C:\\Program Files (x86)\\Tesseract-OCR/eng.traineddata Please make sure the TESSDATA_PREFIX environment variable is set to your "tessdata" directory. Failed loading language \'eng\' Tesseract couldn\'t load any languages! Could not initialize tesseract.')

解决方法:

在.py文件中加入配置:tessdata_dir_config ='--tessdata-dir "C:\\Program Files (x86)\\Tesseract-OCR\\tessdata"'


代码:

import time

time1 = time.time()

from PILimport Image

import pytesseract

tessdata_dir_config ='--tessdata-dir "C:\\Program Files (x86)\\Tesseract-OCR\\tessdata"'

###########二值化算法

def binarizing(img, threshold):

pixdata = img.load()

w, h = img.size

for yin range(h):

for xin range(w):

if pixdata[x, y] < threshold:

pixdata[x, y] =0

            else:

pixdata[x, y] =255

    return img

image = Image.open(r'E://pythonprogram//data//image2.png')

###########去除干扰线算法

def depoint(img):# input: gray image

    pixdata = img.load()

w, h = img.size

for yin range(1, h -1):

for xin range(1, w -1):

count =0

            if pixdata[x, y -1] >245:

count = count +1

            if pixdata[x, y +1] >245:

count = count +1

            if pixdata[x -1, y] >245:

count = count +1

            if pixdata[x +1, y] >245:

count = count +1

            if count >2:

pixdata[x, y] =255

    return img

# 转化为灰度图

img = image.convert('L')

# 把图片变成二值图像。

img1 = binarizing(img, 190)

# img2=depoint(img1)

img1.show()

code = pytesseract.image_to_string(img1,config=tessdata_dir_config)

print(str(code))


结果:

验证码图片
输出结果
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容