目录
说明
效果
模型
项目
编辑
代码
参考
下载
其他
说明
百度网盘AI大赛-表格检测的第2名方案。
该算法包含表格边界框检测、表格分割和表格方向识别三个部分,首先,ppyoloe-plus-x 对边界框进行预测,并对置信度较高的表格边界框(box)进行裁剪。裁剪后的单个表格实例会送入到DBNet中进行语义分割,分割结果通过opencv轮廓处理获得表格关键点(point)。之后,我们根据DBNet计算的关键点在裁剪后的单个表格实例上绘制表格边界。最后,PP-LCNet结合表格边界先验和表格实例图像,对表格的方向进行预测,并根据之前定义的几何轮廓点与语义轮廓点的对应关系,将几何轮廓点映射为语义轮廓点。
本文使用C# OpenCvSharp DNN 实现百度网盘AI大赛-表格检测第2名方案第三部分-表格方向识别
效果
模型
Model Properties
-------------------------
---------------------------------------------------------------
Inputs
-------------------------
name:input
tensor:Float[-1, 3, 624, 624]
---------------------------------------------------------------
Outputs
-------------------------
name:linear_1.tmp_1
tensor:Float[-1, 4]
---------------------------------------------------------------
项目
代码
using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace OpenCvSharp_DNN_Demo
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
string image_path = "";
string startupPath;
string classer_path;
DateTime dt1 = DateTime.Now;
DateTime dt2 = DateTime.Now;
string model_path;
Mat image;
Mat result_mat;
Mat result_image;
Mat result_mat_to_float;
Net opencv_net;
Mat BN_image;
float[] result_array;
int max_image_length;
Mat max_image;
Rect roi;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = fileFilter;
if (ofd.ShowDialog() != DialogResult.OK) return;
pictureBox1.Image = null;
pictureBox2.Image = null;
textBox1.Text = "";
image_path = ofd.FileName;
pictureBox1.Image = new Bitmap(image_path);
image = new Mat(image_path);
}
private void Form1_Load(object sender, EventArgs e)
{
string model_path = "model/paddle_cls.onnx";
opencv_net = CvDnn.ReadNetFromOnnx(model_path);
image_path = "test_img/1.jpg";
pictureBox1.Image = new Bitmap(image_path);
}
private unsafe void button2_Click(object sender, EventArgs e)
{
if (image_path == "")
{
return;
}
if (image_path == "")
{
return;
}
textBox1.Text = "检测中,请稍等……";
pictureBox2.Image = null;
Application.DoEvents();
Mat image = new Mat(image_path);
//缩放图片
max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
roi = new Rect(0, 0, image.Cols, image.Rows);
image.CopyTo(new Mat(max_image, roi));
//数据归一化处理
BN_image = CvDnn.BlobFromImage(max_image, 1 / 255.0, new OpenCvSharp.Size(624, 624), new Scalar(0, 0, 0), true, false);
//配置图片输入数据
opencv_net.SetInput(BN_image);
dt1 = DateTime.Now;
//模型推理,读取推理结果
result_mat = opencv_net.Forward();
dt2 = DateTime.Now;
//将推理结果转为float数据类型
result_mat_to_float = new Mat(1, 4, MatType.CV_32F, result_mat.Data);
//将数据读取到数组中
result_mat_to_float.GetArray<float>(out result_array);
float max = result_array.Max(); //
int maxIndex = Array.IndexOf(result_array, max); // 获取最大值的索引位置
//语义左上角位于几何左上角,定义为0;
//语义左上角位于几何右上角,定义为1;
//语义左上角位于几何右下角,定义了2;
//语义左上角位于几何左下角,定义为3。
textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms\r\n";
string msg = "";
if (maxIndex == 0) {
msg = "语义左上角位于几何左上角";
}
else if (maxIndex == 1)
{
msg = "语义左上角位于几何右上角";
}
else if (maxIndex == 2)
{
msg = "语义左上角位于几何右下角";
}
else if (maxIndex == 3)
{
msg = "语义左上角位于几何左下角";
}
textBox1.Text += "\r\n" + msg;
}
private void pictureBox2_DoubleClick(object sender, EventArgs e)
{
Common.ShowNormalImg(pictureBox2.Image);
}
private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
Common.ShowNormalImg(pictureBox1.Image);
}
}
}
using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;namespace OpenCvSharp_DNN_Demo
{public partial class frmMain : Form{public frmMain(){InitializeComponent();}string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";string image_path = "";string startupPath;string classer_path;DateTime dt1 = DateTime.Now;DateTime dt2 = DateTime.Now;string model_path;Mat image;Mat result_mat;Mat result_image;Mat result_mat_to_float;Net opencv_net;Mat BN_image;float[] result_array;int max_image_length;Mat max_image;Rect roi;private void button1_Click(object sender, EventArgs e){OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = fileFilter;if (ofd.ShowDialog() != DialogResult.OK) return;pictureBox1.Image = null;pictureBox2.Image = null;textBox1.Text = "";image_path = ofd.FileName;pictureBox1.Image = new Bitmap(image_path);image = new Mat(image_path);}private void Form1_Load(object sender, EventArgs e){string model_path = "model/paddle_cls.onnx";opencv_net = CvDnn.ReadNetFromOnnx(model_path);image_path = "test_img/1.jpg";pictureBox1.Image = new Bitmap(image_path);}private unsafe void button2_Click(object sender, EventArgs e){if (image_path == ""){return;}if (image_path == ""){return;}textBox1.Text = "检测中,请稍等……";pictureBox2.Image = null;Application.DoEvents();Mat image = new Mat(image_path);//缩放图片max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);roi = new Rect(0, 0, image.Cols, image.Rows);image.CopyTo(new Mat(max_image, roi));//数据归一化处理BN_image = CvDnn.BlobFromImage(max_image, 1 / 255.0, new OpenCvSharp.Size(624, 624), new Scalar(0, 0, 0), true, false);//配置图片输入数据opencv_net.SetInput(BN_image);dt1 = DateTime.Now;//模型推理,读取推理结果result_mat = opencv_net.Forward();dt2 = DateTime.Now;//将推理结果转为float数据类型result_mat_to_float = new Mat(1, 4, MatType.CV_32F, result_mat.Data);//将数据读取到数组中result_mat_to_float.GetArray<float>(out result_array);float max = result_array.Max(); // int maxIndex = Array.IndexOf(result_array, max); // 获取最大值的索引位置//语义左上角位于几何左上角,定义为0;//语义左上角位于几何右上角,定义为1;//语义左上角位于几何右下角,定义了2;//语义左上角位于几何左下角,定义为3。textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms\r\n";string msg = "";if (maxIndex == 0) {msg = "语义左上角位于几何左上角";}else if (maxIndex == 1){msg = "语义左上角位于几何右上角";}else if (maxIndex == 2){msg = "语义左上角位于几何右下角";}else if (maxIndex == 3){msg = "语义左上角位于几何左下角";}textBox1.Text += "\r\n" + msg;}private void pictureBox2_DoubleClick(object sender, EventArgs e){Common.ShowNormalImg(pictureBox2.Image);}private void pictureBox1_DoubleClick(object sender, EventArgs e){Common.ShowNormalImg(pictureBox1.Image);}}
}
参考
https://github.com/hpc203/TableDetection
下载
源码下载
其他
C# OpenCvSharp DNN 第一部分-表格边界框检测-CSDN博客
C# OnnxRuntime 第二部分-表格分割-CSDN博客