如何使用非同步和多執行緒
在不斷發展的軟體開發領域中,高效處理大量文本數據依然是一個重要挑戰。在本文中,我們探討了 IronOCR 和 Tesseract 背景下的異步支持與多線程的動態協同。異步編程引入了一種非阻塞範式,確保我們的應用程序在執行 OCR 任務期間保持靈活和響應性。同時,我們深入研究了多線程領域,解開了並行處理顯著提升文本識別操作性能的潛力。加入我們的行列,一起揭開這些技術整合的神秘面紗,讓開發者能夠提升其 OCR 驅動應用程序的效率和響應能力。
如何使用非同步和多執行緒搭配Tesseract
- 下載支援 Tesseract 的 C# 庫,並支援異步和多線程
- 利用由IronOCR管理的多執行緒
- 準備 PDF 文件和圖像以便閱讀
- 利用 OcrReadTask 物件來利用非同步併發優勢
- 使用
ReadAsync
易用的方法
立即開始在您的專案中使用IronPDF,並享受免費試用。
查看 IronOCR 上 Nuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變OCR。
Install-Package IronOcr
請考慮安裝 IronOCR DLL 直接下載並手動安裝到您的專案或GAC表單: IronOcr.zip
手動安裝到您的項目中
下載DLL理解多線程
在 IronOCR 中,透過無縫的多線程,圖像處理和 OCR 閱讀的效率得到了提升,開發人員不需要使用專門的 API。IronTesseract 自動利用所有可用的多核處理線程,優化系統資源以實現快速和靈敏的 OCR 執行。這種內在的多線程不僅簡化了開發,還大幅提升了性能,展示了並行計算在 OCR 工作流程中的精妙集成。
所以,多線程的讀取就會像這樣簡單:
:path=/static-assets/ocr/content-code-examples/how-to/async-simple-multithreading.cs
using IronOcr;
using System;
var ocr = new IronTesseract();
using (var input = new OcrPdfInput(@"example.pdf"))
{
var result = ocr.Read(input);
Console.WriteLine(result.Text);
};
Imports IronOcr
Imports System
Private ocr = New IronTesseract()
Using input = New OcrPdfInput("example.pdf")
Dim result = ocr.Read(input)
Console.WriteLine(result.Text)
End Using
瞭解異步支援
在光學字符識別的領域 (光學字符識別)非同步編程,或稱為「async」,在優化性能方面發揮著關鍵作用。非同步支持允許開發人員執行 OCR 任務而不阻塞主線程,確保應用程式保持響應。想像一下處理大型文件或圖像的文字識別——非同步支持允許系統在進行 OCR 操作的同時繼續處理其他任務。
在本節中,我們將深入探討如何在 IronOCR 中輕鬆集成非同步支持,展示不同的方式使您的 OCR 服務不阻塞。
使用 OcrReadTask 物件
在使用 IronOCR 時,OcrReadTask 物件的使用在增強控制和靈活性方面是一個寶貴的資產。這些物件封裝了 OCR 操作,允許開發者高效地管理文字識別任務。本節提供了在 IronOCR 工作流程中使用 OcrReadTask 物件的示例,展示了如何利用它們來啟動和優化 OCR 任務。無論是協調複雜的文件處理還是微調您的 OCR 驅動應用程式的響應性,有效使用 OcrReadTask 物件有助於最大化 IronOCR 的功能。
:path=/static-assets/ocr/content-code-examples/how-to/async-ocrtask.cs
using IronOcr;
IronTesseract ocr = new IronTesseract();
OcrPdfInput largePdf = new OcrPdfInput("chapter1.pdf");
Func<OcrResult> reader = () =>
{
return ocr.Read(largePdf);
};
OcrReadTask readTask = new OcrReadTask(reader.Invoke);
// Start the OCR task asynchronously
readTask.Start();
// Continue with other tasks while OCR is in progress
DoOtherTasks();
// Wait for the OCR task to complete and retrieve the result
OcrResult result = await Task.Run(() => readTask.Result);
Console.Write($"##### OCR RESULTS ###### \n {result.Text}");
largePdf.Dispose();
readTask.Dispose();
static void DoOtherTasks()
{
// Simulate other tasks being performed while OCR is in progress
Console.WriteLine("Performing other tasks...");
Thread.Sleep(2000); // Simulating work for 2000 milliseconds
}
Imports Microsoft.VisualBasic
Imports IronOcr
Private ocr As New IronTesseract()
Private largePdf As New OcrPdfInput("chapter1.pdf")
Private reader As Func(Of OcrResult) = Function()
Return ocr.Read(largePdf)
End Function
Private readTask As New OcrReadTask(AddressOf reader.Invoke)
' Start the OCR task asynchronously
readTask.Start()
' Continue with other tasks while OCR is in progress
DoOtherTasks()
' Wait for the OCR task to complete and retrieve the result
Dim result As OcrResult = Await Task.Run(Function() readTask.Result)
Console.Write($"##### OCR RESULTS ###### " & vbLf & " {result.Text}")
largePdf.Dispose()
readTask.Dispose()
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'static void DoOtherTasks()
'{
' ' Simulate other tasks being performed while OCR is in progress
' Console.WriteLine("Performing other tasks...");
' Thread.Sleep(2000); ' Simulating work for 2000 milliseconds
'}
使用異步方法
`ReadAsync()提供了一種簡單且直觀的機制來異步啟動 OCR 操作。在不需要複雜的線程或任務管理的情況下,開發人員可以輕鬆地將異步 OCR 集成到他們的應用程式中。這種方法釋放了主線程免受堵塞式 OCR 任務的負擔,確保應用程式保持響應和敏捷。
:path=/static-assets/ocr/content-code-examples/how-to/async-read-async.cs
using IronOcr;
using System;
using System.Threading.Tasks;
IronTesseract ocr = new IronTesseract();
using (OcrPdfInput largePdf = new OcrPdfInput("PDFs/example.pdf"))
{
var result = await ocr.ReadAsync(largePdf);
DoOtherTasks();
Console.Write($"##### OCR RESULTS ###### " +
$"\n {result.Text}");
}
static void DoOtherTasks()
{
// Simulate other tasks being performed while OCR is in progress
Console.WriteLine("Performing other tasks...");
System.Threading.Thread.Sleep(2000); // Simulating work for 2000 milliseconds
}
Imports Microsoft.VisualBasic
Imports IronOcr
Imports System
Imports System.Threading.Tasks
Private ocr As New IronTesseract()
Using largePdf As New OcrPdfInput("PDFs/example.pdf")
Dim result = Await ocr.ReadAsync(largePdf)
DoOtherTasks()
Console.Write($"##### OCR RESULTS ###### " & $vbLf & " {result.Text}")
End Using
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'static void DoOtherTasks()
'{
' ' Simulate other tasks being performed while OCR is in progress
' Console.WriteLine("Performing other tasks...");
' System.Threading.Thread.Sleep(2000); ' Simulating work for 2000 milliseconds
'}
結論
總結來說,在IronOCR中利用多線程對於優化OCR任務是一次革命性的改變。IronOCR的內建多線程能力,結合像ReadAsync這樣的用戶友好方法。(),簡化大量文本數據的處理。這種協同作用確保您的應用程序保持響應和高效,使IronOCR成為一個強大的工具,用於創建具有精簡文本識別功能的高性能軟件解決方案。