如何使用计算机视觉查找文本
介绍
IronOCR 利用 OpenCV 使用计算机视觉来检测图像中存在文本的区域。 这对于包含大量噪音的图像、文本分布在许多不同位置的图像,以及文本扭曲的图像非常有用。 IronOCR中计算机视觉的使用将确定文本区域的位置,然后使用Tesseract尝试读取这些区域。
IronOCR.ComputerVision 通过 NuGet 包安装
在IronOCR中执行计算机视觉的OpenCV方法可以在常规的IronOCR NuGet包中看到。
使用这些方法需要在解决方案中安装NuGet IronOcr.ComputerVision
,如果您尚未安装,系统会提示您下载。
- Windows:
IronOcr.ComputerVision.Windows
- Linux:
IronOcr.ComputerVision.Linux
- macOS:
IronOcr.ComputerVision.MacOS
macOS ARM:
IronOcr.ComputerVision.MacOS.ARM
使用 NuGet 包管理器安装或在包管理器控制台中粘贴以下内容:
PM> Install-Package IronOcr.ComputerVision.Windows
这将提供使用我们的模型文件所需的程序集以便使用IronOCR计算机视觉。
## 功能和 API
本教程后面附有代码示例。 这里是当前可用方法的总览:
<table class="table table__configuration-variables">
<tr>
<th scope="col">方法</th>
<th scope="col">说明</th>
</tr>
<tr>
<td><a href="#anchor-findtextregion">查找文本区域</a></td>
<td class="word-break--break-word">检测包含文本元素的区域,并指示 Tesseract 仅在检测到文本的区域内搜索文本。</td>
</tr>
<tr>
<td><a href="#anchor-findmultipletextregions">查找多个文本区域</a></td>
<td class="word-break--break-word">检测包含文本元素的区域,并根据文本区域将页面划分为不同的图像。</td>
</tr>
<tr>
<td><a href="#anchor-gettextregions">获取文本区域</a></td>
<td class="word-break--break-word">扫描图像并以 `List `格式返回文本区域列表<CropRectangle>`.</td>
</tr>
</table>
## 查找文本区域
使用`FindTextRegion`将使用计算机视觉来检测OcrInput对象的每个页面上包含文本元素的区域。
```cs
:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findtextregion-1.cs
可选择使用自定义参数调用:
:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findtextregion-2.cs
using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("/path/file.png");
input.FindTextRegion(Scale: 2.0, DilationAmount: 20, Binarize: true, Invert: true);
OcrResult result = ocr.Read(input);
string resultText = result.Text;
Imports IronOcr
Private ocr = New IronTesseract()
Private input = New OcrInput()
input.LoadImage("/path/file.png")
input.FindTextRegion(Scale:= 2.0, DilationAmount:= 20, Binarize:= True, Invert:= True)
Dim result As OcrResult = ocr.Read(input)
Dim resultText As String = result.Text
在此示例中,我将使用以下图像编写一个方法,该方法需要裁剪包含文本的区域,但输入图像中的文本位置可能会有所不同。 在这种情况下,我可以使用FindTextRegion来缩小扫描区域,仅对计算机视觉检测到的文本区域进行扫描。 这是一个示例图片:
:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findtextregion-3.cs
using IronOcr;
using IronSoftware.Drawing;
using System;
using System.Linq;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("wh-words-sign.jpg");
// Find the text region using Computer Vision
Rectangle textCropArea = input.GetPages().First().FindTextRegion();
// For debugging and demonstration purposes, lets see what region it found:
input.StampCropRectangleAndSaveAs(textCropArea, Color.Red, "image_text_area", AnyBitmap.ImageFormat.Png);
// Looks good, so let us apply this region to hasten the read:
var ocrResult = ocr.Read("wh-words-sign.jpg", textCropArea);
Console.WriteLine(ocrResult.Text);
Imports IronOcr
Imports IronSoftware.Drawing
Imports System
Imports System.Linq
Private ocr = New IronTesseract()
Private input = New OcrInput()
input.LoadImage("wh-words-sign.jpg")
' Find the text region using Computer Vision
Dim textCropArea As Rectangle = input.GetPages().First().FindTextRegion()
' For debugging and demonstration purposes, lets see what region it found:
input.StampCropRectangleAndSaveAs(textCropArea, Color.Red, "image_text_area", AnyBitmap.ImageFormat.Png)
' Looks good, so let us apply this region to hasten the read:
Dim ocrResult = ocr.Read("wh-words-sign.jpg", textCropArea)
Console.WriteLine(ocrResult.Text)
现在这段代码有两个输出,第一个是由 StampCropRectangleAndSaveAs
保存的 .png
文件,用于调试。 我们可以看到 IronCV(计算机视觉)以为文字是
看起来很不错。 现在第二个输出是文本本身,即:
IRONSOFTWARE
50,000+
Developers in our active community
10,777,061 19,313
NuGet downloads Support tickets resolved
50%+ 80%+
Engineering Team growth Support Team growth
$25,000+
Raised with #TEAMSEAS to clean our beaches & waterways
查找多个文本区域
FindMultipleTextRegions
的使用涉及一个 OcrInput
对象的所有页面,并使用计算机视觉检测包含文本元素的区域,并根据文本区域将输入分成单独的图像:
:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findmultipletextregions-1.cs
using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("/path/file.png");
input.FindMultipleTextRegions();
OcrResult result = ocr.Read(input);
string resultText = result.Text;
Imports IronOcr
Private ocr = New IronTesseract()
Private input = New OcrInput()
input.LoadImage("/path/file.png")
input.FindMultipleTextRegions()
Dim result As OcrResult = ocr.Read(input)
Dim resultText As String = result.Text
可选择使用自定义参数调用:
:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findmultipletextregions-2.cs
using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("/path/file.png");
input.FindMultipleTextRegions(Scale: 2.0, DilationAmount: -1, Binarize: true, Invert: false);
OcrResult result = ocr.Read(input);
string resultText = result.Text;
Imports IronOcr
Private ocr = New IronTesseract()
Private input = New OcrInput()
input.LoadImage("/path/file.png")
input.FindMultipleTextRegions(Scale:= 2.0, DilationAmount:= -1, Binarize:= True, Invert:= False)
Dim result As OcrResult = ocr.Read(input)
Dim resultText As String = result.Text
FindMultipleTextRegions` 的另一个重载方法接收一个 OCR 页面,并返回一个 OCR 页面列表,其中每个文本区域对应一个 OCR 页面:
:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findmultipletextregions-3.cs
using IronOcr;
using System.Collections.Generic;
using System.Linq;
int pageIndex = 0;
using var input = new OcrInput();
input.LoadImage("/path/file.png");
var selectedPage = input.GetPages().ElementAt(pageIndex);
List<OcrInputPage> textRegionsOnPage = selectedPage.FindMultipleTextRegions();
Imports IronOcr
Imports System.Collections.Generic
Imports System.Linq
Private pageIndex As Integer = 0
Private input = New OcrInput()
input.LoadImage("/path/file.png")
Dim selectedPage = input.GetPages().ElementAt(pageIndex)
Dim textRegionsOnPage As List(Of OcrInputPage) = selectedPage.FindMultipleTextRegions()
获取文本区域
使用 GetTextRegions
返回在页面中检测到文本的裁剪区域列表:
:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-gettextregions.cs
using IronOcr;
using IronSoftware.Drawing;
using System.Collections.Generic;
using System.Linq;
int pageIndex = 0;
using var input = new OcrInput();
input.LoadImage("/path/file.png");
var selectedPage = input.GetPages().ElementAt(pageIndex);
// List<Rectangle> regions = selectedPage.GetTextRegions();
Imports IronOcr
Imports IronSoftware.Drawing
Imports System.Collections.Generic
Imports System.Linq
Private pageIndex As Integer = 0
Private input = New OcrInput()
input.LoadImage("/path/file.png")
Dim selectedPage = input.GetPages().ElementAt(pageIndex)
' List<Rectangle> regions = selectedPage.GetTextRegions();
特定用例指南
在正确的设置和输入文件的情况下,OCR可以是一个非常强大的工具。 它几乎可以完美地模仿人类的阅读能力。