python-3.x - Pytesseract 图像 OCR - 无法识别数字
问题描述
我有这个图像。
使用 python 3.8 运行 Pytesseract 会产生以下问题:
- “电话”这个词读作 O(不是零,O 和 oscar 一样)
- “传真”一词读作 2%。
- 电话号码读作(56031770
考虑的图像不包含框。框是在检测到的文本区域/单词周围应用框后从 cv2 输出中获取的。
读取传真号码没有问题。(866)357-7704(包括括号和连字符)
图像大小为 23 兆像素(从 pdf 文件转换而来) 该图像已在 opencv 中使用阈值进行预处理,以便您获得二进制图像 该图像不包含粗体字体。所以我没有使用任何侵蚀。
我该怎么做才能正确阅读电话号码?谢谢你。
PS:我使用的是 image_to_data(不是 image_to_text),因为我还需要知道页面上字符串的位置。
编辑:这是代码的相关部分:
from PIL import Image
import pytesseract
from pytesseract import Output
import argparse
import cv2
import os
import numpy as np
import math
from pdf2image import convert_from_path
from scipy.signal import convolve2d
import string
filename = "image.png"
image = cv2.imread(filename)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# estimate noise on image
H, W = gray.shape
M = [[1, -2, 1],
[-2, 4, -2],
[1, -2, 1]]
sigma = np.sum(np.sum(np.absolute(convolve2d(gray, M))))
sigma = sigma * math.sqrt(0.5 * math.pi) / (6 * (W-2) * (H-2))
# if image has too much noise then go with blurring method
if sigma > 10 :
# noisy
gray = cv2.medianBlur(gray, 3)
print("noises deblurred")
# otherwise go with threshholding method
else :
gray = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
print("threshhold applied")
d = pytesseract.image_to_data(gray, output_type=Output.DICT)
for t in d['text'] :
print(t)
因此,这将是 psm 3(默认)
版本 :
Tesseract : tesseract 4.1.1 (用 检索tesseract --version
) & pytessract : 版本: 0.3.2 (用 检索pip3 show pytesseract
)