.NET中两种OCR方式对比

1.使用 Tesseract 
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。好处就是免费。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值