改善tesseract的检测

时间:2020-06-12 16:41:20

标签: python python-3.x opencv python-tesseract

我尝试使用Tesseract从此图像中提取文本。
enter image description here
我尝试过的代码:

img = Image.open('downloadedpng.jpeg').convert('L')
ret,img = cv2.threshold(np.array(img), 125, 255, cv2.THRESH_BINARY)
img = Image.fromarray(img.astype(np.uint8))
print(pytesseract.image_to_string(img))

我得到的输出: re vie
我尝试使用以下代码进行侵蚀和扩张:

img_erosion = cv2.erode(img, kernel, iterations=1)
img_dilation = cv2.dilate(img, kernel, iterations=1)

但是我遇到了错误。 知道如何将其正确转换为字符串吗?

1 个答案:

答案 0 :(得分:2)

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

import cv2
from PIL import Image

img = cv2.imread('1.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.bitwise_not(img) # <- invert
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