1.使用 Tesseract
Install-Package Tesseract
2.从这里下载实例项目(为了获得训练数据)
tessdata目录下的就是训练数据,等下初始化Tesseract引擎对象会使用。
测试代码:
使用MS cognitive做OCR
1. 有一个Cognitive 的账号。
2. 准备一个测试图片
对比两种OCR:
1. MS Cognitive Service是RESTful api based,支持多种客户端而且不用关心机器学习过程,但是收费(每月5000个call,1分钟20个call免费)。不过可考虑使用缓存来降低成本。
2. Tesseract是dll based。因此需要不断更新训练数据,如果需要支持多客户端,需要自己wrap一个RESTful。好处就是免费。
Install-Package Tesseract
2.从这里下载实例项目(为了获得训练数据)
tessdata目录下的就是训练数据,等下初始化Tesseract引擎对象会使用。
测试代码:
private static void TesseractSample()
{
var testImagePath = "phototest.tif";
try
{
using (var engine = new TesseractEngine(@"..\..\tessdata", "eng", EngineMode.Default))
{
using (var img = Pix.LoadFromFile(testImagePath))
{
using (var page = engine.Process(img))
{
var text = page.GetText();
Console.WriteLine("Mean confidence: {0}", page.GetMeanConfidence());
Console.WriteLine("Text (GetText): \r\n{0}", text);
Console.WriteLine("Text (iterator):");
using (var iter = page.GetIterator())
{
iter.Begin();
do
{
do
{
do
{
do
{
if (iter.IsAtBeginningOf(PageIteratorLevel.Block))
{
Console.WriteLine("<BLOCK>");
}
Console.Write(iter.GetText(PageIteratorLevel.Word));
Console.Write(" ");
if (iter.IsAtFinalOf(PageIteratorLevel.TextLine, PageIteratorLevel.Word))
{
Console.WriteLine();
}
} while (iter.Next(PageIteratorLevel.TextLine, PageIteratorLevel.Word));
if (iter.IsAtFinalOf(PageIteratorLevel.Para, PageIteratorLevel.TextLine))
{
Console.WriteLine();
}
} while (iter.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine));
} while (iter.Next(PageIteratorLevel.Block, PageIteratorLevel.Para));
} while (iter.Next(PageIteratorLevel.Block));
}
}
}
}
}
catch (Exception e)
{
Console.WriteLine("Unexpected Error: " + e.Message);
Console.WriteLine("Details: ");
Console.WriteLine(e.ToString());
}
Console.WriteLine("Press anykey to end");
Console.ReadKey();
}
使用MS cognitive做OCR
1. 有一个Cognitive 的账号。
2. 准备一个测试图片
3. 测试代码:
...
MS_OCRApi("phototest.tif");
...
static async void MS_OCRApi(string pathOfImage)
{
var client = new HttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);
const string API_KEY = "{your key}";
// Request headers
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", API_KEY);
// Request parameters
queryString["language"] = "unk";
queryString["detectOrientation "] = "true";
var uri = "https://westus.api.cognitive.microsoft.com/vision/v1.0/ocr?" + queryString;
HttpResponseMessage response;
// Request body
byte[] byteData = File.ReadAllBytes(pathOfImage);
using (var content = new ByteArrayContent(byteData))
{
content.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
response = await client.PostAsync(uri, content);
var s = new MemoryStream();
await response.Content.CopyToAsync(s);
var str = Encoding.UTF8.GetString(s.ToArray());
var results = JsonConvert.DeserializeObject<MsOCRResult>(str);
Console.WriteLine("Results :");
var lines = results.regions.SelectMany(x => x.lines);
foreach (var ocrLine in lines)
{
Console.WriteLine(ocrLine);
}
Console.ReadKey();
}
}
对比两种OCR:
1. MS Cognitive Service是RESTful api based,支持多种客户端而且不用关心机器学习过程,但是收费(每月5000个call,1分钟20个call免费)。不过可考虑使用缓存来降低成本。
2. Tesseract是dll based。因此需要不断更新训练数据,如果需要支持多客户端,需要自己wrap一个RESTful。好处就是免费。