当前位置: 首页 > news >正文

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);}

待识别图片
在这里插入图片描述
识别结果:
在这里插入图片描述


http://www.mrgr.cn/news/100450.html

相关文章:

  • pytorch学习使用
  • Pycharm(十七)生成器
  • 常用的性能提升手段--提纲
  • 【玩转 JS 函数式编程_016】DIY 实战:巧用延续传递风格(CPS)重构倒计时特效逻辑
  • 手动实现legend 与 echarts图交互 通过元素和js事件实现图标某项的高亮 显示与隐藏
  • Android源码编译命令详解
  • 深入理解布隆过滤器:参数设定与优化
  • 论文导读 - 基于大规模测量与多任务深度学习的电子鼻系统实现目标识别、浓度预测与状态判断
  • C++ ——引用
  • 大模型应用实战:深入理解模型上下文协议 MCP
  • 2025 FIC wp
  • 关系型数据库PostgreSQL for Mac 保姆级使用教程
  • C++初阶-模板初阶
  • 计算机网络学习笔记 4-6章
  • java异常
  • Appium自动化开发环境搭建
  • 虚函数表的设计和多态的实现
  • 关于Android Studio的AndroidManifest.xml的详解
  • pyspark将hive数据写入Excel文件中
  • 视觉/深度学习/机器学习相关面经总结(2)(持续更新)