Pytesseract(Tesseract OCR)无法获取一些数字

S42*_*20L 1 python ocr opencv computer-vision python-tesseract

我一直在开发一个使用光学字符识别来读取财务报表的程序,我一生都无法弄清楚为什么我正在使用的开源模块仍然无法读取某些数字。

我创建了一个输出文件,在原始输入周围带有绿色框,其中正在检测文本。在这种情况下,带有“381”的行被选取,但下面的行(具有相同的精确格式)被忽略。

35 还没被接...

我在提取数据之前使用此代码对图像进行预处理,因为之前的丢失率高达 20%,现在接近 5%。

img = cv2.imread(filename)
img = cv2.resize(img, None, fx=1.2, fy=1.2, interpolation=cv2.INTER_CUBIC)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
kernel = np.ones((1, 1), np.uint8)
img = cv2.dilate(img, kernel, iterations=1)
img = cv2.erode(img, kernel, iterations=1)
Run Code Online (Sandbox Code Playgroud)

在此预处理之后,我还运行了一种算法,从文档中删除超过一定大小的实线,但在这种情况下,“35”或“381”在原始文件中都没有下划线,所以我怀疑这是导致问题的原因。我还验证了 5 的顶部部分没有被线条检测算法删除。

我不是 OCR 或 CV 方面的专家,我的专长是更多数据和通用编程——我真的只需要让这个库完成它所宣传的工作,这样我就可以继续并完成程序。有谁知道可能导致此问题的原因是什么?

小智 5

我建议考虑将您的配置设置为特定的页面分段方法 (PSM),例如 11,因为您正在寻找稀疏文本。例如,我的代码:

results = pytesseract.image_to_data(Image.open(tempFile), lang='eng', config='--psm 11', output_type=Output.DICT)       
Run Code Online (Sandbox Code Playgroud)

PSM 如下:

  0    Orientation and script detection (OSD) only.
  1    Automatic page segmentation with OSD.
  2    Automatic page segmentation, but no OSD, or OCR.
  3    Fully automatic page segmentation, but no OSD. (Default)
  4    Assume a single column of text of variable sizes.
  5    Assume a single uniform block of vertically aligned text.
  6    Assume a single uniform block of text.
  7    Treat the image as a single text line.
  8    Treat the image as a single word.
  9    Treat the image as a single word in a circle.
 10    Treat the image as a single character.
 11    Sparse text. Find as much text as possible in no particular order.
 12    Sparse text with OSD.
 13    Raw line. Treat the image as a single text line,
                        bypassing hacks that are Tesseract-specific.
Run Code Online (Sandbox Code Playgroud)

还有一种通过数字而不是文本进行搜索的方法也可能有所帮助。