Skip to main content
 首页 » 编程设计

python之从图像中读取文本的 OCR 库(最好是 python)

2025年05月04日12del

我需要从一些图像中读取文本,图像清晰且噪声极低。 所以我最初的想法是获取文本应该很容易。 (我知道的很少)

我测试了一些 python 库,但不太成功 (pytesser) ,它们可能只有 10% 是正确的。 我转向Googles tesseract-occ但这还差得很远。

这是一个例子:

下面是结果:

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 识别真的有这么糟糕吗?

请您参考如下方法:

您需要对图像进行预处理,例如去除噪点,以获得更好的结果。稍后,您可以使用 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