tesseract 无法识别易于阅读的文本
Easily readable text not recognized by tesseract
我使用了 EAST(高效准确的场景文本检测器)的以下 PyTorch implementation 来识别和绘制许多图像中文本周围的边界框,它效果很好!
但是,为了从这些图像中提取文本并将它们转换为字符串,我正在尝试使用 pytesseract
进行 OCR 的下一步,但它非常失败。使用 --oem
和 --psm
的所有可能配置,我无法获得 pytesseract
来检测看起来非常清晰的文本,例如:
识别出的文字在图片下方。即使我应用了对比度增强,并尝试了扩张和腐蚀,我也无法让 tesseract 识别文本。这只是许多图像中文本更大更清晰的示例之一。关于转换、配置或其他库的任何建议都会有所帮助!
更新:尝试高斯模糊 + Otso 阈值后,我能够在白色背景上获得黑色文本(显然这对 pytesseract 来说是理想的),并且还添加了西班牙语,但它仍然无法阅读非常纯文本 - 因为示例:
读起来像乱码。
处理后的文字图片为 and
以及我正在使用的代码:
img_path = './images/fesa.jpg'
img = Image.open(img_path)
boxes = detect(img, model, device)
origbw = cv2.imread(img_path, 0)
for box in boxes:
box = box[:-1]
poly = [(box[0], box[1]),(box[2], box[3]),(box[4], box[5]),(box[6], box[7])]
x = []
y = []
for coord in poly:
x.append(coord[0])
y.append(coord[1])
startX = int(min(x))
startY = int(min(y))
endX = int(max(x))
endY = int(max(y))
#use pre-defined bounding boxes produced by EAST to crop the original image
cropped_image = origbw[startY:endY, startX:endX]
#contrast enhancement
clahe = cv2.createCLAHE(clipLimit=4.0, tileGridSize=(8,8))
res = clahe.apply(cropped_image)
text = pytesseract.image_to_string(res, config = "-psm 12")
plt.imshow(res)
plt.show()
print(text)
使用 these 个更新的数据文件。
This guide 批评 out-of-the box 性能(可能准确性也会受到影响):
Trained data. On the moment of writing, tesseract-ocr-eng APT package for Ubuntu 18.10 has terrible out of the box performance, likely because of corrupt training data.
根据我所做的以下测试,使用更新后的数据文件似乎可以提供更好的结果。这是我使用的代码:
import pytesseract
from PIL import Image
print(pytesseract.image_to_string(Image.open('farmacias.jpg'), lang='spa', config='--tessdata-dir ./tessdata --psm 7'))
我将 spa.traineddata(您的示例图像有西班牙语单词,对吗?)下载到 ./tessdata/spa.traineddata
。结果是:
ARMACIAS
第二张图片:
PECIALIZADA:
我使用 --psm 7
因为 here 它表示“将图像视为单个文本行”,我认为这对您的测试图像应该有意义。
在this Google Colab你可以看到我做的测试
我使用了 EAST(高效准确的场景文本检测器)的以下 PyTorch implementation 来识别和绘制许多图像中文本周围的边界框,它效果很好!
但是,为了从这些图像中提取文本并将它们转换为字符串,我正在尝试使用 pytesseract
进行 OCR 的下一步,但它非常失败。使用 --oem
和 --psm
的所有可能配置,我无法获得 pytesseract
来检测看起来非常清晰的文本,例如:
识别出的文字在图片下方。即使我应用了对比度增强,并尝试了扩张和腐蚀,我也无法让 tesseract 识别文本。这只是许多图像中文本更大更清晰的示例之一。关于转换、配置或其他库的任何建议都会有所帮助!
更新:尝试高斯模糊 + Otso 阈值后,我能够在白色背景上获得黑色文本(显然这对 pytesseract 来说是理想的),并且还添加了西班牙语,但它仍然无法阅读非常纯文本 - 因为示例:
读起来像乱码。
处理后的文字图片为
img_path = './images/fesa.jpg'
img = Image.open(img_path)
boxes = detect(img, model, device)
origbw = cv2.imread(img_path, 0)
for box in boxes:
box = box[:-1]
poly = [(box[0], box[1]),(box[2], box[3]),(box[4], box[5]),(box[6], box[7])]
x = []
y = []
for coord in poly:
x.append(coord[0])
y.append(coord[1])
startX = int(min(x))
startY = int(min(y))
endX = int(max(x))
endY = int(max(y))
#use pre-defined bounding boxes produced by EAST to crop the original image
cropped_image = origbw[startY:endY, startX:endX]
#contrast enhancement
clahe = cv2.createCLAHE(clipLimit=4.0, tileGridSize=(8,8))
res = clahe.apply(cropped_image)
text = pytesseract.image_to_string(res, config = "-psm 12")
plt.imshow(res)
plt.show()
print(text)
使用 these 个更新的数据文件。
This guide 批评 out-of-the box 性能(可能准确性也会受到影响):
Trained data. On the moment of writing, tesseract-ocr-eng APT package for Ubuntu 18.10 has terrible out of the box performance, likely because of corrupt training data.
根据我所做的以下测试,使用更新后的数据文件似乎可以提供更好的结果。这是我使用的代码:
import pytesseract
from PIL import Image
print(pytesseract.image_to_string(Image.open('farmacias.jpg'), lang='spa', config='--tessdata-dir ./tessdata --psm 7'))
我将 spa.traineddata(您的示例图像有西班牙语单词,对吗?)下载到 ./tessdata/spa.traineddata
。结果是:
ARMACIAS
第二张图片:
PECIALIZADA:
我使用 --psm 7
因为 here 它表示“将图像视为单个文本行”,我认为这对您的测试图像应该有意义。
在this Google Colab你可以看到我做的测试