简体   繁体   English

使用 Pytesseract OCR 从表格图像中识别特定数字

[英]Recognize specific numbers from table image with Pytesseract OCR

I want to read a column of number from an attached image (png file).我想从附加的图像(png 文件)中读取一列数字。

点击查看图片

My code is我的代码是

import cv2
import pytesseract
import os

img = cv2.imread(os.path.join(image_path, image_name), 0)
config= "-c 
        tessedit_char_whitelist=01234567890.:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

pytesseract.image_to_string(img, config=config)

This code gives me the output string: 'n113\\nun\\n1.08'.这段代码给了我输出字符串:'n113\\nun\\n1.08'。 As we can see, there are two problems:正如我们所见,有两个问题:

  1. It fails to recognize a decimal point in 1.13 (see attached picture).它无法识别 1.13 中的小数点(见附图)。
  2. It totally cannot read 1.11 (see attached picture).它完全无法读取 1.11(见附图)。 It just returns 'nun'.它只返回“修女”。

What is a solution to these problems?这些问题的解决方案是什么?

Bests最好的

You need to preprocess the image.您需要对图像进行预处理。 A simple approach is to resize the image, convert to grayscale, and obtain a binary image using Otsu's threshold.一种简单的方法是调整图像大小,转换为灰度,然后使用 Otsu 阈值获得二值图像。 From here we can apply a slight gaussian blur then invert the image so the desired text to extract is in white with the background in black.从这里我们可以应用轻微的高斯模糊,然后反转图像,以便提取的所需文本为白色,背景为黑色。 Here's the processed image ready for OCR这是准备好用于 OCR 的处理过的图像

Result from OCR OCR 的结果

1.13
1.11
1.08

Code代码

import cv2
import pytesseract
import imutils

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

# Resize, grayscale, Otsu's threshold
image = cv2.imread('1.png')
image = imutils.resize(image, width=400)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Blur and perform text extraction
thresh = 255 - cv2.GaussianBlur(thresh, (5,5), 0)
data = pytesseract.image_to_string(thresh, lang='eng',config='--psm 6')
print(data)

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

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

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