Python 中 Tesseract OCR 的 UnicodeDecodeError
UnicodeDecodeError with Tesseract OCR in Python
我正在尝试在 Python 中使用 Tesseract OCR 从图像文件中提取文本,但我遇到了一个错误,我可以弄清楚如何处理它。我的所有环境都很好,因为我在 python!
中使用 ocr 测试了一些示例图像
这是代码
from PIL import Image
import pytesseract
strs = pytesseract.image_to_string(Image.open('binarized_image.png'))
print (strs)
以下是我从 eclipse 控制台得到的错误
strs = pytesseract.image_to_string(Image.open('binarized_body.png'))
File "C:\Python35x64\lib\site-packages\pytesseract\pytesseract.py", line 167, in image_to_string
return f.read().strip()
File "C:\Python35x64\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 20: character maps to <undefined>
我在 Windows10
上使用 python 3.5 x64
问题是 python 正在尝试使用控制台的编码 (CP1252) 而不是它的本意 (UTF-8)。 PyTesseract 已找到一个 unicode 字符,现在正试图将其转换为 CP1252,但它做不到。在另一个平台上,您不会遇到此错误,因为它将开始使用 UTF-8。
您可以尝试使用不同的函数(可能是 returns bytes
而不是 str
的函数,这样您就不必担心编码问题了)。您可以更改 python 的默认编码,如评论之一所述,尽管当您尝试在 windows 控制台上打印字符串时,这会导致问题。或者,这是我推荐的解决方案,您可以下载 Cygwin 和 运行 python 以获得干净的 UTF-8 输出。
如果您想要一个不会破坏任何东西(但)的快速而肮脏的解决方案,您可以考虑以下方法:
import builtins
original_open = open
def bin_open(filename, mode='rb'): # note, the default mode now opens in binary
return original_open(filename, mode)
from PIL import Image
import pytesseract
img = Image.open('binarized_image.png')
try:
builtins.open = bin_open
bts = pytesseract.image_to_string(img)
finally:
builtins.open = original_open
print(str(bts, 'cp1252', 'ignore'))
我遇到了和你一样的问题,但我不得不将 pytesseract 的输出保存到一个文件中。因此,我使用 pytesseract 创建了一个用于 ocr 的函数,并在保存到文件时添加了参数 encoding='utf-8'
所以我的函数现在看起来像这样:
def image_ocr(image_path, output_txt_file_name):
image_text = pytesseract.image_to_string(image_path, lang='eng+ces', config='--psm 1')
with open(output_txt_file_name, 'w+', encoding='utf-8') as f:
f.write(image_text)
我希望这对某人有所帮助:)
我正在尝试在 Python 中使用 Tesseract OCR 从图像文件中提取文本,但我遇到了一个错误,我可以弄清楚如何处理它。我的所有环境都很好,因为我在 python!
中使用 ocr 测试了一些示例图像这是代码
from PIL import Image
import pytesseract
strs = pytesseract.image_to_string(Image.open('binarized_image.png'))
print (strs)
以下是我从 eclipse 控制台得到的错误
strs = pytesseract.image_to_string(Image.open('binarized_body.png'))
File "C:\Python35x64\lib\site-packages\pytesseract\pytesseract.py", line 167, in image_to_string
return f.read().strip()
File "C:\Python35x64\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 20: character maps to <undefined>
我在 Windows10
上使用 python 3.5 x64问题是 python 正在尝试使用控制台的编码 (CP1252) 而不是它的本意 (UTF-8)。 PyTesseract 已找到一个 unicode 字符,现在正试图将其转换为 CP1252,但它做不到。在另一个平台上,您不会遇到此错误,因为它将开始使用 UTF-8。
您可以尝试使用不同的函数(可能是 returns bytes
而不是 str
的函数,这样您就不必担心编码问题了)。您可以更改 python 的默认编码,如评论之一所述,尽管当您尝试在 windows 控制台上打印字符串时,这会导致问题。或者,这是我推荐的解决方案,您可以下载 Cygwin 和 运行 python 以获得干净的 UTF-8 输出。
如果您想要一个不会破坏任何东西(但)的快速而肮脏的解决方案,您可以考虑以下方法:
import builtins
original_open = open
def bin_open(filename, mode='rb'): # note, the default mode now opens in binary
return original_open(filename, mode)
from PIL import Image
import pytesseract
img = Image.open('binarized_image.png')
try:
builtins.open = bin_open
bts = pytesseract.image_to_string(img)
finally:
builtins.open = original_open
print(str(bts, 'cp1252', 'ignore'))
我遇到了和你一样的问题,但我不得不将 pytesseract 的输出保存到一个文件中。因此,我使用 pytesseract 创建了一个用于 ocr 的函数,并在保存到文件时添加了参数 encoding='utf-8'
所以我的函数现在看起来像这样:
def image_ocr(image_path, output_txt_file_name):
image_text = pytesseract.image_to_string(image_path, lang='eng+ces', config='--psm 1')
with open(output_txt_file_name, 'w+', encoding='utf-8') as f:
f.write(image_text)
我希望这对某人有所帮助:)