r - 用 R 做 OCR

标签 r shell pdf ocr tesseract

我一直在尝试在 R 中进行 OCR(读取 PDF 数据,其中数据为扫描图像)。一直在阅读这个@ http://electricarchaeology.ca/2014/07/15/doing-ocr-within-r/

这是一个非常好的帖子。

有效的3个步骤:

  • 将 pdf 转换为 ppm(一种图像格式)
  • 将 ppm 转换为 tif 为 tesseract 做好准备(使用 ImageMagick 进行转换)
  • 将 tif 转换为文本文件

  • 根据链接帖子,上述3个步骤的有效代码:
    lapply(myfiles, function(i){
      # convert pdf to ppm (an image format), just pages 1-10 of the PDF
      # but you can change that easily, just remove or edit the 
      # -f 1 -l 10 bit in the line below
      shell(shQuote(paste0("F:/xpdf/bin64/pdftoppm.exe ", i, " -f 1 -l 10 -r 600 ocrbook")))
      # convert ppm to tif ready for tesseract
      shell(shQuote(paste0("F:/ImageMagick-6.9.1-Q16/convert.exe *.ppm ", i, ".tif")))
      # convert tif to text file
      shell(shQuote(paste0("F:/Tesseract-OCR/tesseract.exe ", i, ".tif ", i, " -l eng")))
      # delete tif file
      file.remove(paste0(i, ".tif" ))
      })
    

    前两个步骤进行得很好。 (虽然花了很多时间,对于 pdf 的 4 页,但稍后会研究可扩展性部分,首先尝试是否可行)

    运行此程序时,前两步工作正常。

    在运行第三步时,即
    shell(shQuote(paste0("F:/Tesseract-OCR/tesseract.exe ", i, ".tif ", i, " -l eng")))
    

    我有这个错误:

    Error: evaluation nested too deeply: infinite recursion / options(expressions=)?



    或者 Tesseract 正在崩溃。

    任何解决方法或根本原因分析将不胜感激。

    最佳答案

    通过使用“tesseract”,我创建了一个有效的示例脚本。即使它也适用于扫描的 PDF。

    library(tesseract)
    library(pdftools)
    
    # Render pdf to png image
    
    img_file <- pdftools::pdf_convert("F:/gowtham/A/B/invoice.pdf", format = 'tiff',  dpi = 400)
    
    # Extract text from png image
    text <- ocr(img_file)
    write.table(text, "F:/gowtham/A/B/mydata.txt")
    

    我是 R 和编程的新手。如果错了请指导我。希望这对你有帮助。

    关于r - 用 R 做 OCR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31979857/

    相关文章:

    linux - 如何将源服务器中文件的 md5checksum 与 shell 脚本中的目标服务器进行比较?

    MySQL 清除 shell 脚本

    python-3.x - Python 和 OpenCV

    android - iText 作为文本在 Android 上从 PDF 中提取/读取

    R - 将 ggplot 对象列表更改为 grid.arrange 将接受的 grobs 列表?

    使用数据表按不同顺序对多列进行排名

    r - 修改列表列中的数据框

    python - 批量感叹号变量

    r - 如何在R中将Matlab时间转换为日期?

    python - PLY/YACC解析PDF上的冲突