赞
踩
目录
VS2022+.net 4.8+OpenCvSharp4
Sdcb.PaddleInference/2.5.0-preview.1
Sdcb.PaddleOCR/2.6.0.6-preview.1
using OpenCvSharp;
using Sdcb.PaddleInference;
using Sdcb.PaddleOCR;
using Sdcb.PaddleOCR.Models;
using Sdcb.PaddleOCR.Models.Online;
using System;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace OCRTest_V4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
img = "test.jpg";
pictureBox1.Image = new Bitmap(img);
src = Cv2.ImRead(img);
}
Bitmap bmp;
string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
string img = "";
DateTime dt1 = DateTime.Now;
DateTime dt2 = DateTime.Now;
StringBuilder sb = new StringBuilder();
Mat src;
FullOcrModel model_v3;
PaddleOcrAll all_v3;
FullOcrModel model_v4;
PaddleOcrAll all_v4;
bool Mkldnn = false;
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = fileFilter;
if (ofd.ShowDialog() != DialogResult.OK) return;
pictureBox1.Image = null;
img = ofd.FileName;
bmp = new Bitmap(img);
pictureBox1.Image = new Bitmap(img);
textBox1.Text = "";
src = Cv2.ImRead(img);
}
private void TestV4()
{
sb.Clear();
dt1 = DateTime.Now;
PaddleOcrResult result = all_v4.Run(src);
dt2 = DateTime.Now;
int index = 1;
foreach (PaddleOcrResultRegion region in result.Regions)
{
sb.AppendLine($"{index++},Text: {region.Text}, Score: {region.Score}");
}
sb.AppendLine("------------------------------------");
sb.AppendLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
sb.AppendLine("Open Mkldnn:" + Mkldnn);
sb.AppendLine("V4耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
textBox1.Text = sb.ToString();
}
private void TestV3()
{
sb.Clear();
dt1 = DateTime.Now;
PaddleOcrResult result = all_v3.Run(src);
dt2 = DateTime.Now;
int index = 1;
foreach (PaddleOcrResultRegion region in result.Regions)
{
sb.AppendLine($"{index++},Text: {region.Text}, Score: {region.Score}");
}
sb.AppendLine("------------------------------------");
sb.AppendLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
sb.AppendLine("Open Mkldnn:" + Mkldnn);
sb.AppendLine("V3耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
textBox1.Text = sb.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
if (all_v3 == null)
{
MessageBox.Show("请先初始化");
return;
}
if (src == null)
{
MessageBox.Show("请先选择图片");
return;
}
TestV3();
}
private void button3_Click(object sender, EventArgs e)
{
if (all_v4 == null)
{
MessageBox.Show("请先初始化");
return;
}
if (src == null)
{
MessageBox.Show("请先选择图片");
return;
}
TestV4();
}
private void button4_Click(object sender, EventArgs e)
{
Mkldnn = false;
Task t= Init();
t.Wait();
MessageBox.Show("初始化成功");
}
private void button5_Click(object sender, EventArgs e)
{
Mkldnn = true;
Task t = Init_Mkldnn();
t.Wait();
MessageBox.Show("初始化成功");
}
async Task Init()
{
model_v3 = await OnlineFullModels.ChineseV3.DownloadAsync();
all_v3 = new PaddleOcrAll(model_v3, PaddleDevice.Mkldnn());
all_v3.AllowRotateDetection = true; /* 允许识别有角度的文字 */
all_v3.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */
//model_v4 = await OnlineFullModels.ChineseV4.DownloadAsync();
//all_v4 = new PaddleOcrAll(model_v4, PaddleDevice.Mkldnn());
//all_v4.AllowRotateDetection = true; /* 允许识别有角度的文字 */
//all_v4.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */
model_v4 = await OnlineFullModels.ChineseV4.DownloadAsync();
all_v4 = new PaddleOcrAll(model_v4,
PaddleDevice.Mkldnn(),
PaddleDevice.Mkldnn(),
c =>
{
Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableONNXRuntime(c.UnsafeGetHandle());
Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableORTOptimization(c.UnsafeGetHandle());
// c.CpuMathThreadCount = 8;
});
all_v4.AllowRotateDetection = true; /* 允许识别有角度的文字 */
all_v4.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */
}
async Task Init_Mkldnn()
{
model_v3 = await OnlineFullModels.ChineseV3.DownloadAsync();
all_v3 = new PaddleOcrAll(model_v3,
c => Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableONNXRuntime(c.UnsafeGetHandle()),
PaddleDevice.Mkldnn(),
c => Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableONNXRuntime(c.UnsafeGetHandle()));
all_v3.AllowRotateDetection = true; /* 允许识别有角度的文字 */
all_v3.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */
model_v4 = await OnlineFullModels.ChineseV4.DownloadAsync();
all_v4 = new PaddleOcrAll(model_v4,
c => Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableONNXRuntime(c.UnsafeGetHandle()),
PaddleDevice.Mkldnn(),
c => Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableONNXRuntime(c.UnsafeGetHandle()));
all_v4.AllowRotateDetection = true; /* 允许识别有角度的文字 */
all_v4.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */
}
}
}
- using OpenCvSharp;
- using Sdcb.PaddleInference;
- using Sdcb.PaddleOCR;
- using Sdcb.PaddleOCR.Models;
- using Sdcb.PaddleOCR.Models.Online;
- using System;
- using System.Drawing;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
-
- namespace OCRTest_V4
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void Form1_Load(object sender, EventArgs e)
- {
- img = "test.jpg";
- pictureBox1.Image = new Bitmap(img);
- src = Cv2.ImRead(img);
- }
-
- Bitmap bmp;
- string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
- string img = "";
-
- DateTime dt1 = DateTime.Now;
- DateTime dt2 = DateTime.Now;
-
- StringBuilder sb = new StringBuilder();
-
- Mat src;
-
- FullOcrModel model_v3;
- PaddleOcrAll all_v3;
-
- FullOcrModel model_v4;
- PaddleOcrAll all_v4;
-
- bool Mkldnn = false;
-
- private void button2_Click(object sender, EventArgs e)
- {
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.Filter = fileFilter;
- if (ofd.ShowDialog() != DialogResult.OK) return;
-
- pictureBox1.Image = null;
-
- img = ofd.FileName;
- bmp = new Bitmap(img);
- pictureBox1.Image = new Bitmap(img);
- textBox1.Text = "";
-
- src = Cv2.ImRead(img);
- }
-
- private void TestV4()
- {
- sb.Clear();
- dt1 = DateTime.Now;
- PaddleOcrResult result = all_v4.Run(src);
- dt2 = DateTime.Now;
- int index = 1;
- foreach (PaddleOcrResultRegion region in result.Regions)
- {
- sb.AppendLine($"{index++},Text: {region.Text}, Score: {region.Score}");
- }
- sb.AppendLine("------------------------------------");
- sb.AppendLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
- sb.AppendLine("Open Mkldnn:" + Mkldnn);
- sb.AppendLine("V4耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
- textBox1.Text = sb.ToString();
- }
-
- private void TestV3()
- {
- sb.Clear();
- dt1 = DateTime.Now;
- PaddleOcrResult result = all_v3.Run(src);
- dt2 = DateTime.Now;
- int index = 1;
- foreach (PaddleOcrResultRegion region in result.Regions)
- {
- sb.AppendLine($"{index++},Text: {region.Text}, Score: {region.Score}");
- }
- sb.AppendLine("------------------------------------");
- sb.AppendLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
- sb.AppendLine("Open Mkldnn:" + Mkldnn);
- sb.AppendLine("V3耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");
- textBox1.Text = sb.ToString();
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- if (all_v3 == null)
- {
- MessageBox.Show("请先初始化");
- return;
- }
-
- if (src == null)
- {
- MessageBox.Show("请先选择图片");
- return;
- }
-
- TestV3();
- }
-
- private void button3_Click(object sender, EventArgs e)
- {
- if (all_v4 == null)
- {
- MessageBox.Show("请先初始化");
- return;
- }
-
- if (src == null)
- {
- MessageBox.Show("请先选择图片");
- return;
- }
- TestV4();
- }
-
- private void button4_Click(object sender, EventArgs e)
- {
- Mkldnn = false;
- Task t= Init();
- t.Wait();
- MessageBox.Show("初始化成功");
- }
-
- private void button5_Click(object sender, EventArgs e)
- {
- Mkldnn = true;
- Task t = Init_Mkldnn();
- t.Wait();
- MessageBox.Show("初始化成功");
- }
-
- async Task Init()
- {
- model_v3 = await OnlineFullModels.ChineseV3.DownloadAsync();
- all_v3 = new PaddleOcrAll(model_v3, PaddleDevice.Mkldnn());
- all_v3.AllowRotateDetection = true; /* 允许识别有角度的文字 */
- all_v3.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */
-
- //model_v4 = await OnlineFullModels.ChineseV4.DownloadAsync();
- //all_v4 = new PaddleOcrAll(model_v4, PaddleDevice.Mkldnn());
- //all_v4.AllowRotateDetection = true; /* 允许识别有角度的文字 */
- //all_v4.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */
-
- model_v4 = await OnlineFullModels.ChineseV4.DownloadAsync();
- all_v4 = new PaddleOcrAll(model_v4,
- PaddleDevice.Mkldnn(),
- PaddleDevice.Mkldnn(),
- c =>
- {
- Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableONNXRuntime(c.UnsafeGetHandle());
- Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableORTOptimization(c.UnsafeGetHandle());
- // c.CpuMathThreadCount = 8;
- });
-
- all_v4.AllowRotateDetection = true; /* 允许识别有角度的文字 */
- all_v4.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */
- }
-
- async Task Init_Mkldnn()
- {
- model_v3 = await OnlineFullModels.ChineseV3.DownloadAsync();
- all_v3 = new PaddleOcrAll(model_v3,
- c => Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableONNXRuntime(c.UnsafeGetHandle()),
- PaddleDevice.Mkldnn(),
- c => Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableONNXRuntime(c.UnsafeGetHandle()));
-
- all_v3.AllowRotateDetection = true; /* 允许识别有角度的文字 */
- all_v3.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */
-
- model_v4 = await OnlineFullModels.ChineseV4.DownloadAsync();
- all_v4 = new PaddleOcrAll(model_v4,
- c => Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableONNXRuntime(c.UnsafeGetHandle()),
- PaddleDevice.Mkldnn(),
- c => Sdcb.PaddleInference.Native.PaddleNative.PD_ConfigEnableONNXRuntime(c.UnsafeGetHandle()));
-
- all_v4.AllowRotateDetection = true; /* 允许识别有角度的文字 */
- all_v4.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。