C#本地使用离线ocr库识别图片中文本,工具包PaddleOCRSharp
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);}
待识别图片
识别结果: