简体   繁体   English

无法使用 Tesseract 对字母数字图像进行 OCR

[英]Unable to OCR alphanumerical image with Tesseract

I'm trying to read some alphanumerical strings in python with pytesseract.我正在尝试使用 pytesseract 读取 python 中的一些字母数字字符串。 I pre-process the images to reduce noise and make them black and white, but I consistently have issues reading the digits inside the string.我对图像进行预处理以减少噪音并将它们变成黑白的,但我始终无法读取字符串中的数字。

original:原来的: 原图

after cleanup:清理后: 清理后的图像

Extracted text: WISOMW提取文本: WISOMW

Code used:使用的代码:

def convert(path):    
    image = cv2.imread(path)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (3, 3), 0)
    thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
    invert = 255 - thresh
    cv2.imwrite("processed.jpg", invert)

    # Perform text extraction
    return pytesseract.image_to_string(invert, config="--psm 7")

I've tried different configuration options for tesseract:我为 tesseract 尝试了不同的配置选项:

  • oem : tried 1, 3 oem :试过 1、3
  • psm : tried different modes psm :尝试了不同的模式
  • tessedit_char_whitelist : limited to alphanumerical characters tessedit_char_whitelist :限于字母数字字符

I feel I'm missing something obvious given that it reliably reads the alpha characters.鉴于它可靠地读取字母字符,我觉得我遗漏了一些明显的东西。 Any ideas of what can it be?关于它可能是什么的任何想法?

You were so close.你是如此接近。 A dilate helps increase white/decrease black.扩张有助于增加白色/减少黑色。 The resolution is low, so a small kernel is used for dilate.分辨率低,所以用一个小的kernel做dilate。 If you remove the _INV from your threshold step, you don't need to do another inversion.如果您从阈值步骤中删除 _INV,则不需要再进行一次反演。

import cv2
import numpy as np
import pytesseract

img = cv2.imread('wis9mw.jpg', cv2.IMREAD_GRAYSCALE )

img = cv2.GaussianBlur(img, (3, 3), 0)
img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

kernel = np.ones((1,1), np.uint8)
img = cv2.dilate(img, kernel, iterations=1)

cv2.imwrite('processed.jpg', img)

text = pytesseract.image_to_string(img, config="--psm 6")
print(text)

gives

WIS9MW

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM