【问题标题】:OCR with tesseract in R Fails to Recognize all Line BreaksR中带有tesseract的OCR无法识别所有换行符
【发布时间】:2019-11-25 07:01:02
【问题描述】:

我正在尝试将许多 PDF 文档转换为 R 中的文本,以便使用字符串解析和正则表达式从中提取一组代码。我正在使用来自tesseract 库的ocr,虽然它适用于许多页面,但它确实遗漏了很多我需要的信息。

我发现问题是图像/PDF 中的换行符不一致。例如:

我正在尝试从左栏中获取代码。我能够成功提取的唯一代码是描述超过一行的代码。

我已经使用magick 尝试了各种pre-processing 技术,但在大多数情况下都失败了。我能够获得代码集的唯一实例是从图像中裁剪右侧,但不幸的是,在我的情况下这不是一个有效的解决方案。

file <- magick::image_read("44F245A2-5FEE-408F-A197-756436A5CAFD.png")

file %>%
  magick::image_resize("2000x") %>%
  magick::image_convert(type = 'Grayscale') %>%
  tesseract::ocr() %>%
  cat()

# or
# descriptions in this document.
# 94942C This is a description that takes on multiple lines. It can contain any combination of
# alphanumeric characters or punctuation. Different types of things can go in here and the
# | terpenes Steet gine see
# 272144 This is a description that takes on multiple lines. It can contain any combination of
# eee
# length of the description could be anywhere from 1 line to 5 lines of text.
# E76744 This is a description that takes on multiple lines. It can contain any combination of
# alphanumeric characters or punctuation. Different types of things can go in here and the
# [terpenes Steet gine see
# K77744 This is a description that takes on multiple lines. It can contain any combination of
# alphanumeric characters or punctuation. Different types of things can go in here and the
# | terrane een Steet gine seem
# 172744 This is a description that takes on multiple lines. It can contain any combination of
# Se
# length of the description could be anywhere from 1 line to 5 lines of text.
# A71744 This is a description that takes on multiple lines. It can contain any combination of
# alphanumeric characters or punctuation. Different types of things can go in here and the
# | teammates Steet gine see

理想情况下,我希望能够从上述链接中的图像中获取所有代码。任何帮助都会很棒。

【问题讨论】:

  • 问题是文字有下划线。 Tesseract 难以检测带有下划线的文本行。也许您可以尝试使用 Leptonica 或类似的东西来检测和删除取消划线。

标签: r image-processing imagemagick ocr tesseract


【解决方案1】:

尝试使用不同的页面分割模式,可用的分割模式有:

Page segmentation modes:
  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    Tre at 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,

根据我的经验尝试 PSM #4,根据我的经验,#12 给出的文本最多,但它可能不是按顺序排列的,如果你想将代码与描述相关联,这可能是个问题。

【讨论】:

    猜你喜欢
    • 2015-01-13
    • 2017-08-27
    • 1970-01-01
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多