2013-10-24 89 views
0

我需要編寫一些代碼來調用C#中的tesseract OCR。我安裝了它並使用了下面的代碼。但它不起作用:Visual Studio x86工具,正常的cmd.exe,Process.Start()

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,它的評論和它的工作原理。我試圖獲取進程的stdout,但它總是拋出「進程退出」異常。

之後,我嘗試在命令窗口中調用tesseract。我cd到臨時目錄,運行tesseract img.png output這裏的利益的事情發生了:

  1. 當開始通過開始 - 命令窗口>運行 - > CMD,它工作正常。
  2. 當在Visual Studio解決方案資源管理器中啓動命令窗口時,右鍵單擊 - >打開命令提示符(這是VS Productivity Power Tools的功能),它顯示「tesseract不被識別爲內部或外部命令」。

我檢查環境變量中的PATH,它是正確的。我能看到的唯一區別是VS提示符顯示「設置使用Microsoft Visual Studio 2010 x86工具的環境」。在頂端。它不搜索PATH變量來查找命令嗎?他們不是一回事嗎?它與我的C#代碼的失敗有什麼關係?

我的操作系統是Windows Server 2008 64位。

回答

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(); 
+0

感謝。這只是第二天,現在起作用。我認爲這是因爲我重新啓動了我的電腦。也許這是一個緩存問題。 –

相關問題