简体   繁体   English

如何在 ASP.Net MVC4 Web API 项目中使用 Microsoft OCR 库 (Microsoft.Windows.Ocr)?

[英]How to use Microsoft OCR Library ( Microsoft.Windows.Ocr ) in an ASP.Net MVC4 Web API Project?

TL;DR:特尔;博士:

Does anyone know of a way to reference Microsoft.Windows.Ocr (/ WindowsPreview.Media.Ocr.dll ) Assembly on a server-side ASP.Net Web application like MV4 Web API and make use of the OCR Functionality in that assembly to take a photo image as input and extract the text content out of it ?有谁知道在服务器端 ASP.Net Web 应用程序(如 MV4 Web API)上引用Microsoft.Windows.Ocr ( WindowsPreview.Media.Ocr.dll ) 程序集的方法,并利用该程序集中的 OCR 功能来获取将照片图像作为输入并从中提取文本内容? If yes, please provide detailed instructions in your answer.如果是,请在回答中提供详细说明。

Question Details (and what I have tried so far)问题详细信息(以及我迄今为止尝试过的内容)

I am building a web application that takes an image uploaded to the Server (via a file upload UI screen) and then reads the text using OCR and displays the text on the next page, right next to the image that was uploaded.我正在构建一个 Web 应用程序,它将图像上传到服务器(通过文件上传 UI 屏幕),然后使用OCR读取文本并在下一页上显示文本,就在上传的图像旁边。

Since most commercial OCR Libraries cost an arm and length ( over $1,300 last time I checked ) I thought I can try and use the Microsoft OCR Library Microsoft.Windows.Ocr that is FREE and seems to be very simple and straightforward to use.由于大多数商业OCR库成本一只胳膊和长度( 超过$ 1,300个我最后一次检查),我想我可以尝试使用Microsoft OCR库Microsoft.Windows.Ocr免费的,似乎是非常简单和容易使用。

So I tried to install the Microsoft.Windows.Ocr Nuget Package to my ASP.Net MVC4 Web API Project and that succeeded.所以我尝试将Microsoft.Windows.Ocr Nuget 包安装到我的 ASP.Net MVC4 Web API 项目中,并且成功了。

安装 Microsoft.Windows.Ocr Nuget 包

After that, I looked through my MVC4 Web API Project References, and to my surprise, did not find a reference to Microsoft.Windows.Ocr.dll Assembly.之后,我查看了我的 MVC4 Web API 项目引用,令我惊讶的是,没有找到对Microsoft.Windows.Ocr.dll程序集的引用。

缺少对 Microsoft.Windows.Ocr.dll 的引用

So then I tried to add a reference to the x86 version of the Microsoft.Windows.Ocr.dll Assembly by browsing to that Assembly in the \\packages folder, and selected the WindowsPreview.Media.Ocr.dll from \\lib\\win81\\x86 folder然后我尝试通过浏览到\\packages文件夹中的该\\packages来添加对 Microsoft.Windows.Ocr.dll 程序集的 x86 版本的引用,并从\\lib\\win81\\x86选择了WindowsPreview.Media.Ocr.dll文件夹

Note: The Assembly name is WindowsPreview.Media.Ocr.dll and not Microsoft.Windows.Ocr.dll , not sure why!注意:程序集名称是 WindowsPreview.Media.Ocr.dll 而不是 Microsoft.Windows.Ocr.dll ,不知道为什么!

浏览并选择 Microsoft.Windows.Ocr.dll OCR 程序集

When I did that and clicked OK , I got the following Error Message .当我这样做并单击“确定”时,我收到以下错误消息

---------------------------
Microsoft Visual Studio
---------------------------
A reference to   
'D:\TestProjects\packages\Microsoft.Windows.Ocr.1.0.0\lib\win81\x86\
 WindowsPreview.Media.Ocr.dll' could not be added. Please make sure 
 that the file is accessible, and that it is a valid assembly 
 or COM component.
---------------------------
OK   
---------------------------

Microsoft OCR 库添加到项目时出错

I then found out from the Nuget Page that the "Supported Platforms" are only Windows Phone 8, Windows Phone 8.1, Windows 8.1 (Windows Store apps only).然后我从Nuget 页面发现“支持的平台”只有Windows Phone 8、Windows Phone 8.1、Windows 8.1(仅限 Windows 应用商店应用)。

微软 ocr 库 nuget 错误

But surely, there must be a way to use this OCR dll on the Server-side in an ASP.Net Application?但是可以肯定,必须有一种方法可以在 ASP.Net 应用程序中的服务器端使用此OCR dll

Does anyone know of a way to reference Microsoft.Windows.Ocr (/ WindowsPreview.Media.Ocr.dll ) Assembly on a server-side ASP.Net Web application like MV4 Web API and make use of the OCR Functionality in that assembly to take a photo image as input and extract the text content out of it ?** If yes, please provide detailed instructions in your answer.有谁知道在服务器端 ASP.Net Web 应用程序(如 MV4 Web API)上引用Microsoft.Windows.Ocr ( WindowsPreview.Media.Ocr.dll ) 程序集的方法,并利用该程序集中的 OCR 功能来获取将照片图像作为输入并从中提取文本内容?**如果是,请在您的答案中提供详细说明。

Any "hacks" and/or Sample code would be much appreciated!!任何“黑客”和/或示例代码将不胜感激!

Thank you!!谢谢!!

If you are using Visual Studio 2015 and Windows 10, the如果您使用的是 Visual Studio 2015 和 Windows 10,则

Microsoft.Windows.Ocr微软.Windows.Ocr

has been moved to Universal Windows Platform.已移至通用 Windows 平台。 It is available as它可以作为

Windows.Media.Ocr视窗媒体文件

So you need to upgrade your VS 2015 with tools for Windows 10 enabled.因此,您需要使用启用了 Windows 10 的工具升级您的 VS 2015。

I did the following and Windows.Media.Ocr got added as a reference in my Web API.我做了以下操作,并在我的 Web API 中添加了 Windows.Media.Ocr 作为参考。

  1. In your Web API project's references right click any of the references and click View in Object Browser.在您的 Web API 项目的引用中,右键单击任何引用,然后单击在对象浏览器中查看。 Your Object browser will look something like this你的对象浏览器看起来像这样在此处输入图片说明
  2. There is a '...' button right next to the "Browse: All components" Box. “浏览:所有组件”框旁边有一个“...”按钮。 The tooltip reads "Edit Custom Component set".Click!工具提示显示“编辑自定义组件集”。单击!
  3. In the "Edit Custom Component Set" Box click the Browse Tab and Navigate to "C:/Program Files(x86)/Windows Kits/10/References/Windows.Foundation.UniversalApiContract/1.0.0.0" and click the winmd file that results and click Add.在“编辑自定义组件集”框中,单击“浏览”选项卡并导航到“C:/Program Files(x86)/Windows Kits/10/References/Windows.Foundation.UniversalApiContract/1.0.0.0”,然后单击生成的 winmd 文件并单击添加。
  4. Hit Ok!点击确定!
  5. Now click icon that reads "Add to references in Selected Project in Solution Explorer"(It is the third button from the '...' button next to "Browse:").现在单击“在解决方案资源管理器中添加到选定项目中的引用”的图标(它是“浏览:”旁边的“...”按钮中的第三个按钮)。 You can see that Windows.Media.Ocr is available to use.您可以看到可以使用 Windows.Media.Ocr。

Note: The following works only with VS 2015 and windows 10. That too VS 2015 should be updated for Universal Windows Platform (UWP).注意:以下内容仅适用于 VS 2015 和 Windows 10。VS 2015 也应针对通用 Windows 平台(UWP) 进行更新。 Check this for a sample OCR.检查示例 OCR。

Hope this helps.希望这可以帮助。 Update It got imported into my reference but is failing to load though.更新它已导入到我的参考中,但无法加载。 Hope it provides some start for people.希望它为人们提供一些开始。 Thanks!.谢谢!。

You can Skip to Update 2 below for a working solution.您可以跳到下面的更新 2 以获得有效的解决方案。

IT WILL THROW A TYPE LOAD EXCEPTION.它会抛出一个类型的负载异常。 That being said I am posting because I am trying to do the same thing but can't get the project to run.话虽如此,我发帖是因为我正在尝试做同样的事情,但无法让项目运行。 Here are some basic instructions on how to get the winrt api into your non windows app project.以下是有关如何将 winrt api 导入非 Windows 应用程序项目的一些基本说明。

http://weblogs.thinktecture.com/cnagel/2012/10/calling-winrt-from-windows-desktop-apps.html http://weblogs.thinktecture.com/cnagel/2012/10/calling-winrt-from-windows-desktop-apps.html

Also don't try to reference the dll, reference instead the winmd file.也不要尝试引用 dll,而是引用 winmd 文件。

Here is a sample console app that references the ocr library but when you run the solution it throws the type load exception.( https://github.com/Xandroid4Net/MicrsoftOcrConsoleApp ) It should be easily ported from a console app to and asp.net application.这是一个引用 ocr 库的示例控制台应用程序,但是当您运行该解决方案时,它会引发类型加载异常。( https://github.com/Xandroid4Net/MicrsoftOcrConsoleApp )它应该可以轻松地从控制台应用程序移植到 asp。净应用。 I don't know how to fix the type load exception maybe you can get farther than I can.我不知道如何修复类型加载异常,也许你可以做得比我更远。 Please post if you do find a solution.如果您确实找到了解决方案,请发布。

UPDATE 1 (12/1/2014)更新 1 (12/1/2014)

More digging revealed the following assembly binding error.更多的挖掘揭示了以下程序集绑定错误。 Any idea how to set a package Id for a Process?知道如何为进程设置包 ID 吗?

File: WindowsPreview.Media.Ocr!WindowsPreview.Media.Ocr.OcrEngine, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime.htm文件:WindowsPreview.Media.Ocr!WindowsPreview.Media.Ocr.OcrEngine,版本=255.255.255.255,Culture=neutral,PublicKeyToken=null,ContentType=WindowsRuntime.htm

File Contents:文件内容:

* Assembly Binder Log Entry (12/1/2014 @ 11:48:01 PM) * * 组装活页夹日志条目 (12/1/2014 @ 11:48:01 PM) *

The operation failed.操作失败。 Bind result: hr = 0x80073d54.绑定结果:hr = 0x80073d54。 The process has no package identity.该进程没有包标识。

Assembly manager loaded from: C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\clr.dll Running under executable C:\\Users\\Wesley\\Documents\\Dev\\ConsoleApplication2\\Program.exe --- A detailed error log follows.程序集管理器加载自:C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\clr.dll 在可执行文件 C:\\Users\\Wesley\\Documents\\Dev\\ConsoleApplication2\\Program.exe 下运行 --- 详细错误日志如下.

BEGIN : Windows Runtime Type bind.开始:Windows 运行时类型绑定。 END : The process has no package identity. END :进程没有包标识。 (Exception from HRESULT: 0x80073D54) (来自 HRESULT 的异常:0x80073D54)

Update 2 (12/03/2014)更新 2 (12/03/2014)

This is a nasty workaround but it worked on my Windows 8.1 Surface Pro 3 tablet.这是一个令人讨厌的解决方法,但它适用于我的 Windows 8.1 Surface Pro 3 平板电脑。 The WebOcr is a webforms but it can easily be refactored for MVC WebOcr 是一个 webforms,但它可以很容易地为 MVC 重构

1) Download https://github.com/Xandroid4Net/CommandLineOcr . 1) 下载https://github.com/Xandroid4Net/CommandLineOcr This is the psuedo store app.这是伪商店应用程序。

2) Build and run the app. 2) 构建并运行应用程序。 It will appear to do nothing.它似乎什么都不做。 That is okay, it will update the registry with a new key that we will need.没关系,它将使用我们需要的新密钥更新注册表。

3) Download https://github.com/Xandroid4Net/WebOcr . 3) 下载https://github.com/Xandroid4Net/WebOcr

4) Find OcrCommandLineCaller.cs in the Webocr project. 4)在Webocr项目中找到OcrCommandLineCaller.cs。

5) Locate the registry key at HKEY_CURRENT_USER\\Software\\Classes\\ActivatableClasses\\Package\\Some_Sort_Of_Guid_For_Your_APP\\Server\\App.App....\\AppUserModelId 5) 在 HKEY_CURRENT_USER\\Software\\Classes\\ActivatableClasses\\Package\\Some_Sort_Of_Guid_For_Your_APP\\Server\\App.App....\\AppUserModelId 找到注册表项

Refer to this SO question if you need more help finding the registry key.如果您需要更多帮助来查找注册表项,请参阅此 SO 问题。 IApplicationActivationManager::ActivateApplication in C#? IApplicationActivationManager::ActivateApplication 在 C# 中?

6) Update the appActiveManager.ActivateApplication call in OcrCommandLineCaller.cs with the strange guid app identifier found in the registry key. 6) 使用在注册表项中找到的奇怪的 guid 应用程序标识符更新 OcrCommandLineCaller.cs 中的 appActiveManager.ActivateApplication 调用。

7) In default.aspx.cs replace the saveAsPath with the path on your machine. 7) 在 default.aspx.cs 中,将 saveAsPath 替换为您机器上的路径。 In the windows store app there is a static path represented by Windows.Storage.ApplicationData.Current.LocalFolder.在 Windows 商店应用程序中,有一个由 Windows.Storage.ApplicationData.Current.LocalFolder 表示的静态路径。 This is the path where I saved my images for simplicity.为简单起见,这是我保存图像的路径。

8) Modify any of the code to your heart's content and let me know if you have any questions. 8) 将任何代码修改为您满意的内容,如果您有任何问题,请告诉我。

This is a very rough and nasty solution, but it does work.这是一个非常粗糙和讨厌的解决方案,但它确实有效。

I have been using the MODI solution packaged with MS Office for a few years, and was pretty happy with it (it was free if you bought Office).几年来,我一直在使用与 MS Office 一起打包的 MODI 解决方案,并且对它非常满意(如果您购买了 Office,则它是免费的)。 I was rather disappointed when it was discontinued.当它停产时,我相当失望。 I've tried Tesseract... I really wanted to like it, but found it slow and inaccurate for Dutch machine type of good quality... and like you, I could not justify spending anything north $200 for what was essentially a hobby project.我试过 Tesseract ......我真的很想喜欢它,但发现它对于高质量的荷兰机器类型来说速度慢且不准确......和你一样,我无法证明在本质上是一个业余爱好项目上花费 200 美元以北.

After a desperate search, someone on here pointed me at TOCR (a Transym product).经过绝望的搜索,这里有人指给我 TOCR(一种 Transym 产品)。 An epiphany followed soon after.顿悟紧随其后。 ;-) I think I need to say at this point that I am not affiliated with Transym in any way, and yes, I paid the full price... of 60 pounds! ;-) 我想在这一点上我需要说我与 Transym 没有任何关联,是的,我支付了全价...... 60 英镑! (no typo... sixty quid), which, including VAT, worked out to 113 euros. (没有错别字... 60 英镑),其中包括增值税,共计 113 欧元。

It is essentially meant for integrators (it includes a scan/viewer/OCR app, but that is merely meant as a demo - if you buy the license, you get its source code).它本质上是为集成商设计的(它包括一个扫描/查看器/OCR 应用程序,但这仅仅是一个演示——如果你购买了许可证,你就会得到它的源代码)。 The API is outdated (it is, as OCR engines go, quite a mature code base), but it is fast, stable, and unexpectedly accurate. API 已经过时(就 OCR 引擎而言,它是一个相当成熟的代码库),但它快速、稳定且出乎意料地准确。 Not as accurate as the DokuStar engine or other esoteric engines, but for my application (Dutch and ENglish machine type) it holds its own against various engines that are well north of $1000.不像 DokuStar 引擎或其他深奥的引擎那么准确,但对于我的应用程序(荷兰语和英语机器类型),它对各种价格超过 1000 美元的引擎都有自己的影响。 Recognition accuracy on Dutch machine type is excellent (it doesn't do handwriting).荷兰机器类型的识别准确度非常好(它不会手写)。 In my opinion, in terms of value for money, it is simply ridiculously good.在我看来,就性价比而言,它简直是好得离谱。 As to the API: I wrote a rudimentary .NET wrapper around it to suit my needs - this was done in a few evenings.至于 API:我围绕它编写了一个基本的 .NET 包装器以满足我的需要——这是在几个晚上完成的。

There is an eval version available on their web site ( http://www.transym.com/index.htm ).在他们的网站 ( http://www.transym.com/index.htm ) 上有一个 eval 版本。 And no, I don't get any money if you do ;-)不,如果你这样做,我不会得到任何钱;-)

I managed to invoke the OCR from Web API by invoking the DLL from powershell.我设法通过从 powershell 调用 DLL 来从 Web API 调用 OCR。 I found also that you cannot invoke this DLL in parallel.我还发现您不能并行调用此 DLL。

It took me a few days, but here is the repo.我花了几天时间,但这里是 repo。 Available both as nuget and web API with Windows docker https://github.com/MihaiTheCoder/ocr-all-in-one可作为 nuget 和 Web API 使用 Windows docker https://github.com/MihaiTheCoder/ocr-all-in-one

I developed Web API project mostly for this question, so hopefully somebody will find it useful.我主要为这个问题开发了 Web API 项目,所以希望有人会发现它有用。

The powershell that is invoking Windows OCR is here: https://github.com/MihaiTheCoder/ocr-all-in-one/blob/master/Ocr.Wrapper/Resources/Get-Text-Win-OCR.ps1调用 Windows OCR 的 powershell 在这里: https : //github.com/MihaiTheCoder/ocr-all-in-one/blob/master/Ocr.Wrapper/Resources/Get-Text-Win-OCR.ps1

It is a 2 stage process because the initialization of the OCR engine takes more time, and it is better to have it cached.这是一个 2 个阶段的过程,因为 OCR 引擎的初始化需要更多时间,最好将其缓存。

IMHO, probably not worth your effort to try to use a library that is not supported on the platform you are targeting.恕我直言,可能不值得您尝试使用您所针对的平台不支持的库。

You could check this answer for some free alternatives.您可以查看此答案以获取一些免费替代方案。

I managed to get the UWP OCR API working on desktop app using uwpdesktop NuGet.我设法使用 uwpdesktop NuGet 使 UWP OCR API 在桌面应用程序上工作。

Install-Package uwpdesktop安装包 uwpdesktop

You can then make calls to UWP APIs.然后可以调用 UWP API。

I am not a lawyer, but my understanding is the the Microsoft end user license agreement for that library does not allow to use the Microsoft OCR Library for Windows Runtime in your scenario.我不是律师,但我的理解是该库的 Microsoft 最终用户许可协议不允许在您的场景中使用 Microsoft OCR 库 for Windows Runtime。

Specifically, read the second paragraph, where it forbids to use the software on a server in a production environment:具体请阅读第二段,其中禁止在生产环境中的服务器上使用该软件:

"2. INSTALLATION AND USE RIGHTS. "2. 安装和使用权利。

a.一种。 General.一般的。 One user may install and use copies of the software to design, develop, test and demonstrate your programs.一名用户可以安装和使用该软件的副本来设计、开发、测试和演示您的程序。 You may not use the software on a server in a production environment ."您不得在生产环境中的服务器上使用该软件。”

At the same time, they also restrict the run of the distributable code exclusively to Windows Store or Windows Phone:同时,他们还将可分发代码的运行仅限于 Windows Store 或 Windows Phone:

"3. ADDITIONAL LICENSING REQUIREMENTS AND/OR USE RIGHTS. "3. 附加许可要求和/或使用权。

a.一种。 Distributable Code.可分发代码。 The software contains code that you are permitted to distribute in programs you develop if you comply with the terms below.如果您遵守以下条款,则该软件包含您被允许在您开发的程序中分发的代码。

i.一世。 .. ..

ii. ii. .. ..

iii.三、 Distribution Restrictions.分发限制。 You may not你不可以

 ..

· distribute Distributable Code to run on a platform other than the Windows Store or Windows Phone ;" ·分发可分发代码以在 Windows 应用商店或 Windows Phone 以外的平台上运行;”

The full EULA can be read at: https://www.microsoft.com/web/webpi/eula/windows_runtime_ocr_library_terms_of_use.htm完整的 EULA 可以在以下位置阅读: https : //www.microsoft.com/web/webpi/eula/windows_runtime_ocr_library_terms_of_use.htm

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

相关问题 ASP.NET MVC中的Tesseract OCR - Tesseract OCR in ASP.NET MVC 如何在ASP.NET Core中创建Microsoft GraphServiceClient Web Api 6 - How to create Microsoft GraphServiceClient in ASP.NET Core Web Api 6 ASP.NET MVC4和Web API身份验证+授权(Windows 8和Web) - ASP.NET MVC4 and Web API Authentication + Authorization (Windows 8 and Web) 如何在ASP.NET Core 2.0 Web API中配置和使用Microsoft.AspNetCore.JsonPatch - How to configure and use Microsoft.AspNetCore.JsonPatch in ASP.NET Core 2.0 Web API 如何使用Microsoft ASP.NET Web API OData中的Delta <T>和Code First \ JsonMediaTypeFormatter - How to use Delta<T> from Microsoft ASP.NET Web API OData with Code First\JsonMediaTypeFormatter 从ASP.Net Web API和MVC Web应用程序访问Microsoft Graph API(用于OneDrive)。 - Accessing Microsoft Graph API (for OneDrive) from ASP.Net Web API & MVC Web Application. asp.net mvc4中的web.config中的Windows身份验证 - Windows Authentication in web.config in asp.net mvc4 在 web api 项目中使用 asp.net mvc - Use asp.net mvc within a web api project 如何将MySql与asp.net身份一起使用以及将MVC与Microsoft ASP.NET身份示例一起使用? - How to use MySql with asp.net identity and MVC with Microsoft ASP.NET Identity Samples? ASP.NET MVC4 Web ZDB974238714CA8DE634A7CE1D08 中的 URL 路由 - URL route in ASP.NET MVC4 Web API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM