简体   繁体   English

如何使用 Pytesseract 提取图像中的小数

[英]How to extract decimal in image with Pytesseract

要提取的图像

Above is the image ,I have tried everything I could get from SO or google ,nothing seems to work.上面是图像,我已经尝试了所有可以从 SO 或 google 获得的东西,但似乎没有任何效果。 I can not get the exact value in image , I should get 2.10 , Instead it always get 210.我无法获得 image 中的确切值,我应该得到 2.10 ,而总是得到 210 。

And it is not limited to this image only any image which have a decimal before number 1 tesseract ignores the decimal value.并且不限于此图像,只有在数字 1 tesseract 之前有小数的任何图像都会忽略十进制值。

 def returnAllowedAmount(self,imgpath):
        th = 127
        max_val = 255
        img = cv2.imread(imgpath,0) #Load Image in Memory
        img = cv2.resize(img, None, fx=2.5, fy=2.5, interpolation=cv2.INTER_CUBIC) #rescale Image
        img = cv2.medianBlur(img, 1)
        ret , img = cv2.threshold(img,th,max_val,cv2.THRESH_TOZERO)
        self.showImage(img)

        returnData = pytesseract.image_to_string(img,lang='eng',config='-psm 13 ' )
        returnData = ''.join(p for p in returnData if p.isnumeric() or p == ".") # REMOVE $ SIGN

Before throwing the image into Pytesseract, some preprocessing to clean/smooth the image helps.在将图像放入 Pytesseract 之前,一些用于清理/平滑图像的预处理会有所帮助。 Here's a simple approach这是一个简单的方法

  • Convert image to grayscale and enlarge image将图像转换为灰度并放大图像
  • Threshold临界点
  • Perform morphological operations to clean image执行形态学操作以清洁图像
  • Invert image反转图像

First we convert the image to grayscale, resize using the imutils library then threshold to obtain a binary image首先我们将图像转换为灰度,使用imutils库调整大小,然后阈值以获得二值图像

在此处输入图片说明

Now we perform morphological transformations to smooth the image现在我们执行形态变换来平滑图像

在此处输入图片说明

Now we invert the image for Pytesseract and add a Gaussian blur现在我们反转 Pytesseract 的图像并添加高斯模糊

在此处输入图片说明

We use the --psm 10 config flag since we want to treat the image as a single character.我们使用--psm 10配置标志,因为我们希望将图像视为单个字符。 Here's some additional configuration flags that could be useful这是一些可能有用的附加配置标志

Results结果

$2.10 2.10 美元

After filtering过滤后

2.10 2.10

import cv2
import pytesseract
import imutils

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

image = cv2.imread('1.png',0)
image = imutils.resize(image, width=300)
thresh = cv2.threshold(image, 150, 255, cv2.THRESH_BINARY_INV)[1]

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)

result = 255 - close 
result = cv2.GaussianBlur(result, (5,5), 0)

data = pytesseract.image_to_string(result, lang='eng',config='--psm 10 ')
processed_data = ''.join(char for char in data if char.isnumeric() or char == '.')
print(data)
print(processed_data)

cv2.imshow('thresh', thresh)
cv2.imshow('close', close)
cv2.imshow('result', result)
cv2.waitKey()

I was able to increase the number of correct decimals by using the methods mentioned in the other answers.通过使用其他答案中提到的方法,我能够增加正确小数的数量。 Yet, a small share of the decimals were not recognized correctly.然而,一小部分小数没有被正确识别。

The solution I found was to change the language setting for pytesseract.我找到的解决方案是更改 pytesseract 的语言设置。

I was using a non-English setting, but changing the config to lang='eng' fixed all remaining issues.我使用的是非英语设置,但将配置更改为lang='eng'修复了所有剩余问题。

Not sure what the reason is, but with the new LSTM engine for Tesseract, the training data is probably mostly English.不确定是什么原因,但使用 Tesseract 的新 LSTM 引擎,训练数据可能主要是英语。

Sometimes tesseract is oddly sensitive to image size.有时 tesseract 对图像大小异常敏感。 You can often get better results by scaling your image.您通常可以通过缩放图像来获得更好的结果。

I scaled your image by a factor of 2 and I got good results.我将您的图像缩放了 2 倍,结果很好。

import cv2
import pytesseract

# if windows
# pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe'

img = cv2.imread('twoten.png', 0)
img = cv2.resize(img, (0,0), fx=2, fy=2)

config = ("--psm 12")

data = pytesseract.image_to_string(img, lang='eng', config = config)

print(data)

which gave this in a console:这在控制台中给出了这个:

$2.10

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

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