在检测电话号码的同时改善tesseract的输出

时间:2020-06-13 20:58:21

标签: ocr tesseract python-tesseract

我遇到了读取图像中电话号码的问题。我尝试使用tesseract在图像中检测到它们,但是有时它给我一个错误的答案。例如,数字是8 995 005-81-86,但是tesseract给我输出了8 995 0005-81-86。我该如何解决?也许二值化?


代码是基本的

import pytesseract as pt
from PIL import Image

img = Image.open('1.png')
number = pt.image_to_string(img)

print(number)

https://i.stack.imgur.com/kvhAq.png

1 个答案:

答案 0 :(得分:1)

为获得最佳效果,您应该在白色文字上使用黑体字

import cv2
from PIL import Image

img = cv2.imread('kvhAq.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY)
im = Image.fromarray(thresh.astype("uint8"))
print(pytesseract.image_to_string(im))

enter image description here