2017-09-20 2 views
0

OCRをrdでpdfしようとするとエラーが出ます。 コードを実行した後、 "i.txt"ファイルも生成されましたが、それでもエラーは発生しています。rcでocrを実行しているときにエラーが発生しました

pdftoppm version 4.00 
Copyright 1996-2017 Glyph & Cog, LLC 
Usage: pdftoppm [options] <PDF-file> <PPM-root> 
    -f <int>   : first page to print 
    -l <int>   : last page to print 
    -r <number>  : resolution, in DPI (default is 150) 
    -mono    : generate a monochrome PBM file 
    -gray    : generate a grayscale PGM file 
    -freetype <string>: enable FreeType font rasterizer: yes, no 
    -aa <string>  : enable font anti-aliasing: yes, no 
    -aaVector <string>: enable vector anti-aliasing: yes, no 
    -opw <string>  : owner password (for encrypted files) 
    -upw <string>  : user password (for encrypted files) 
    -q    : don't print any messages or errors 
    -cfg <string>  : configuration file to use in place of .xpdfrc 
    -v    : print copyright and version info 
    -h    : print usage information 
    -help    : print usage information 
    --help   : print usage information 
    -?    : print usage information 
convert.exe: unable to open image '*.ppm': Invalid argument @ error/blob.c/OpenBlob/3146. 
convert.exe: no images defined `D:/PDF_OCR_File/test.pdf.tif' @ error/convert.c/ConvertImageCommand/3275. 
Tesseract Open Source OCR Engine v3.05.01 with Leptonica 
Error in fopenReadStream: file not found 
Error in findFileFormat: image file not found 
Error during processing. 
[[1]] 
[1] FALSE 

Warning messages: 
1: running command 'C:\Windows\system32\cmd.exe /c "D:/Software_for_PDF_OCR/xpdf-tools-win-4.00/bin64/pdftoppm.exe D:/PDF_OCR_File/test.pdf -f 1 -l 2 -r 600 ocrbook"' had status 99 
2: In shell(shQuote(paste0("D:/Software_for_PDF_OCR/xpdf-tools-win-4.00/bin64/pdftoppm.exe ", : 
    '"D:/Software_for_PDF_OCR/xpdf-tools-win-4.00/bin64/pdftoppm.exe D:/PDF_OCR_File/test.pdf -f 1 -l 2 -r 600 ocrbook"' execution failed with error code 99 
3: running command 'C:\Windows\system32\cmd.exe /c "D:/Software_for_PDF_OCR/ImageMagick-7.0.7-Q16/convert.exe *.ppm D:/PDF_OCR_File/test.pdf.tif"' had status 1 
4: In shell(shQuote(paste0("D:/Software_for_PDF_OCR/ImageMagick-7.0.7-Q16/convert.exe *.ppm ", : 
    '"D:/Software_for_PDF_OCR/ImageMagick-7.0.7-Q16/convert.exe *.ppm D:/PDF_OCR_File/test.pdf.tif"' execution failed with error code 1 
5: running command 'C:\Windows\system32\cmd.exe /c "D:/Software_for_PDF_OCR/Tesseract-OCR/tesseract.exe D:/PDF_OCR_File/test.pdf.tif D:/PDF_OCR_File/test.pdf -l eng"' had status 1 
6: In shell(shQuote(paste0("D:/Software_for_PDF_OCR/Tesseract-OCR/tesseract.exe ", : 
    '"D:/Software_for_PDF_OCR/Tesseract-OCR/tesseract.exe D:/PDF_OCR_File/test.pdf.tif D:/PDF_OCR_File/test.pdf -l eng"' execution failed with error code 1 
7: In file.remove(paste0(i, ".tiff")) : 
    cannot remove file 'D:/PDF_OCR_File/test.pdf.tiff', reason 'No such file or directory' 

マイsetwdは()この"D:/ PDF_OCR_File" である

これは、私はそれがなっている場合、私は知らないエラー

dest <- "D:/PDF_OCR_File" 
myfiles <- list.files(path = dest, pattern = "pdf", full.names = TRUE) 

sapply(myfiles, FUN = function(i){ 
    file.rename(from = i, to = paste0(dirname(i), "/", gsub(" ", "", basename(i)))) 
}) 


myfiles <- list.files(path = dest, pattern = "pdf", full.names = TRUE) 




lapply(myfiles, function(i){ 

    shell(shQuote(paste0("D:/Software_for_PDF_OCR/xpdf-tools-win-4.00/bin64/pdftoppm.exe ", i, " -f 1 -l 2 -r 600 ocrbook"))) 
    # convert ppm to tif ready for tesseract 
    shell(shQuote(paste0("D:/Software_for_PDF_OCR/ImageMagick-7.0.7-Q16/convert.exe *.ppm ", i, ".tif"))) 
    # convert tif to text file 
    shell(shQuote(paste0("D:/Software_for_PDF_OCR/Tesseract-OCR/tesseract.exe ", i, ".tif ", i, " -l eng"))) 
    # delete tif file 
    file.remove(paste0(i, ".tiff")) 
}) 

を取得した上でコードです間違っている、または間違いを私が作っている。 提案が役に立ちます。 ありがとうございます。

+0

'file.exists(myfiles [[1]])'はあなたに何を与えるのですか? –

+0

> file.exists(myfiles [[1]]) TRUE – deepesh

+0

pdf-ppmコマンドが何とか失敗したように見えるので、次のコマンドは失敗します。ターミナルで最初のコマンドが動作するようにしてください。 OCRの 'magick'パッケージを使うことができます –

答えて

0

あなたのコードにthisを使用していると思いますか? 私はそのコードといくつかの古い構文の問題を発見しました。

私が作ってみた解決策はこれです:

dest <- "C:\\users\\YOURNAME\\desktop" 

    files <- list.files(path = dest, pattern = "pdf", full.names = TRUE) 

    sapply(files, FUN = function(a){ 
    file.rename(from = a, to = paste0(dirname(a), "/", gsub(" ", "", basename(a)))) 
     }) 

     files <- tools::file_path_sans_ext(list.files(path = dest, pattern = "pdf", full.names = TRUE)) 
    lapply(files, function(i){ 
     shell(shQuote(paste0("pdftoppm -f 1 -l 10 -r 70 ", i,".pdf", " ",i))) 
     }) 


    myppms <- tools::file_path_sans_ext(list.files(path = dest, pattern = "ppm", full.names = TRUE)) 
    lapply(myppms, function(y){ 
     shell(shQuote(paste0("magick ", y,".ppm"," ",y,".tif"))) 
     file.remove(paste0(y,".ppm")) 
     }) 

    mytiffs <- tools::file_path_sans_ext(list.files(path = dest, pattern = "tif", full.names = TRUE)) 
    lapply(mytiffs, function(z){ 
     shell(shQuote(paste0("tesseract ", z,".tif", " ",z))) 
     file.remove(paste0(z,".tif")) 
     }) 

GitHubのスニペットとの最初の問題は、オプションの両方が欠けている部分であり、CMDがある、理解するために、間違った場所にあるということですなぜあなたはヘルプメニューを手に入れているのですか? "ocrbook"は出力ファイル名です(複数のファイルを作成したい場合は悪い)ので、 "ocrbook-000001.png"という名前のPPM、PNGを取得します。そのコードブロック内の関数(i)の問題は、 "ocrbook-000001"に変換されたファイル名の代わりに "originalpdfname.pdf.png"を探していることです。私は、関数内で関数を作成してPNGファイルを見つけて(z)に入れることで修正しました。

TesseractはPNGファイルをうまく変換するため、ImageMagickを使用してPPMからTIFFに変換する必要はありません。 xPDFを使ってPDFをPNGに変換するだけです。しかし、GitHubの例では、ImageMagick構文が古くなっています。 「変換」は別のCMDコマンドと衝突するように見えるため、後の反復で「magick」に変更されました。 hereを参照してください。一貫性のために、私はこの例のコンバータをとにかく使用しました。

このコード例のもう一つの点は、tesseractがデフォルトで英語に設定されていることです。これは新しいバージョンで作成されたものかもしれないので、もはや "-l eng"を指定する必要はなくなりました。 hereを参照してください。 "out"はエクスポートされたtxtファイル名です(観測のみから)。パスを削除して関数内で使用して、元のファイル名を模倣し、ファイルを実行するたびに上書きしないようにする必要があります新しいファイルのOCR。

関連する問題