为什么pytesseract不能识别个位数? [英] Why pytesseract does not recognise single digits?
问题描述
我正在一个网站上执行 ocr,特别是在这两个图像上:
我对 OCR 相当陌生,我使用以下内容:
from PIL 导入图片导入 pytesseractmy_image = '....png'文本 = pytesseract.image_to_string(Image.open(my_image))
在第二张图片中,它可以识别除单个数字 3、4、5、6 之外的所有内容.
在第一张图片中,它也无法识别单个数字.
我通过调整图像大小、反转它们和使用阈值来预处理图像.
这是一种标准字体,所以我知道还有其他方法可以做到这一点,但在一定程度上它对我有用,所以我想在进行更高级的事情之前保持简单.
对于这两个图像,你可以
- 对图像进行上采样:为了准确识别.
- 应用
结果将是:
6200133000
代码:
导入 cv2导入 pytesseractimg1 = 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 位数字")打印(txt1)cv2.imshow(thr1", thr1)cv2.waitKey(0)
对于第二张图片:
结果将是:
23 1.28 4.50 9.004 2.00 3.75 3.005 3.50 4.33 1.726 7.00 6.00 1.28
相同的代码,只需删除以下行:
gry1 = gry1[30:(h*2), w+50:w*2]
I am performing ocr on a site and specifically on these two images:
I am fairly new to OCR, I use the following:
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.
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
- upsample the image : For accurate recognition.
- apply simple-thresholding: To display the features.
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]
这篇关于为什么pytesseract不能识别个位数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!