C#本地使用離線ocr庫識別圖片文本,工具包PaddleOCRSharp
PaddleOCRSharp介紹
項目地址:https://github.com/raoyutian/PaddleOCRSharp
PaddleOCRSharp 是一個.NET版本OCR可離線使用類庫。項目核心組件PaddleOCR.dll目前已經支持C\C++、.NET、Python、Golang、Rust、java等眾多開發語言的直接API接口調用。項目包含文本識別、文本檢測、表格識別功能。本項目做了大量優化,提高了識別率和推理性能。包含總模型僅8.6M的超輕量級中文OCR,單模型支持中英文數字組合識別、豎排文本識別、長文本識別。同時支持中英文、純英文以及多種語言文本檢測識別。
PaddleOCRSharp封裝極其簡化,實際調用僅幾行代碼,極大的方便了中下游開發者的使用和降低了PaddleOCR的使用入門級別,同時提供不同的.NET框架使用,方便各個行業應用開發與部署。Nuget包即裝即用,可以離線部署,不需要網絡就可以識別的高精度中英文OCR。
本項目支持官方所有公開的通用OCR模型,如:PPOCRV2、PPOCRV3、PPOCRV4、PP-OCRv4_server、PP-OCRv4_server_doc(1.5萬字符字典模型)。最新版默認使用中英文V4模型ch_PP-OCRv4
我的測試使用框架:
.NET9
- WPF
- nuget包:PaddleOCRSharp
XAML代碼:
<Window x:Class="WpfOCR.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfOCR"mc:Ignorable="d"Title="MainWindow" Height="850" Width="900" FontSize="22" WindowStartupLocation="CenterScreen"><Grid><Grid.RowDefinitions><RowDefinition Height="140"></RowDefinition><RowDefinition Height="*"></RowDefinition></Grid.RowDefinitions><StackPanel Grid.Row="0" Orientation="Vertical" ><TextBlock Margin="5" TextWrapping="Wrap" Text="選擇待識別圖片:" VerticalAlignment="Top" HorizontalAlignment="Left" /><TextBox Margin="5" x:Name="imgDetectionPath" TextWrapping="Wrap" Text="" Width="680" HorizontalAlignment="Left" /><Button Margin="5" Content="識別" Click="Button_Click" Width="96" HorizontalAlignment="Left" VerticalAlignment="Top" /></StackPanel><Grid Grid.Row="1" ><Grid.RowDefinitions><RowDefinition Height="auto" ></RowDefinition><RowDefinition Height="*"></RowDefinition></Grid.RowDefinitions><TextBlock Grid.Row="0" HorizontalAlignment="Left" TextWrapping="Wrap" Text="識別結果:" VerticalAlignment="Top" /><TextBox Grid.Row="1" x:Name="txtResult" VerticalScrollBarVisibility="Auto" HorizontalAlignment="Stretch" TextWrapping="Wrap" AcceptsReturn="True" Text="" VerticalAlignment="Stretch" /></Grid></Grid>
</Window>
后臺代碼:
using PaddleOCRSharp;
using System.Drawing;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WpfOCR;/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{public MainWindow(){InitializeComponent();}//識別private void Button_Click(object sender, RoutedEventArgs e){string imgPath = this.imgDetectionPath.Text;var imagebyte = File.ReadAllBytes(imgPath);Bitmap bitmap = new Bitmap(new MemoryStream(imagebyte));OCRModelConfig config = null;OCRParameter oCRParameter = new OCRParameter();oCRParameter.use_gpu = true;//當使用GPU版本的預測庫時,該參數打開才有效果OCRResult ocrResult = null;PaddleOCREngine engine = new PaddleOCREngine(config, oCRParameter);ocrResult = engine.DetectText(bitmap);List<OcrResult>? ocrResults = System.Text.Json.JsonSerializer.Deserialize<List<OcrResult>>(ocrResult.JsonText);string text = string.Join(System.Environment.NewLine, ocrResults.Select(g => g.Text));this.txtResult.Text = text;}public record OcrResult(string Text);}
待識別圖片
識別結果: