首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

C# OpenCvSharp DNN 实现百度网盘AI大赛-表格检测第2名方案第一部分-表格边界框检测

  • 25-02-19 03:42
  • 3331
  • 12158
blog.csdn.net

目录

说明

效果

模型

项目

代码

frmMain.cs

YoloDet.cs 

参考

下载

其他


说明

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

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

本文使用C# OpenCvSharp DNN 实现百度网盘AI大赛-表格检测第2名方案第一部分-表格边界框检测。

效果

模型

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]
---------------------------------------------------------------

项目

代码

frmMain.cs

using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace OpenCvSharp_DNN_Demo
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        YoloDet obj_detector;

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";

        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;

        Mat image;

        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 obj_model_path = "model/yolo_obj_det.onnx";

            obj_detector = new YoloDet(obj_model_path);

            image_path = "test_img/real5.jpg";
            pictureBox1.Image = new Bitmap(image_path);

        }

        private unsafe void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }
            textBox1.Text = "检测中,请稍等……";
            pictureBox2.Image = null;
            Application.DoEvents();

            Mat src = new Mat(image_path);

            dt1 = DateTime.Now;
            List result = obj_detector.infer(src);
            dt2 = DateTime.Now;

            //绘制
            Mat draw_img = src.Clone();
            for (int i = 0; i < result.Count; i++)
            {
                Rect r = Rect.FromLTRB(result[i].xmin, result[i].ymin, result[i].xmax, result[i].ymax);

                Cv2.PutText(draw_img, $"table:{result[0].score:P0}", new OpenCvSharp.Point(r.TopLeft.X, r.TopLeft.Y - 10), HersheyFonts.HersheySimplex, 8, Scalar.Red, 8);
                Cv2.Rectangle(draw_img, r, Scalar.Red, thickness: 8);

                //裁剪,保存,为下一步检测做准备
                Mat crop_img = new Mat(src, r);
                Cv2.ImWrite(i + ".jpg", crop_img);
            }
            pictureBox2.Image = new Bitmap(draw_img.ToMemoryStream());
            textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
        }
       
        private void pictureBox2_DoubleClick(object sender, EventArgs e)
        {
            Common.ShowNormalImg(pictureBox2.Image);
        }
        
        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            Common.ShowNormalImg(pictureBox1.Image);
        }
    }
}
 

  1. using OpenCvSharp;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. namespace OpenCvSharp_DNN_Demo
  7. {
  8. public partial class frmMain : Form
  9. {
  10. public frmMain()
  11. {
  12. InitializeComponent();
  13. }
  14. YoloDet obj_detector;
  15. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
  16. string image_path = "";
  17. DateTime dt1 = DateTime.Now;
  18. DateTime dt2 = DateTime.Now;
  19. Mat image;
  20. private void button1_Click(object sender, EventArgs e)
  21. {
  22. OpenFileDialog ofd = new OpenFileDialog();
  23. ofd.Filter = fileFilter;
  24. if (ofd.ShowDialog() != DialogResult.OK) return;
  25. pictureBox1.Image = null;
  26. pictureBox2.Image = null;
  27. textBox1.Text = "";
  28. image_path = ofd.FileName;
  29. pictureBox1.Image = new Bitmap(image_path);
  30. image = new Mat(image_path);
  31. }
  32. private void Form1_Load(object sender, EventArgs e)
  33. {
  34. string obj_model_path = "model/yolo_obj_det.onnx";
  35. obj_detector = new YoloDet(obj_model_path);
  36. image_path = "test_img/real5.jpg";
  37. pictureBox1.Image = new Bitmap(image_path);
  38. }
  39. private unsafe void button2_Click(object sender, EventArgs e)
  40. {
  41. if (image_path == "")
  42. {
  43. return;
  44. }
  45. textBox1.Text = "检测中,请稍等……";
  46. pictureBox2.Image = null;
  47. Application.DoEvents();
  48. Mat src = new Mat(image_path);
  49. dt1 = DateTime.Now;
  50. List<Bbox> result = obj_detector.infer(src);
  51. dt2 = DateTime.Now;
  52. //绘制
  53. Mat draw_img = src.Clone();
  54. for (int i = 0; i < result.Count; i++)
  55. {
  56. Rect r = Rect.FromLTRB(result[i].xmin, result[i].ymin, result[i].xmax, result[i].ymax);
  57. Cv2.PutText(draw_img, $"table:{result[0].score:P0}", new OpenCvSharp.Point(r.TopLeft.X, r.TopLeft.Y - 10), HersheyFonts.HersheySimplex, 8, Scalar.Red, 8);
  58. Cv2.Rectangle(draw_img, r, Scalar.Red, thickness: 8);
  59. //裁剪,保存,为下一步检测做准备
  60. Mat crop_img = new Mat(src, r);
  61. Cv2.ImWrite(i + ".jpg", crop_img);
  62. }
  63. pictureBox2.Image = new Bitmap(draw_img.ToMemoryStream());
  64. textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
  65. }
  66. private void pictureBox2_DoubleClick(object sender, EventArgs e)
  67. {
  68. Common.ShowNormalImg(pictureBox2.Image);
  69. }
  70. private void pictureBox1_DoubleClick(object sender, EventArgs e)
  71. {
  72. Common.ShowNormalImg(pictureBox1.Image);
  73. }
  74. }
  75. }

YoloDet.cs 

  1. using OpenCvSharp;
  2. using OpenCvSharp.Dnn;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. namespace OpenCvSharp_DNN_Demo
  7. {
  8. internal class YoloDet
  9. {
  10. Net model;
  11. int[] resize_shape = new int[2] { 928, 928 };
  12. public YoloDet(string model_path)
  13. {
  14. model = CvDnn.ReadNetFromOnnx(model_path);
  15. }
  16. unsafe public List<Bbox> infer(Mat srcimg, float score = 0.4f)
  17. {
  18. int ori_h = srcimg.Rows;
  19. int ori_w = srcimg.Cols;
  20. img_preprocess
  21. Mat img;
  22. int new_w = 0;
  23. int new_h = 0;
  24. int left = 0;
  25. int top = 0;
  26. img = Common.ResizePad(srcimg, resize_shape[0], ref new_w, ref new_h, ref left, ref top);
  27. //Cv2.ImWrite("0.jpg", img);
  28. img.ConvertTo(img, MatType.CV_32FC3, 1.0 / 255.0);
  29. Mat blob = CvDnn.BlobFromImage(img);
  30. model.SetInput(blob);
  31. //模型推理,读取推理结果
  32. Mat[] outs = new Mat[1] { new Mat() };
  33. string[] outBlobNames = model.GetUnconnectedOutLayersNames().ToArray();
  34. model.Forward(outs, outBlobNames);
  35. img_postprocess
  36. float x_factor = (float)ori_w / new_w;
  37. float y_factor = (float)ori_h / new_h;
  38. List<Rect> boxes = new List<Rect>();
  39. List<float> scores = new List<float>();
  40. int rows = outs[0].Size(2);
  41. //将推理结果转为float数据类型
  42. //5, 17661
  43. Mat result_mat = new Mat(5, 17661, MatType.CV_32F, outs[0].Data);
  44. result_mat = result_mat.T();
  45. //string str = result_mat.Dump();
  46. for (int i = 0; i < rows; i++)
  47. {
  48. float* ptr = (float*)(result_mat.Ptr(i).ToPointer());
  49. float max_score = ptr[4];
  50. //float max_score = result_mat.At<float>(i, 4);
  51. if (max_score >= score)
  52. {
  53. //At效率不高
  54. //float x1 = result_mat.At<float>(i, 0);
  55. //float y2 = result_mat.At<float>(i, 1);
  56. //float w2 = result_mat.At<float>(i, 2);
  57. //float h2 = result_mat.At<float>(i, 3);
  58. float x = ptr[0];
  59. float y = ptr[1];
  60. float w = ptr[2];
  61. float h = ptr[3];
  62. int xmin = Math.Max((int)((x - w / 2 - left) * x_factor), 0);
  63. int ymin = Math.Max((int)((y - h / 2 - top) * y_factor), 0);
  64. boxes.Add(new Rect(xmin, ymin, (int)(w * x_factor), (int)(h * y_factor)));
  65. scores.Add(max_score);
  66. }
  67. }
  68. int[] indices;
  69. CvDnn.NMSBoxes(boxes, scores, score, 0.4f, out indices);
  70. int num_keep = indices.Length;
  71. List<Bbox> bboxes = new List<Bbox>();
  72. for (int i = 0; i < num_keep; i++)
  73. {
  74. int ind = indices[i];
  75. bboxes.Add(new Bbox(boxes[ind].X, boxes[ind].Y, Math.Min(boxes[ind].X + boxes[ind].Width, ori_w - 1), Math.Min(boxes[ind].Y + boxes[ind].Height, ori_h - 1), scores[ind]));
  76. }
  77. return bboxes;
  78. }
  79. }
  80. }
  81. /*
  82. Model Properties
  83. -------------------------
  84. date:2024-10-28T13:52:42.181333
  85. description:Ultralytics YOLO11l model trained on coco.yaml
  86. author:Ultralytics
  87. version:8.3.23
  88. task:detect
  89. license:AGPL-3.0 License (https://ultralytics.com/license)
  90. docs:https://docs.ultralytics.com
  91. stride:32
  92. batch:1
  93. imgsz:[928, 928]
  94. names:{0: 'table'}
  95. ---------------------------------------------------------------
  96. Inputs
  97. -------------------------
  98. name:images
  99. tensor:Float[1, 3, 928, 928]
  100. ---------------------------------------------------------------
  101. Outputs
  102. -------------------------
  103. name:output0
  104. tensor:Float[1, 5, 17661]
  105. ---------------------------------------------------------------
  106. */

参考

  1. https://github.com/hpc203/TableDetection
  2. https://aistudio.baidu.com/projectdetail/5398861?searchKeyword=%E8%A1%A8%E6%A0%BC%E6%A3%80%E6%B5%8B%E5%A4%A7%E8%B5%9B&searchTab=ALL

下载

源码下载

其他

C# OnnxRuntime 第二部分-表格分割-CSDN博客

C# OpenCvSharp DNN 第三部分-表格方向识别-CSDN博客

天天代码码天天
微信公众号
.NET 人工智能实践
注:本文转载自blog.csdn.net的天天代码码天天的文章"https://lw112190.blog.csdn.net/article/details/144487387"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top