我需要编写一些代码来调用C#中的tesseract OCR。我安装它并使用以下代码。但它不起作用:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = tempDir.FullName;
// doesn't work
startInfo.Arguments = string.Format("/C tesseract {0} {1}", imgName, textName);
// this works
//startInfo.Arguments = string.Format("/C copy {0} {1}", imgName, textName);
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
p.WaitForExit();
p.Close();
不会抛出任何异常或错误。我只是无法获取目录中的输出文件。我尝试了一个内置的命令,如copy
,它评论并且有效。我试图得到进程的标准输出,但它总是抛出“进程退出”异常。
之后我尝试在命令窗口中调用tesseract。我进入临时目录,运行tesseract img.png output
,这里感兴趣的事情发生了:
我在环境变量中检查PATH,这是正确的。我能看到的唯一区别是VS提示符显示“使用Microsoft Visual Studio 2010 x86工具设置环境”。在顶端。它不搜索PATH变量来查找命令吗?他们不是一回事吗?它是否与我的C#代码失败有关?
我的操作系统是Windows Server 2008 64位。
答案 0 :(得分:1)
我使用它的方式如下:
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = "tesseract.exe";
p.StartInfo.Arguments = string.Format("\"{0}\" \"{1}\" -l {2} -psm {3} {4}", imageFile, outputFileName, language, PageSegMode, Hocr ? "hocr" : string.Empty);
p.Start();
p.WaitForExit();
if (p.ExitCode == 0)
{
// read output text file
}
p.Close();