Run Does not Work

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 ?

• 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...) – Henrik Schumacher Jul 24 '18 at 12:53
• @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) – james Jul 24 '18 at 13:00
• @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"] – james Jul 24 '18 at 13:02
• @HenrikSchumacher I get no error, but no file is created either – james Jul 24 '18 at 13:03
• @HenrikSchumacher It still does not work :( I also tryied it Mathematica 11.01 no change – james Jul 24 '18 at 14:56

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" }}
]


• Nice comparison: they are both terrible? – chris Aug 9 '18 at 9:21
• Wow, thanks a lot ! This is a really nice answer, and it works now ! :) – james Aug 14 '18 at 10:44
• Do you have any suggestion of a terminal-input program, which would deliver better results ? – james Aug 14 '18 at 10:45
• @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. – rhermans Aug 14 '18 at 10:52
• I see. No worries. Thanks a lot for your answer. – james Aug 14 '18 at 10:55