C# OCR實(shí)現(xiàn)文字識(shí)別功能
簡(jiǎn)介
OCR英文全稱是Optical Character Recognition,中文叫做光學(xué)字符識(shí)別。它是利用光學(xué)技術(shù)和計(jì)算機(jī)技術(shù)把印在或?qū)懺诩埳系奈淖肿x取出來(lái),并轉(zhuǎn)換成一種計(jì)算機(jī)能夠接受、人又可以理解的格式。文字識(shí)別是計(jì)算機(jī)視覺(jué)研究領(lǐng)域的分支之一。
效果預(yù)覽

核心庫(kù)概述
NuGet包:PaddleOCRSharp
引用此包后會(huì)自動(dòng)加載進(jìn)所需的第三方依賴包
OCR文字識(shí)別庫(kù)有很多,包括在線的,比較好的是百度的,為什么選用這個(gè)主要是,考慮的離線環(huán)境,PaddleOCR 的識(shí)別率還是不錯(cuò)的。
問(wèn)題
在使用的時(shí)候也遇到了一些問(wèn)題,比如 內(nèi)存釋放不及時(shí) 等
底層的C++邏輯對(duì)內(nèi)存的管理有一些小瑕疵,會(huì)有內(nèi)存溢出的問(wèn)題,通常不會(huì)出現(xiàn)
源碼
界面布局
<Window x:Class="PaddleOCRWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="OCR離線版" Height="450" Width="900" WindowStartupLocation="CenterScreen">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<GroupBox Grid.Column="0" Header="預(yù)覽" Margin="10,10,5,10">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,5">
<Button x:Name="BtnOCR" Content="打開(kāi)圖片" Height="30" Width="120" Margin="0,0,10,0" Click="BtnOCR_Click"/>
<Button x:Name="BtnClose" Content="清除" Height="30" Width="120" Click="BtnClose_Click"/>
</StackPanel>
<Image Name="ImgPreview" Grid.Row="1" Margin="5"/>
</Grid>
</GroupBox>
<GroupBox Grid.Column="1" Header="識(shí)別結(jié)果" Margin="5,10,10,10">
<TextBox x:Name="TxtPreview" TextWrapping="Wrap" BorderThickness="0" IsReadOnly="True"/>
</GroupBox>
</Grid>
</Window>
后臺(tái)邏輯
using Microsoft.Win32;
using PaddleOCRSharp;
using System;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;
namespace PaddleOCRWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void BtnOCR_Click(object sender, RoutedEventArgs e)
{
StartDistinguish();
}
private void BtnClose_Click(object sender, RoutedEventArgs e)
{
ImgPreview.Source = null;
TxtPreview.Text = "";
}
/// <summary>
/// 調(diào)用核心
/// </summary>
private void StartDistinguish()
{
OpenFileDialog openFile = new()
{
Filter = "圖片(*.bmp;*.jpg;*.jpeg;*.png)|*.bmp;*.jpg;*.jpeg;*.png"
};
if (!(bool)openFile.ShowDialog()) return;
Dispatcher.BeginInvoke(new Action(() =>
{
ImgPreview.Source = new BitmapImage(new Uri(openFile.FileName, UriKind.RelativeOrAbsolute));
}));
Task.Run(() =>
{
var imagebyte = File.ReadAllBytes(openFile.FileName);
Bitmap bitmap = new(new MemoryStream(imagebyte));
OCRModelConfig? config = null;
OCRParameter oCRParameter = new();
OCRResult ocrResult = new();
using (PaddleOCREngine engine = new(config, oCRParameter))
{
ocrResult = engine.DetectText(bitmap);
}
if (ocrResult != null)
{
Dispatcher.BeginInvoke(new Action(() =>
{
TxtPreview.Text = ocrResult.Text;
}));
}
});
}
}
}
到此這篇關(guān)于C# OCR實(shí)現(xiàn)文字識(shí)別功能的文章就介紹到這了,更多相關(guān)C# OCR文字識(shí)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于StreamRead和StreamWriter的使用(實(shí)例講解)
下面小編就為大家分享一篇基于StreamRead和StreamWriter的使用實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
DevExpress獲取TreeList可視區(qū)域節(jié)點(diǎn)集合的實(shí)現(xiàn)方法
這篇文章主要介紹了DevExpress獲取TreeList可視區(qū)域節(jié)點(diǎn)集合的實(shí)現(xiàn)方法,有一定實(shí)用價(jià)值,需要的朋友可以參考下2014-08-08
為IObservable實(shí)現(xiàn)自己的運(yùn)算符(詳解)
下面小編就為大家?guī)?lái)一篇為IObservable實(shí)現(xiàn)自己的運(yùn)算符(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
C# WinForm程序處理后臺(tái)繁忙導(dǎo)致前臺(tái)控件假死現(xiàn)象解決方法
這篇文章主要介紹了C# WinForm程序處理后臺(tái)繁忙導(dǎo)致前臺(tái)控件假死現(xiàn)象解決方法,本文通過(guò)Application.DoEvents()解決這個(gè)問(wèn)題,并講解了Application.DoEvents()的作用,需要的朋友可以參考下2015-06-06

