Windows pytesseract image_to_osd Invalid resolution 0 dpi. Using 70 instead. Too few characters报错及解决
Posted 程序媛一枚~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Windows pytesseract image_to_osd Invalid resolution 0 dpi. Using 70 instead. Too few characters报错及解决相关的知识,希望对你有一定的参考价值。
Windows pytesseract image_to_osd Invalid resolution 0 dpi. Using 70 instead. Too few characters报错及解决
1. 安装
-
python3.7+
-
pip install pytesseract==0.1.9
-
安装tesseract-ocr(配置path环境变量或者在代码中指定tesseract_cmd)
2.报错及解决
2.1 TesseractNotFoundError
- 报错: 无可执行的tesseract
- 解决:配置PATH环境变量或者在代码中指定tesseract_cmd
import pytesseract
# 如果未在PATH中配置tesseract环境变量,则需要手动设置tesseract可执行文件的全路径
pytesseract.pytesseract.tesseract_cmd = r'D:\\tesseract\\tesseract.exe'
# Example tesseract_cmd = r'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract'
2.2 Error opening data file D:\\Tesseract-OCR\\fra.traineddata Please make sure the TESSDATA_PREFIX environment variable is set to your “tessdata” directory. Failed loading language ‘fra’ Tesseract couldn’t load any languages! Could not initialize tesseract
- 解决: 把fra.traineddata放到tesseract.exe上一级./tessdata的路径 或者在代码中导入:
import os
import pytesseract
# 语言文件.traindata
os.environ['TESSDATA_PREFIX'] = 'D:\\\\tesseract\\\\tessdata' # 在PATH中配置tesseract环境变量或者此处指定
# 如果未在PATH中配置tesseract环境变量,则需要手动设置tesseract可执行文件的全路径
pytesseract.pytesseract.tesseract_cmd = r'D:\\\\tesseract\\\\tesseract.exe'
2.3 raise TesseractError(proc.returncode, get_errors(error_string)) pytesseract.pytesseract.TesseractError: (1, ‘Estimating resolution as 304 UZN file C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\tess_oeu8q72f loaded. Warning. Invalid resolution 0 dpi. Using 70 instead. Too few characters. Skipping this page Error during processing.’)
直接执行命令行ok,pytesseract.image_to_osd(Image.open(‘test.png’))) 报错
tesseract E:\\mat\\py-demo-22\\extreme_points\\images\\normal.jpg E:\\mat\\py-demo-22\\extreme_points\\images\\normal_1 -l osd --psm 0
-
报错:tesseract升级的bug
-
解决:直接传递照片文件路径,或者切回低版本的tesseract:5.0.0-alpha-20201224 ok。详情可参考
# 获取方向和文本系统
# print(pytesseract.image_to_osd(Image.open('test.png'))) # happening on tesseract version released after Jan1, 2021.I have tested in tesseract version 5.0.0-alpha-20201224
print(pytesseract.image_to_osd('test.png'))
遇到其他问题了可以去github issues里找解决办法;
3. 源码
# 测试pytesseract的方法
# test_osd.py
import os
import pytesseract
from PIL import Image
# 语言文件.traindata
os.environ['TESSDATA_PREFIX'] = 'D:\\\\tesseract\\\\tessdata' # 在PATH中配置tesseract环境变量或者此处指定
# 如果未在PATH中配置tesseract环境变量,则需要手动设置tesseract可执行文件的全路径
pytesseract.pytesseract.tesseract_cmd = r'D:\\\\tesseract\\\\tesseract.exe'
# 图片转string
print("PIL image_to_string: ", pytesseract.image_to_string(Image.open('test.png')))
# 提供图片的相对或者绝对路径
print('image_to_string: ', pytesseract.image_to_string('test.png'))
# 打印pytesseract支持的所有语言
print('langs: ', pytesseract.get_languages(config=''))
# French语言的识别
print('fra image_to_string: ',pytesseract.image_to_string(Image.open('test-european.jpg'), lang='fra'))
# 批量处理单个文件list
print('batch Images image_to_string: ',pytesseract.image_to_string('images.txt'))
# 超时后结束pyteseract
try:
print(pytesseract.image_to_string('test.jpg', timeout=2)) # Timeout after 2 seconds
print(pytesseract.image_to_string('test.jpg', timeout=0.5)) # Timeout after half a second
except RuntimeError as timeout_error:
# Tesseract processing is terminated
pass
# 获取预测的边界框
# print(pytesseract.image_to_boxes(Image.open('test.png')))
print('image_to_boxes: ',pytesseract.image_to_boxes('test.png'))
# 获取详细数据,包括方框、置信度、行号和页码
print('image_to_data: ',pytesseract.image_to_data(Image.open('test.png')))
# 获取方向和文本系统
# print(pytesseract.image_to_osd(Image.open('test.png'))) # but this issue is happening on tesseract version released after Jan1, 2021.I have tested in tesseract version 5.0.0-alpha-20201224
print('image_to_osd: ',pytesseract.image_to_osd('test.png'))
# Get a searchable PDF
# pdf = pytesseract.image_to_pdf_or_hocr('test.png', extension='pdf') # 报错
# with open('test.pdf', 'w+b') as f:
# f.write(pdf) # pdf type is bytes by default
# Get HOCR output
# hocr = pytesseract.image_to_pdf_or_hocr('test.png', extension='hocr') # error tesseract v5.2.0.20220712
# Get ALTO XML output
xml = pytesseract.image_to_alto_xml('test.png')
print('image_to_alto_xml: ', xml)
参考
- https://digi.bib.uni-mannheim.de/tesseract/
- https://pypi.org/project/pytesseract/0.1.9/
- https://github.com/seminar2012/tesseract?organization=seminar2012&organization=seminar2012
- 各版本语言包下载路径:
以上是关于Windows pytesseract image_to_osd Invalid resolution 0 dpi. Using 70 instead. Too few characters报错及解决的主要内容,如果未能解决你的问题,请参考以下文章
python pytesseract.image_to_string 无法读取图像中的文本