[英]Why pytesseract does not recognise single digits?
I am performing ocr on a site and specifically on these two images:我正在一个网站上执行 ocr,特别是在这两个图像上:
I am fairly new to OCR, I use the following:我对 OCR 相当陌生,我使用以下内容:
from PIL import Image
import pytesseract
my_image = '....png'
text = pytesseract.image_to_string(Image.open(my_image))
In the second image it recognises everything except the single digits 3, 4, 5, 6.在第二张图片中,它可以识别除单个数字 3、4、5、6 之外的所有内容。
In the first image it does not recognises the single digits too.在第一张图像中,它也无法识别单个数字。
I preprocess the images by resizing them, inverting them and using threshold.我通过调整图像大小、反转它们和使用阈值来预处理图像。
It's a standard font so I know there are other ways to do this, but until a certain degree it works for me, so I want to keep it simple before going to something more advanced.这是一种标准字体,所以我知道还有其他方法可以做到这一点,但在一定程度上它对我有用,所以我想在进入更高级的东西之前保持简单。
For the both image, you can对于这两个图像,您可以
For the first image, you can take part of the image selecting a range:对于第一张图像,您可以选择图像的一部分:
Result will be:结果将是:
62001
33000
Code:代码:
import cv2
import pytesseract
img1 = cv2.imread("lNKH4.png") # "FX2in.png"
gry1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
(h, w) = gry1.shape[:2]
gry1 = cv2.resize(gry1, (w*2, h*2))
gry1 = gry1[30:(h*2), w+50:w*2]
thr1 = cv2.threshold(gry1, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
txt1 = pytesseract.image_to_string(thr1, config="--psm 6 digits")
print(txt1)
cv2.imshow("thr1", thr1)
cv2.waitKey(0)
For the 2nd image:对于第二张图片:
Result will be:结果将是:
2
3 1.28 4.50 9.00
4 2.00 3.75 3.00
5 3.50 4.33 1.72
6 7.00 6.00 1.28
Same code, just remove the following line:相同的代码,只需删除以下行:
gry1 = gry1[30:(h*2), w+50:w*2]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.