tesseract无法识别的易读文本

问题描述

我使用了以下{strong> EAST (有效和准确的场景文本检测器)中的PyTorch implementation来识别和绘制许多图像中文本周围的边框,并且效果很好! / p>

但是,我正在尝试pytesseract进行OCR以便从这些图像中提取文本并将其转换为字符串的下一步–严重失败。使用--oem--psm的所有可能配置,我无法获得pytesseract来检测看似非常清晰的文本,例如:

enter image description here

识别的文本在图像下方。即使我已经应用了对比度增强功能,并且也尝试过扩张和腐蚀,但我仍无法通过tesseract来识别文本。这只是文本更大,更清晰的许多图像的一个示例。关于转换,配置或其他库的任何建议都将有所帮助!

更新:尝试使用高斯模糊+ Otso阈值化后,我能够在白色背景上获得黑色文本(显然这是pytesseract的理想选择),并且还添加了西班牙语,但是它仍然无法读取非常纯净的文本-例如:

img3

读取为乱码。

已处理的文本图像为

img1

img2

以及我使用的代码:


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批评了开箱即用的性能(也许准确性也可能受到影响):

训练数据。在撰写本文时,可能是因为损坏的训练数据,用于Ubuntu 18.10的tesseract-ocr-eng APT软件包的开箱即用性能很差。

根据我所做的以下测试,使用更新的数据文件似乎可以提供更好的结果。这是我使用的代码:

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中,您可以看到我所做的测试。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...