效果
项目
VS2022+.net4.8+OCRV4
代码
using OpenCvSharp;
using Sdcb.PaddleInference;
using Sdcb.PaddleOCR;
using Sdcb.PaddleOCR.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace PaddleInference.OCRServiceV4
{
public partial class frmTest : Form
{
public frmTest()
{
InitializeComponent();
}
String startupPath;
public PaddleOcrAll paddleOcr;
private string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
Bitmap bmp;
PaddleOcrResult ocrResult;
DateTime dt1 = DateTime.Now;
DateTime dt2 = DateTime.Now;
String imgPath;
Mat mat;
Pen pen = new Pen(Brushes.Red, 2);
private void frmTest_Load(object sender, EventArgs e)
{
comboBox1.SelectedIndex = 0;
startupPath = Application.StartupPath;
rbOnnx.Checked = true;
}
private void btnLoadModel_Click(object sender, EventArgs e)
{
LoadModel();
MessageBox.Show("加载完成");
}
void LoadModel()
{
string detectionModelDir;
string classificationModelDir;
string recognitionModelDir;
string labelFilePath;
if (comboBox1.SelectedIndex == 0)
{
detectionModelDir = startupPath + "\\inference\\ch_PP-OCRv4_det_infer";
classificationModelDir = startupPath + "\\inference\\ch_ppocr_mobile_v2.0_cls_infer";
recognitionModelDir = startupPath + "\\inference\\ch_PP-OCRv4_rec_infer";
labelFilePath = startupPath + "\\inference\\ppocr_keys.txt";
}
else
{
detectionModelDir = startupPath + "\\inference_server\\detv4_teacher_inference";
classificationModelDir = startupPath + "\\inference_server\\ch_ppocr_mobile_v2.0_cls_infer";
recognitionModelDir = startupPath + "\\inference_server\\ch_PP-OCRv4_rec_server_infer";
labelFilePath = startupPath + "\\inference_server\\ppocr_keys.txt";
}
FullOcrModel model = FullOcrModel.FromDirectory(detectionModelDir, classificationModelDir, recognitionModelDir, labelFilePath, ModelVersion.V4);
if (rbOnnx.Enabled == true)
{
paddleOcr = new PaddleOcrAll(model, PaddleDevice.Onnx());
}
else if (rbOpenblas.Enabled == true)
{
paddleOcr = new PaddleOcrAll(model, PaddleDevice.Openblas());
}
else if (rbMkldnn.Enabled == true)
{
paddleOcr = new PaddleOcrAll(model, PaddleDevice.Mkldnn());
}
else
{
paddleOcr = new PaddleOcrAll(model);
}
paddleOcr.AllowRotateDetection = true; /* 允许识别有角度的文字 */
paddleOcr.Enable180Classification = false; /* 允许识别旋转角度大于90度的文字 */
}
private void btnSelect_Click(object sender, EventArgs e)
{
if (paddleOcr == null)
{
MessageBox.Show("请先加载模型");
return;
}
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = fileFilter;
if (ofd.ShowDialog() != DialogResult.OK) return;
imgPath = ofd.FileName;
bmp = new Bitmap(imgPath, true);
RotateImage(bmp);
pictureBox1.Image = bmp;
richTextBox1.Clear();
mat = new Mat(ofd.FileName);
//pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
dt1 = DateTime.Now;
ocrResult = paddleOcr.Run(mat);
dt2 = DateTime.Now;
ShowOCRResult(ocrResult);
}
/// <summary>
/// 显示结果
/// </summary>
private void ShowOCRResult(PaddleOcrResult ocrResult)
{
richTextBox1.Clear();
Bitmap bitmap = (Bitmap)this.pictureBox1.Image;
richTextBox1.AppendText("耗时:" + (dt2 - dt1).TotalMilliseconds + "ms\n");
richTextBox1.AppendText("-----------------------------------\n");
richTextBox1.AppendText(ocrResult.Text + "\n");
using (Graphics g = Graphics.FromImage(bitmap))
{
foreach (var item in ocrResult.Regions)
{
g.DrawRectangle(pen, item.Rect.BoundingRect().Left, item.Rect.BoundingRect().Top, item.Rect.BoundingRect().Width, item.Rect.BoundingRect().Height);
}
}
pictureBox1.Image = null;
pictureBox1.Image = bitmap;
}
private void button1_Click(object sender, EventArgs e)
{
if (paddleOcr == null)
{
MessageBox.Show("请先加载模型");
return;
}
if (pictureBox1.Image == null)
{
return;
}
richTextBox1.Clear();
Application.DoEvents();
dt1 = DateTime.Now;
ocrResult = paddleOcr.Run(mat);
dt2 = DateTime.Now;
ShowOCRResult(ocrResult);
}
/// <summary>
/// 根据图片exif调整方向
/// </summary>
/// <param name="img"></param>
public void RotateImage(Bitmap img)
{
var exif = img.PropertyItems;
byte orien = 0;
var item = exif.Where(m => m.Id == 274).ToArray();
if (item.Length > 0)
orien = item[0].Value[0];
switch (orien)
{
case 2:
img.RotateFlip(RotateFlipType.RotateNoneFlipX);//horizontal flip
break;
case 3:
img.RotateFlip(RotateFlipType.Rotate180FlipNone);//right-top
break;
case 4:
img.RotateFlip(RotateFlipType.RotateNoneFlipY);//vertical flip
break;
case 5:
img.RotateFlip(RotateFlipType.Rotate90FlipX);
break;
case 6:
img.RotateFlip(RotateFlipType.Rotate90FlipNone);//right-top
break;
case 7:
img.RotateFlip(RotateFlipType.Rotate270FlipX);
break;
case 8:
img.RotateFlip(RotateFlipType.Rotate270FlipNone);//left-bottom
break;
default:
break;
}
}
}
}
Demo下载