专栏名称: dotNET跨平台
专注于.NET Core的技术传播。在这里你可以谈微软.NET,Mono的跨平台开发技术。在这里可以让你的.NET项目有新的思路,不局限于微软的技术栈,横跨Windows,Linux 主流平台
目录
相关文章推荐
半导体行业联盟  ·  经国务院批准,实施出口管制 ·  2 天前  
半导体行业联盟  ·  中方:性质恶劣,已起诉美国! ·  2 天前  
半导体行业联盟  ·  DeepSeek日活突破2000万,创造“中 ... ·  2 天前  
半导体行业联盟  ·  美宣布对中国商品加征10%关税 ·  4 天前  
半导体行业联盟  ·  世界如何看待DeepSeek ·  4 天前  
51好读  ›  专栏  ›  dotNET跨平台

C# OpenCvSharp 部署表格检测

dotNET跨平台  · 公众号  ·  · 2025-01-22 08:00

正文

说明

百度网盘AI大赛-表格检测的第2名方案。

该算法包含表格边界框检测、表格分割和表格方向识别三个部分,首先,ppyoloe-plus-x 对边界框进行预测,并对置信度较高的表格边界框(box)进行裁剪。裁剪后的单个表格实例会送入到DBNet中进行语义分割,分割结果通过opencv轮廓处理获得表格关键点(point)。之后,我们根据DBNet计算的关键点在裁剪后的单个表格实例上绘制表格边界。最后,PP-LCNet结合表格边界先验和表格实例图像,对表格的方向进行预测,并根据之前定义的几何轮廓点与语义轮廓点的对应关系,将几何轮廓点映射为语义轮廓点。

效果



模型信息

paddle_cls.onnx

Model Properties
-------------------------
---------------------------------------------------------------

Inputs
-------------------------
name:input
tensor:Float[-1, 3, 624, 624]
---------------------------------------------------------------

Outputs
-------------------------
name:linear_1.tmp_1
tensor:Float[-1, 4]
---------------------------------------------------------------

yolo_edge_det.onnx

Model Properties
-------------------------
date:2024-10-28T08:16:43.725877
description:Ultralytics YOLO11l-seg model trained on coco-seg.yaml
author:Ultralytics
version:8.3.23
task:segment
license:AGPL-3.0 License (https://ultralytics.com/license)
docs:https://docs.ultralytics.com
stride:32
batch:1
imgsz:[800, 800]
names:{0: 'table'}
---------------------------------------------------------------

Inputs
-------------------------
name:images
tensor:Float[1, 3, 800, 800]
---------------------------------------------------------------

Outputs
-------------------------
name:output0
tensor:Float[1, 37, 13125]
name:output1
tensor:Float[1, 32, 200, 200]
---------------------------------------------------------------

yolo_obj_det.onnx

Model Properties
-------------------------
date:2024-10-28T13:52:42.181333
description:Ultralytics YOLO11l model trained on coco.yaml
author:Ultralytics
version:8.3.23
task:detect
license:AGPL-3.0 License (https://ultralytics.com/license)
docs:https://docs.ultralytics.com
stride:32
batch:1
imgsz:[928, 928]
names:{0: 'table'}
---------------------------------------------------------------

Inputs
-------------------------
name:images
tensor:Float[1, 3, 928, 928]
---------------------------------------------------------------

Outputs
-------------------------
name:output0
tensor:Float[1, 5, 17661]
---------------------------------------------------------------

项目

C++封装DLL,C#调用

代码

using OpenCvSharp;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace TableDetection
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Stopwatch stopwatch = new Stopwatch();
        Mat image;
        string image_path;
        string startupPath;
        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        const string DllName = "TableDetectionSharp.dll";
        IntPtr engine;
        /*
        //初始化
        extern "C" _declspec(dllexport) int __cdecl  init(void** engine, char* obj_model_path, char*  edge_model_path, char* cls_model_path, char* msg);

        //tableDet
        extern "C" _declspec(dllexport) int __cdecl  tableDet(void* engine, Mat* image, char* msg, int* out_imgs_size, Mat* out_img1, Mat* out_img2, Mat* out_img3);

        //释放
        extern "C" _declspec(dllexport) void __cdecl destroy(void* engine);
         */

        [DllImport(DllName, EntryPoint = "init", CallingConvention = CallingConvention.Cdecl)]
        internal extern static int init(ref IntPtr engine, string obj_model_path, string edge_model_path, string cls_model_path, StringBuilder msg);

        [DllImport(DllName, EntryPoint = "tableDet", CallingConvention = CallingConvention.Cdecl)]
        internal extern static int tableDet(IntPtr engine, IntPtr image, StringBuilder msg, ref int out_imgs_size, IntPtr out_img1, IntPtr out_img2, IntPtr out_img3);

        [DllImport(DllName, EntryPoint = "destroy", CallingConvention = CallingConvention.Cdecl)]
        internal extern static int destroy(IntPtr engine);

        private void Form1_Load(object sender, EventArgs e)
        {
            startupPath = Application.StartupPath;

            string obj_model_path = startupPath + "\\model\\yolo_obj_det.onnx";
            string edge_model_path = startupPath + "\\model\\yolo_edge_det.onnx";
            string cls_model_path = startupPath + "\\model\\paddle_cls.onnx";

            StringBuilder msg = new StringBuilder(512);

            int res = init(ref engine, obj_model_path, edge_model_path, cls_model_path, msg);
            if (res == -1)
            {
                MessageBox.Show(msg.ToString());
                return;
            }
            else
            {
                Console.WriteLine(msg.ToString());
            }
            image_path = startupPath + "\\test_img\\real5.jpg";
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);
        }

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

        Mat out_img1;
        Mat out_img2;
        Mat out_img3;

        private void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

            button2.Enabled = false;

            Application.DoEvents();

            Cv2.DestroyAllWindows();
            if (image != null) image.Dispose();
            if (out_img1 != null) out_img1.Dispose();
            if (out_img2 != null) out_img2.Dispose();
            if (out_img3 != null) out_img3.Dispose();
            if (pictureBox1.Image != null) pictureBox1.Image.Dispose();

            StringBuilder msg = new StringBuilder(512);
            int out_imgs_size = 0;
            image = new Mat(image_path);
            out_img1 = new Mat();
            out_img2 = new Mat();
            out_img3 = new Mat();

            stopwatch.Restart();





请到「今天看啥」查看全文

推荐文章
半导体行业联盟  ·  经国务院批准,实施出口管制
2 天前
半导体行业联盟  ·  中方:性质恶劣,已起诉美国!
2 天前
半导体行业联盟  ·  DeepSeek日活突破2000万,创造“中国速度”
2 天前
半导体行业联盟  ·  美宣布对中国商品加征10%关税
4 天前
半导体行业联盟  ·  世界如何看待DeepSeek
4 天前
采采  ·  那些不可描述的造句段子
8 年前
加措上师语录  ·  幸福来源于哪里?
7 年前