简体   繁体   English

ASP.NET MVC中的Tesseract OCR

[英]Tesseract OCR in ASP.NET MVC

I am trying to implement Tesseract OCR in my project. 我正在尝试在我的项目中实施Tesseract OCR。 I've got two projects in my solution: 我的解决方案中有两个项目:

  • Portal - which is ASP.NET MVC App 门户-ASP.NET MVC应用程序
  • OCR - which is a class library, where I have Tesseract OCR-这是一个类库,我在这里拥有Tesseract

In OCR class library, I have method: 在OCR类库中,我有方法:

using (var ocrEngine = new TesseractEngine(/*WHAT DOES COME HERE?*/, "eng", EngineMode.Default))
            {

                using (var pix = PixConverter.ToPix(new Bitmap(imageFilePath)))
                {
                    using (var page = ocrEngine.Process(pix))
                    {
                        output = page.GetText();
                    }
                }
            }

In my project I have folder tessdata , but I don't know how to reference it, to make everything work. 在我的项目中,我有tessdata文件夹,但我不知道如何引用它,以使一切正常。 I tried: 我试过了:

@"./tessdata"

or 要么

System.Web.Hosting.HostingEnvironment.MapPath("~/tessdata")

but none of them worked. 但他们都不起作用。

I use Server.MapPath and the demo works fine. 我使用Server.MapPath ,演示正常。

    using (var engine = new TesseractEngine(Server.MapPath(@"~/tessdata"), "eng", EngineMode.Default))
    {
        // have to load Pix via a bitmap since Pix doesn't support loading a stream.
        using (var image = new System.Drawing.Bitmap(file.InputStream))
        {
            using (var pix = PixConverter.ToPix(image))
            {

                using (var page = engine.Process(pix))
                {

                }
            }
        }
    }

You can view the full source code here 您可以在此处查看完整的源代码

You can use below approach: 您可以使用以下方法:

Path.Combine(HttpRuntime.AppDomainAppPath,"tessdata")

like below : 像下面这样:

using (var engine = new TesseractEngine(Path.Combine(HttpRuntime.AppDomainAppPath,"tessdata"), "eng", EngineMode.Default))
    {
        // have to load Pix via a bitmap since Pix doesn't support loading a stream.
        using (var image = new System.Drawing.Bitmap(file.InputStream))
        {
            using (var pix = PixConverter.ToPix(image))
            {

                using (var page = engine.Process(pix))
                {

                }
            }
        }
    }

Hope it helps you 希望对您有帮助

我所做的只是将tessdata文件夹移动到主ASP.NET MVC项目,然后按照@Sunil Kumar的建议使用简单的Path.Combine(HttpRuntime.AppDomainAppPath,"tessdata")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM