【问题标题】:Visual Studio x86 tools, normal cmd.exe, Process.Start()Visual Studio x86 工具,普通 cmd.exe,Process.Start()
【发布时间】:2013-11-02 19:13:12
【问题描述】:

我需要编写一些代码来在 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。我 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 位。

【问题讨论】:

    标签: c# visual-studio command tesseract


    【解决方案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();
    

    【讨论】:

    • 谢谢。这只是第二天,它现在可以工作了。我想这是因为我重新启动了我的电脑。可能是缓存问题。
    • 我需要帮助,但我无法发布问题。
    猜你喜欢
    • 2021-12-28
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 2019-07-14
    • 1970-01-01
    相关资源
    最近更新 更多