1 问题描述
使用Tesseract进行ocr文字识别,运行识别代码,报错如下:
C:\Users\lishu\anaconda3\envs\pt2\python.exe D:/code/ptcontainer/opencv/car_reg.py
Traceback (most recent call last):
File "D:\code\ptcontainer\opencv\car_reg.py", line 100, in <module>
text = pytesseract.image_to_string(cropped, config='--psm 11')
File "C:\Users\lishu\anaconda3\envs\pt2\lib\site-packages\pytesseract\pytesseract.py", line 423, in image_to_string
return {
File "C:\Users\lishu\anaconda3\envs\pt2\lib\site-packages\pytesseract\pytesseract.py", line 426, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "C:\Users\lishu\anaconda3\envs\pt2\lib\site-packages\pytesseract\pytesseract.py", line 288, in run_and_get_output
run_tesseract(**kwargs)
File "C:\Users\lishu\anaconda3\envs\pt2\lib\site-packages\pytesseract\pytesseract.py", line 264, in run_tesseract
raise TesseractError(proc.returncode, get_errors(error_string))
pytesseract.pytesseract.TesseractError: (1, 'Error opening data file \\Tesseract-OCR\\tessdata/eng.traineddata Please make sure the TESSDATA_PREFIX environment variable is set to the parent directory of your "tessdata" directory. Failed loading language \'eng\' Tesseract couldn\'t load any languages! Could not initialize tesseract.')
Process finished with exit code 1
2 问题分析
2.1 检测Tesseract是否正确安装
通过命令查看,Tesseract已经正确安装,环境变量也设置正确
2.2 检查TESSDATA_PREFIX是否正确设置
TESSDATA_PREFIX的系统变量也进行正确设置。
3 问题解决
经过资料查找,找到问题所在:
Tesseract的安装路径必须和执行的python代码在同一路径下,才能成功找到eng.traineddata。
解决方案如下:
重新安装Tesseract程序,并安装到D盘(与python程序同一盘符下)目录下,重新设置TESSDATA_PREFIX系统变量和环境变量,问题解决。