Tesseract OCR简单的例子

嗨,你能不能给我一个简单的例子来测试Tesseract OCR,最好是在C#中。
我试过这里的演示。 我下载了英文数据集并在C盘中解压缩。 并修改代码如下:

string path = @"C:\pic\mytext.jpg"; Bitmap image = new Bitmap(path); Tesseract ocr = new Tesseract(); ocr.SetVariable("tessedit_char_whitelist", "0123456789"); // If digit only ocr.Init(@"C:\tessdata\", "eng", false); // To use correct tessdata List result = ocr.DoOCR(image, Rectangle.Empty); foreach (tessnet2.Word word in result) Console.WriteLine("{0} : {1}", word.Confidence, word.Text); 

不幸的是,代码不起作用。 程序死于“ocr.Init(…”行。即使使用try-catch,我甚至无法获得exception。

我能够运行vietocr ! 但这对我来说是一个非常大的项目。 我需要一个像上面这样的简单例子。

谢谢

好。 我发现这里的解决方案tessnet2无法加载 Adam给出的Ans

显然我使用的是错误的tessdata版本。 我直观地遵循源页面指令并导致了问题。

它说

快速使用Tessnet2

  1. 在这里下载二进制文件 ,将程序集Tessnet2.dll的引用添加到您的.NET项目中。

  2. 在此下载语言数据定义文件并将其放在tessdata目录中。 Tessdata目录和您的exe必须位于同一目录中。

下载二进制文件后,当您按照链接下载语言文件时,有许多语言文件。 但它们都不是正确的版本。 您需要选择所有版本并转到下一页以获取正确的版本(tesseract-2.00.eng)! 他们应该将下载二进制链接更新到版本3,或者将版本2语言文件放在第一页上。 或者至少大胆提一下这个版本问题很重要的事实!

无论如何我找到了它。 感谢大家。

在C#中测试Tesseract OCR的一个简单示例:

  public static string GetText(Bitmap imgsource) { var ocrtext = string.Empty; using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default)) { using (var img = PixConverter.ToPix(imgsource)) { using (var page = engine.Process(img)) { ocrtext = page.GetText(); } } } return ocrtext; } 

信息: tessdata文件夹必须存在于存储库中:bin \ Debug \

尝试将该行更新为:

ocr.Init(@“C:\”,“eng”,false); //这里的路径应该是tessdata的父文件夹

我有同样的问题,现在已经解决了。 我有tesseract2,在32位和64位的这个文件夹下,我将文件64位文件夹(因为我的系统是64位)复制到主文件夹(“Tesseract2”)和bin / Debug文件夹下。 现在我的解决方案正常。

就我而言,除了正确的字符识别外,我所有这些都有效。

但是你需要考虑以下几点:

  • 使用正确的tessnet2库
  • 使用正确的tessdata语言版本
  • tessdata应该位于应用程序文件夹之外的某个位置,您可以在init参数中放入完整路径。 使用ocr.Init(@"c:\tessdata", "eng", true);
  • 调试会让你头疼。 然后你需要更新你的app.config使用它。 (我不能把xml代码放在这里。给我你的电子邮件,我会把它发给你)

希望这会有所帮助

这是一个很好的工作示例项目; Tesptract OCR样品(Visual Studio)与Leptonica预处理 Tesseract OCR样品(Visual Studio)与Leptonica预处理

Tesseract OCR 3.02.02 API可能令人困惑,因此这将指导您将Tesseract和Leptonica dll包含在Visual Studio C ++项目中,并提供一个示例文件,该文件采用图像路径进行预处理和OCR。 Leptonica中的预处理脚本将输入图像转换为黑白书本文本。

建立

要将它包含在您自己的项目中,您需要引用头文件和lib并复制tessdata文件夹和dll。

将tesseract-include文件夹复制到项目的根文件夹。 现在在Visual Studio Solution Explorer中单击您的项目,然后转到Project> Properties。

VC ++目录>包含目录:

.. \ tesseract-include \ tesseract; .. \ tesseract-include \ leptonica; $(IncludePath)C / C ++>预处理器>预处理器定义:

_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)C / C ++>链接器>输入>附加依赖项:

.. \ tesseract-include \ libtesseract302.lib; .. \ tesseract-include \ liblept168.lib;%(AdditionalDependencies)现在,您可以在项目的文件中包含标题:

包括

包括

现在将tesseract-include中的两个dll文件和Debug中的tessdata文件夹复制到项目的输出目录中。

初始化tesseract时,如果tessdata文件夹的父文件夹(!important)不是可执行文件的当前目录,则需要指定该文件夹的位置。 您可以复制我的脚本,假定tessdata安装在可执行文件的文件夹中。

tesseract :: TessBaseAPI * api = new tesseract :: TessBaseAPI(); api-> Init(“D:\ tessdataParentFolder \”,……样本

您可以编译提供的示例,该示例使用图像路径的一个命令行参数。 preprocess()函数使用Leptonica创建一个黑白图书的图像副本,使tesseract的工作准确率达到90%。 ocr()函数显示Tesseract API返回字符串输出的function。 toClipboard()可用于将文本保存到Windows上的剪贴板。 您可以将这些复制到您自己的项目中。

按照这些说明,我能够让它工作。

  • 下载示例代码 Tesseract示例代码

  • 将其解压缩到新位置

  • 打开〜\ tesseract-samples-master \ src \ Tesseract.Samples.sln(我使用Visual Studio 2017)

  • 为该项目安装Tesseract NuGet包(或者我必须卸载/重新安装) NuGet Tesseract

  • 在Tesseract.Samples.Program.cs中取消注释最后两行有意义的行: Console.Write("Press any key to continue . . . "); Console.ReadKey(true); Console.Write("Press any key to continue . . . "); Console.ReadKey(true);

  • 跑(点击F5)

  • 你应该得到这个Windows控制台输出 在此处输入图像描述

这对我有用,我有3-4个PDF到文本提取器,如果一个不起作用,另一个将…特别是tesseract这个代码可用于Windows 7,8,Server 2008。 希望这对你有所帮助

  do { // Sleep or Pause the Thread for 1 sec, if service is running too fast... Thread.Sleep(millisecondsTimeout: 1000); Guid tempGuid = ToSeqGuid(); string newFileName = tempGuid.ToString().Split('-')[0]; string outputFileName = appPath + "\\pdf2png\\" + fileNameithoutExtension + "-" + newFileName + ".png"; extractor.SaveCurrentImageToFile(outputFileName, ImageFormat.Png); // Create text file here using Tesseract foreach (var file in Directory.GetFiles(appPath + "\\pdf2png")) { try { var pngFileName = Path.GetFileNameWithoutExtension(file); string[] myArguments = { "/C tesseract ", file, " " + appPath + "\\png2text\\" + pngFileName }; // /C for closing process automatically whent completes string strParam = String.Join(" ", myArguments); var myCmdProcess = new Process(); var theProcess = new ProcessStartInfo("cmd.exe", strParam) { CreateNoWindow = true, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, WindowStyle = ProcessWindowStyle.Minimized }; // Keep the cmd.exe window minimized myCmdProcess.StartInfo = theProcess; myCmdProcess.Exited += myCmdProcess_Exited; myCmdProcess.Start(); //if (process) { /* MessageBox.Show("cmd.exe process started: " + Environment.NewLine + "Process Name: " + myCmdProcess.ProcessName + Environment.NewLine + " Process Id: " + myCmdProcess.Id + Environment.NewLine + "process.Handle: " + myCmdProcess.Handle); */ Process.EnterDebugMode(); //ShowWindow(hWnd: process.Handle, nCmdShow: 2); /* MessageBox.Show("After EnterDebugMode() cmd.exe process Exited: " + Environment.NewLine + "Process Name: " + myCmdProcess.ProcessName + Environment.NewLine + " Process Id: " + myCmdProcess.Id + Environment.NewLine + "process.Handle: " + myCmdProcess.Handle); */ myCmdProcess.WaitForExit(60000); /* MessageBox.Show("After WaitForExit() cmd.exe process Exited: " + Environment.NewLine + "Process Name: " + myCmdProcess.ProcessName + Environment.NewLine + " Process Id: " + myCmdProcess.Id + Environment.NewLine + "process.Handle: " + myCmdProcess.Handle); */ myCmdProcess.Refresh(); Process.LeaveDebugMode(); //myCmdProcess.Dispose(); /* MessageBox.Show("After LeaveDebugMode() cmd.exe process Exited: " + Environment.NewLine); */ } //process.Kill(); // Waits for the process to complete task and exites automatically Thread.Sleep(millisecondsTimeout: 1000); // This works fine in Windows 7 Environment, and not in Windows 8 // Try following code block // Check, if process is not comletey exited if (!myCmdProcess.HasExited) { //process.WaitForExit(2000); // Try to wait for exit 2 more seconds /* MessageBox.Show(" Process of cmd.exe was exited by WaitForExit(); Method " + Environment.NewLine); */ try { // If not, then Kill the process myCmdProcess.Kill(); //myCmdProcess.Dispose(); //if (!myCmdProcess.HasExited) //{ // myCmdProcess.Kill(); //} MessageBox.Show(" Process of cmd.exe exited ( Killed ) successfully " + Environment.NewLine); } catch (System.ComponentModel.Win32Exception ex) { MessageBox.Show( " Exception: System.ComponentModel.Win32Exception " + ex.ErrorCode + Environment.NewLine); } catch (NotSupportedException notSupporEx) { MessageBox.Show(" Exception: NotSupportedException " + notSupporEx.Message + Environment.NewLine); } catch (InvalidOperationException invalidOperation) { MessageBox.Show( " Exception: InvalidOperationException " + invalidOperation.Message + Environment.NewLine); foreach ( var textFile in Directory.GetFiles(appPath + "\\png2text", "*.txt", SearchOption.AllDirectories)) { loggingInfo += textFile + " In Reading Text from generated text file by Tesseract " + Environment.NewLine; strBldr.Append(File.ReadAllText(textFile)); } // Delete text file after reading text here Directory.GetFiles(appPath + "\\pdf2png").ToList().ForEach(File.Delete); Directory.GetFiles(appPath + "\\png2text").ToList().ForEach(File.Delete); } } } catch (Exception exception) { MessageBox.Show( " Cought Exception in Generating image do{...}while{...} function " + Environment.NewLine + exception.Message + Environment.NewLine); } } // Delete png image here Directory.GetFiles(appPath + "\\pdf2png").ToList().ForEach(File.Delete); Thread.Sleep(millisecondsTimeout: 1000); // Read text from text file here foreach (var textFile in Directory.GetFiles(appPath + "\\png2text", "*.txt", SearchOption.AllDirectories)) { loggingInfo += textFile + " In Reading Text from generated text file by Tesseract " + Environment.NewLine; strBldr.Append(File.ReadAllText(textFile)); } // Delete text file after reading text here Directory.GetFiles(appPath + "\\png2text").ToList().ForEach(File.Delete); } while (extractor.GetNextImage()); // Advance image enumeration...