[英]Tesseract OCR on binary image
I have a binary image like this,我有一个像这样的二进制图像,
I want to extract the numbers in the image using tesseract ocr in Python.我想在 Python 中使用 tesseract ocr 提取图像中的数字。 I used
pytesseract
like this on the image,我在图像上使用了这样的
pytesseract
,
txt = pytesseract.image_to_string(img)
But I am not getting any good results.但我没有得到任何好的结果。
What can I do in pre-processing or augmentation that can help tesseract do better.?我可以在预处理或增强中做些什么来帮助 tesseract 做得更好。?
I tried to localize the text from the image using East Text Detector
but it was not able to recognize the text.我尝试使用
East Text Detector
本地化图像中的East Text Detector
但无法识别文本。
How to proceed with this in python.?如何在python中继续这个。?
I think the page-segmentation-mode is an important factor here.我认为页面分割模式是这里的一个重要因素。
Since we are trying to read column values, we could use --psm 4
( source )由于我们正在尝试读取列值,我们可以使用
--psm 4
( source )
import cv2
import pytesseract
img = cv2.imread("k7bqx.jpg")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
txt = pytesseract.image_to_string(gry, config="--psm 4")
We want to get the text starts with #
我们想让文本以
#
开头
txt = sorted([t[:2] for t in txt if "#" in t])
Result:结果:
['#3', '#7', '#9', '#€']
But we miss 4, 5, we could apply adaptive-thresholding
:但是我们错过了 4、5,我们可以应用
adaptive-thresholding
:
Result:结果:
['#3', '#4', '#5', '#7', '#9', '#€']
Unfortunately, #2
and #6
are not recognized.不幸的是,
#2
和#6
无法识别。
Code:代码:
import cv2
import pytesseract
img = cv2.imread("k7bqx.jpg")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.adaptiveThreshold(gry, 252, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY_INV, blockSize=131, C=100)
bnt = cv2.bitwise_not(thr)
txt = pytesseract.image_to_string(bnt, config="--psm 4")
txt = txt.strip().split("\n")
txt = sorted([t[:2] for t in txt if "#" in t])
print(txt)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.