使用 Tesseract 从小图像中读取日期 [英] Using Tesseract to read dates from a small images
问题描述
我有一组相当小的包含日期的图像.尺寸可能有问题,但我会说质量还可以.我已按照
现在,这读作 9 MAR 2021\n\x0c
.不错,但是第一个 2
读作 "
.在这一点上,我认为我滥用了 Tesseract 的部分功能.毕竟,我知道它应该期待什么,即"%d %b %Y"
.
有没有办法告诉 Tesseract 考虑到这种强约束,它应该尝试找到最佳匹配?将此元数据提供给引擎应该会极大地促进该任务.我一直在阅读文档,但我找不到这样做的方法.
我在 Tesseract 4.1 上使用 pytesseract
.使用 Pytyon 3.9.
您需要了解以下内容:
我们在不丢失任何像素的情况下对图像进行上采样.
其次,我们需要将图片中的字符加粗,使OCR结果准确.
现在 OCR:
2021 年 3 月 29 日
代码:
导入 cv2导入 pytesseract#加载图片img = cv2.imread(xsGBK.jpg")# 使图像居中img = cv2.copyMakeBorder(img, 50, 50, 50, 50, cv2.BORDER_CONSTANT, value=[0, 0, 0])# 转换为灰度灰色 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 扩张gry = cv2.dilate(gry,无,迭代=1)# OCR打印(pytesseract.image_to_string(灰色))# 展示cv2.imshow(",灰色)cv2.waitKey(0)
I have a rather small set of images which contains dates. The size might be a problem, but I'd say that the quality is OK. I have followed the guidelines to provide the clearest image I can to the engine. After resizing, apply filters, lots of trial and error, etc. I came up with an image that is almost properly read. I put an example below:
Now, this is read as "9 MAR 2021\n\x0c
. Not bad, but the first 2
is read as "
. At this point I think I'm misusing part of the power of Tesseract. After all, I know what it should expect, i.e. something as "%d %b %Y"
.
Is there a way to tell Tesseract that it should try to find the best match given this strong constraint? Providing this metadata to the engine should heavily facilitate the task. I have been reading the documentation, but I can't find the way to do this.
I'm using pytesseract
on Tesseract 4.1. with Pytyon 3.9.
You need to know the followings:
Now if we center the image (by adding borders):
We up-sample the image without losing any pixel.
Second, we need to make the characters in the image bold to make the OCR result accurate.
Now OCR:
29 MAR 2021
Code:
import cv2
import pytesseract
# Load the image
img = cv2.imread("xsGBK.jpg")
# Center the image
img = cv2.copyMakeBorder(img, 50, 50, 50, 50, cv2.BORDER_CONSTANT, value=[0, 0, 0])
# Convert to the gray-scale
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Dilate
gry = cv2.dilate(gry, None, iterations=1)
# OCR
print(pytesseract.image_to_string(gry))
# Display
cv2.imshow("", gry)
cv2.waitKey(0)
这篇关于使用 Tesseract 从小图像中读取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!