5
$\begingroup$

Hi I would like to use the terminal code OCR softwar, called: Tesseract Optical Character Recognition (OCR)

Here you have a nice description of how to set it up: http://chillyfacts.com/convert-image-to-text-using-cmd-prompt/

Now my question is:

When I type: "C:\OCR\tesseract" "C:\Image_to_OCR.jpg" "C:\out" in the cmd command line, it works...

But when I try to run it from Mathematica:

Run["\"C:\\OCR\\tesseract\" \"C:\\Image_to_OCR.jpg \" \"C:\\out\""]

it does not work and returns "1" .

Any idea why or how I can fix it ? Anyone ?

$\endgroup$
  • $\begingroup$ Contrary to Run, Import["! <<command line code here>>", "Text"] works for me. Maybe you want to give it a try. Is is also capable of returning the error message and not only the error code (IRRC, you have to append "2>&1" to the command line code for that...) $\endgroup$ – Henrik Schumacher Jul 24 '18 at 12:53
  • $\begingroup$ @HenrikSchumacher Thank you for your comment. Someohow it does not work for me :(. Maybe I am pasting a wrong command line. Can you show me how you typed your command line ? (I have Mathematica 11.3) $\endgroup$ – james Jul 24 '18 at 13:00
  • $\begingroup$ @HenrikSchumacher This is what I typed: Import["!\"C:\\Program Files (x86)\\Tesseract-OCR\\tesseract\" \ \"C:\\Image_to_OCR.jpg \" \"C:\\Users\\James\\Desktop\\out5.txt\"" <> "2>&1", "Text"] $\endgroup$ – james Jul 24 '18 at 13:02
  • $\begingroup$ @HenrikSchumacher I get no error, but no file is created either $\endgroup$ – james Jul 24 '18 at 13:03
  • $\begingroup$ @HenrikSchumacher It still does not work :( I also tryied it Mathematica 11.01 no change $\endgroup$ – james Jul 24 '18 at 14:56
7
+100
$\begingroup$

Files

Let's create some test files

Table[
 Export[
  StringTemplate["Test``.jpg"][k],
  Rasterize@TextCell[
    RandomChoice[DictionaryLookup["*"]],
    RandomChoice[{"Text", "Subsection", "Section", "Title"}]
    ]
  ]
 , {k, 10}
 ]

RunProcess

Test a single file using RunProcess (advisable).

RunProcess[
 {
  "C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe",
  "Test1.jpg",
  "Test1"
  }
 ]
(* <|"ExitCode" -> 0, "StandardOutput" -> "", 
 "StandardError" -> 
  "Tesseract Open Source OCR Engine v3.02 with Leptonica

  "|> *)

Run

Test a single file using Run, notice the extra quotation marks go only around the long path name to protect the spaces. Arguments do not need quotation marks.

Run[
  "\"C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe\" Test1.jpg Test1"
 ]
 (* 0 *)

Processing many files

Table[
 {
  StringTemplate["Test``.jpg"][k],
  RunProcess[
    {
     "C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe",
     StringTemplate["Test``.jpg"][k],
     StringTemplate["Test``"][k]
     }
    ]["ExitCode"]
  }
 , {k, 10}
 ]

TextRecognize

Compare with Mathematica's TextRecognize

TableForm[
 Table[
  {
   StringTemplate["Test``.jpg"][k],
   Import[StringTemplate["Test``.jpg"][k]],
   Import[StringTemplate["Test``.txt"][k], "String"],
   TextRecognize[StringTemplate["Test``.jpg"][k]]
   }
  , {k, 10}
  ]
 , TableHeadings -> {Automatic, {"File Name", "Image", "Tesseract", 
    "Mathematica\nTextRecognize" }}
 ]

Mathematica graphics

$\endgroup$
  • 2
    $\begingroup$ Nice comparison: they are both terrible? $\endgroup$ – chris Aug 9 '18 at 9:21
  • $\begingroup$ Wow, thanks a lot ! This is a really nice answer, and it works now ! :) $\endgroup$ – james Aug 14 '18 at 10:44
  • $\begingroup$ Do you have any suggestion of a terminal-input program, which would deliver better results ? $\endgroup$ – james Aug 14 '18 at 10:45
  • $\begingroup$ @james I can only speculate, but I think the poor performance here is because there is no extra dictionary information and no grammar context. I have no suggestions for better OCR software. $\endgroup$ – rhermans Aug 14 '18 at 10:52
  • $\begingroup$ I see. No worries. Thanks a lot for your answer. $\endgroup$ – james Aug 14 '18 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.