无法使用 tessnet2 和 Tesseract-OCR 从图像中读取文本
Unable to read the text from an image using tessnet2 and Tesseract-OCR
我已经编写了 below.Net 代码来从图像中读取文本:
用于编写代码的平台:
Windows 10,Visual Studio 2015,tesseract-ocr-setup-4.00.00dev 和 tessnet2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using tessnet2;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var image = new Bitmap(@"D:\Python\download.jpg");
var ocr = new Tesseract();
ocr.Init(@"C:\Program Files (x86)\Tesseract-OCR\tessdata", "eng",false);
var result = ocr.DoOCR(image, Rectangle.Empty);
foreach (tessnet2.Word word in result)
{
Console.WriteLine(word.Text);
File.AppendAllText(@"D:\Python\writefile.txt",word.Text);
}
Console.ReadLine();
}
}
}
我都尝试过 "Any CPU" 和 X86 的 CPU。也尝试从项目属性更改目标框架版本。
但是,我遇到以下错误:
An unhandled exception of type 'System.IO.FileLoadException' occurred in
mscorlib.dll
Additional information: Mixed mode assembly is built against version
'v2.0.50727'
of the runtime and cannot be loaded in the 4.0 runtime without additional
configuration information.
编辑:
刚刚在我的 app.config 中写了这个以消除错误,现在看起来如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
参考这个安装了NuGet:https://www.nuget.org/packages/NuGet.Tessnet2/
我无法阅读图像。我从 Google 图片之一下载的图片,里面有文字。
这是我收到的消息:
当我检查路径 C:\Program Files (x86)\Tesseract-OCR\tessdata
这是它的样子:
我做错了什么?如何解决这个问题?
问题已解决:从此处下载 LANG 包:https://github.com/tesseract-ocr/langdata
缺少什么 previously.The Tessnet2 工作最重要的事情是获取语言包,在这里 (https://github.com/tesseract-ocr/langdata) 获取您想要的语言。对于示例,我使用英语。
下载语言并将其解压缩到“..\Tesseract-OCR\tessdata”文件夹。
注意:貌似默认安装时语言包是不会进入tessdata的。
这是我修改后的代码版本:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using tessnet2;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var image = new Bitmap(@"D:\Python\download.jpg");
tessnet2.Tesseract ocr = new tessnet2.Tesseract();
ocr.Init(@"C:\Program Files (x86)\Tesseract-OCR\tessdata", "eng",false);
List<tessnet2.Word> result = ocr.DoOCR(image, Rectangle.Empty);
foreach (tessnet2.Word word in result)
{
Console.WriteLine("{0} : {1}",word.Confidence,word.Text);
}
Console.Read();
}
}
}
干杯!!!
我已经编写了 below.Net 代码来从图像中读取文本:
用于编写代码的平台: Windows 10,Visual Studio 2015,tesseract-ocr-setup-4.00.00dev 和 tessnet2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using tessnet2;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var image = new Bitmap(@"D:\Python\download.jpg");
var ocr = new Tesseract();
ocr.Init(@"C:\Program Files (x86)\Tesseract-OCR\tessdata", "eng",false);
var result = ocr.DoOCR(image, Rectangle.Empty);
foreach (tessnet2.Word word in result)
{
Console.WriteLine(word.Text);
File.AppendAllText(@"D:\Python\writefile.txt",word.Text);
}
Console.ReadLine();
}
}
}
我都尝试过 "Any CPU" 和 X86 的 CPU。也尝试从项目属性更改目标框架版本。
但是,我遇到以下错误:
An unhandled exception of type 'System.IO.FileLoadException' occurred in
mscorlib.dll
Additional information: Mixed mode assembly is built against version
'v2.0.50727'
of the runtime and cannot be loaded in the 4.0 runtime without additional
configuration information.
编辑: 刚刚在我的 app.config 中写了这个以消除错误,现在看起来如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
参考这个安装了NuGet:https://www.nuget.org/packages/NuGet.Tessnet2/
我无法阅读图像。我从 Google 图片之一下载的图片,里面有文字。
这是我收到的消息:
当我检查路径 C:\Program Files (x86)\Tesseract-OCR\tessdata
这是它的样子:
我做错了什么?如何解决这个问题?
问题已解决:从此处下载 LANG 包:https://github.com/tesseract-ocr/langdata
缺少什么 previously.The Tessnet2 工作最重要的事情是获取语言包,在这里 (https://github.com/tesseract-ocr/langdata) 获取您想要的语言。对于示例,我使用英语。
下载语言并将其解压缩到“..\Tesseract-OCR\tessdata”文件夹。
注意:貌似默认安装时语言包是不会进入tessdata的。
这是我修改后的代码版本:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using tessnet2;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var image = new Bitmap(@"D:\Python\download.jpg");
tessnet2.Tesseract ocr = new tessnet2.Tesseract();
ocr.Init(@"C:\Program Files (x86)\Tesseract-OCR\tessdata", "eng",false);
List<tessnet2.Word> result = ocr.DoOCR(image, Rectangle.Empty);
foreach (tessnet2.Word word in result)
{
Console.WriteLine("{0} : {1}",word.Confidence,word.Text);
}
Console.Read();
}
}
}
干杯!!!