classname13 classname13 - 2 months ago 21
C# Question

c# generate hocr file using charlesw tesseract

how can i generate hocr using the tesseract wrapper here

currently i need to dynamically add the location of the tessdata to the environment variables and run my code

System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.FileName = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + @"\tesseract-3.05.00dev-win32-vc19\tesseract.exe";
string inputImg = @"00067.jpg";
string hocrLocation = @"00067";
string argsPdf = "\"" + inputImg + "\"" + " " + "\"" + hocrLocation + "\"" + " hocr ";
Console.WriteLine(argsPdf);
pProcess.StartInfo.Arguments = argsPdf;
pProcess.StartInfo.CreateNoWindow = false;
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.Start();
string strOutput = pProcess.StandardOutput.ReadToEnd();
Console.WriteLine("OUtput: " + strOutput);
pProcess.WaitForExit();


and then i found the tesseract wrapper. how can i generate an hocr file using the wrapper? i cant find an example how to do it.

this is the current code(from the example ) im using but how to output an hocr file?

var testImagePath = "./phototest.tif";
if (args.Length > 0)
{
testImagePath = args[0];
}

try
{
using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default))
{
using (var img = Pix.LoadFromFile(testImagePath))
{
using (var page = engine.Process(img))
{

}
}
}
}
catch (Exception e)
{
Trace.TraceError(e.ToString());
Console.WriteLine("Unexpected Error: " + e.Message);
Console.WriteLine("Details: ");
Console.WriteLine(e.ToString());
}

Answer

string hocrText = page.GetHOCRText(pageNum - 1);