Class CommandOcrEngine
An IOcrEngine that runs an OCR program for each image and reads the hOCR or ALTO it writes, such as
tesseract {image} {output} -l {langs} --dpi {dpi} hocr. Egami ships no engine and links none: the program is yours.
public sealed class CommandOcrEngine : IOcrEngine
- Inheritance
-
CommandOcrEngine
- Implements
- Inherited Members
Examples
// Any OCR program that writes hOCR or ALTO is an engine. No shell runs the command, and each run has its own temp folder.
CommandOcrEngine tesseract = new("tesseract {image} {output} -l {langs} --dpi {dpi} hocr", new CommandOcrOptions
{
Languages = ["eng"],
Timeout = TimeSpan.FromMinutes(2),
});
try
{
OcrResult result = await tesseract.RecognizeAsync(new OcrRequest(page), Ct);
Console.WriteLine(result.Text);
}
catch (OcrEngineException e)
{
Console.WriteLine($"{e.Message} {e.ErrorOutput}");
}
Remarks
The command is split into words at spaces, with double or single quotes around a word that holds spaces; no shell runs it, so
file names and languages can never become commands. Each word may hold these placeholders: {image}, the path of the
image as a PNG file; {output}, a path (without extension) for the program's output file; {langs}, the request's
languages joined by +; {dpi}, the resolution as a whole number; {page}, the page index. Write {{ and
}} for literal braces.
With {output} in the command, the engine reads that file, or the one file the program wrote beside it with an extension
added (Tesseract adds .hocr or .xml); without it, the engine reads the program's standard output. Each run gets a
temporary folder of its own that is deleted afterwards, so runs may overlap.
Constructors
CommandOcrEngine(string, CommandOcrOptions?)
Initializes a new instance of the CommandOcrEngine class.
public CommandOcrEngine(string command, CommandOcrOptions? options = null)
Parameters
commandstringThe program and its arguments, with placeholders, such as
tesseract {image} - -l {langs} hocr.optionsCommandOcrOptions
Examples
// Any OCR program that writes hOCR or ALTO is an engine. No shell runs the command, and each run has its own temp folder.
CommandOcrEngine tesseract = new("tesseract {image} {output} -l {langs} --dpi {dpi} hocr", new CommandOcrOptions
{
Languages = ["eng"],
Timeout = TimeSpan.FromMinutes(2),
});
try
{
OcrResult result = await tesseract.RecognizeAsync(new OcrRequest(page), Ct);
Console.WriteLine(result.Text);
}
catch (OcrEngineException e)
{
Console.WriteLine($"{e.Message} {e.ErrorOutput}");
}
Exceptions
- ArgumentNullException
commandis null.- ArgumentException
The command is empty, has an unclosed quote, or names an unknown placeholder.
Properties
Info
Gets what the engine can do: its name, languages, largest image and whether calls may overlap.
public OcrEngineInfo Info { get; }
Property Value
Methods
RecognizeAsync(OcrRequest, CancellationToken)
Runs the program on the request's image and reads what it writes.
public Task<OcrResult> RecognizeAsync(OcrRequest request, CancellationToken cancellationToken = default)
Parameters
requestOcrRequestThe image and how to read it.
cancellationTokenCancellationTokenCancels the run; the program is stopped.
Returns
- Task<OcrResult>
The first page the program wrote, with ImageSize set to the image's size if the output gives none.
Exceptions
- ArgumentNullException
requestis null.- ArgumentException
The command uses
{langs}and neither the request nor the options name a language.- OcrEngineException
The program could not start, failed, timed out or wrote no hOCR or ALTO.
- OperationCanceledException
cancellationTokenwas canceled.