用于从图像中读取文本的OCR库(最好是Python)

2 投票
1 回答
6184 浏览
提问于 2025-04-18 05:32

我需要从一些图片中读取文字,这些图片很清晰,噪音也很少。最开始我以为这应该很简单,能轻松提取出文字。(我真是太天真了)

我试了一些python库,但效果都不太好(比如pytesser),它们大概只能识别10%的文字。于是我转向了谷歌的tesseract-ocr,但效果还是差得远。

这里有一个例子:

在这里输入图片描述

下面是识别的结果:

nemnamons

Ill
w_on

lhggerllo
' 59
' as

\M_P2ma\

vuu uu

Cafllode omer
Mom | Dyna
Mom | Dyna

lnggerllo



2vMnne= Tr2rspnn| Factory (Hexmy;

lalgeflll Uxzlconflg
w_o«
w_o«

cammem

我哪里做错了?还是说OCR识别真的这么差?

1 个回答

1

为了得到更好的结果,你需要先对图片进行一些处理,比如去掉噪声。之后,你可以使用一个叫做 pytesseract 的库,从你的图片中提取文字:

def get_string(img_path):
    img = cv2.imread(img_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Remove some noise
    kernel = np.ones((1, 1), np.uint8)
    img = cv2.dilate(img, kernel, iterations=1)
    img = cv2.erode(img, kernel, iterations=1)
    cv2.imwrite("removed_noise.png", img)    

    # Recognize text with tesseract for python
    result = pytesseract.image_to_string(Image.open("removed_noise.png"))

    return result

撰写回答