百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术教程 > 正文

C# OpenCvSharp 部署表格检测

csdh11 2025-02-10 11:58 24 浏览

说明

百度网盘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 = ;
pictureBox2.Image = ;
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 != ) image.Dispose();
if (out_img1 != ) out_img1.Dispose();
if (out_img2 != ) out_img2.Dispose();
if (out_img3 != ) out_img3.Dispose();
if (pictureBox1.Image != ) 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();

int res = tableDet(engine, image.CvPtr, msg, ref out_imgs_size, out_img1.CvPtr, out_img2.CvPtr, out_img3.CvPtr);
if (res == 0)
{
stopwatch.Stop();
double costTime = stopwatch.Elapsed.TotalMilliseconds;
if (out_imgs_size >= 1)
{
pictureBox2.Image = new Bitmap(out_img1.ToMemoryStream());
}

if (out_imgs_size >= 2)
{
Cv2.ImShow("2", out_img2);
}

if (out_imgs_size >= 3)
{
Cv2.ImShow("3", out_img3);
}

textBox1.Text = $"耗时:{costTime:F2}ms";
}
else
{
textBox1.Text = "识别失败";
}
button2.Enabled = true;
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
destroy(engine);
}

private void button3_Click(object sender, EventArgs e)
{
if (pictureBox2.Image == )
{
return;
}
Bitmap output = new Bitmap(pictureBox2.Image);
var sdf = new SaveFileDialog();
sdf.Title = "保存";
sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp";
if (sdf.ShowDialog() == DialogResult.OK)
{
switch (sdf.FilterIndex)
{
case 1:
{
output.Save(sdf.FileName, ImageFormat.Jpeg);
break;
}
case 2:
{
output.Save(sdf.FileName, ImageFormat.Png);
break;
}
case 3:
{
output.Save(sdf.FileName, ImageFormat.Bmp);
break;
}
}
MessageBox.Show("保存成功,位置:" + sdf.FileName);
}

}
}
}

参考

https://github.com/hpc203/TableDetection


相关推荐

Github霸榜的SpringBoot全套学习教程,从入门到实战,内容超详细

前言...

SpringBoot+LayUI后台管理系统开发脚手架

源码获取方式:关注,转发之后私信回复【源码】即可免费获取到!项目简介本项目本着避免重复造轮子的原则,建立一套快速开发JavaWEB项目(springboot-mini),能满足大部分后台管理系统基础开...

Spring Boot+Vue全栈开发实战,中文版高清PDF资源

SpringBoot+Vue全栈开发实战,中文高清PDF资源,需要的可以私我:)SpringBoot致力于简化开发配置并为企业级开发提供一系列非业务性功能,而Vue则采用数据驱动视图的方式将程序...

2021年超详细的java学习路线总结—纯干货分享

本文整理了java开发的学习路线和相关的学习资源,非常适合零基础入门java的同学,希望大家在学习的时候,能够节省时间。纯干货,良心推荐!第一阶段:Java基础...

探秘Spring Cache:让Java应用飞起来的秘密武器

探秘SpringCache:让Java应用飞起来的秘密武器在当今快节奏的软件开发环境中,性能优化显得尤为重要。SpringCache作为Spring框架的一部分,为我们提供了强大的缓存管理能力,让...

3,从零开始搭建SSHM开发框架(集成Spring MVC)

目录本专题博客已共享在(这个可能会更新的稍微一些)https://code.csdn.net/yangwei19680827/maven_sshm_blog...

Spring Boot中如何使用缓存?超简单

SpringBoot中的缓存可以减少从数据库重复获取数据或执行昂贵计算的需要,从而显著提高应用程序的性能。SpringBoot提供了与各种缓存提供程序的集成,您可以在应用程序中轻松配置和使用缓...

我敢保证,全网没有再比这更详细的Java知识点总结了,送你啊

接下来你看到的将是全网最详细的Java知识点总结,全文分为三大部分:Java基础、Java框架、Java+云数据小编将为大家仔细讲解每大部分里面的详细知识点,别眨眼,从小白到大佬、零基础到精通,你绝...

1,从零开始搭建SSHM开发框架(环境准备)

目录本专题博客已共享在https://code.csdn.net/yangwei19680827/maven_sshm_blog1,从零开始搭建SSHM开发框架(环境准备)...

做一个适合二次开发的低代码平台,把程序员从curd中解脱出来-1

干程序员也有好长时间了,大多数时间都是在做curd。现在想做一个通用的curd平台直接将我们解放出来;把核心放在业务处理中。用过代码生成器,在数据表设计好之后使用它就可以生成需要的controller...

设计一个高性能Java Web框架(java做网站的框架)

设计一个高性能JavaWeb框架在当今互联网高速发展的时代,构建高性能的JavaWeb框架对于提升用户体验至关重要。本文将从多个角度探讨如何设计这样一个框架,让我们一起进入这段充满挑战和乐趣的旅程...

【推荐】强&牛!一款开源免费的功能强大的代码生成器系统!

今天,给大家推荐一个代码生成器系统项目,这个项目目前收获了5.3KStar,个人觉得不错,值得拿出来和大家分享下。这是我目前见过最好的代码生成器系统项目。功能完整,代码结构清晰。...

Java面试题及答案总结(2025版持续更新)

大家好,我是Java面试分享最近很多小伙伴在忙着找工作,给大家整理了一份非常全面的Java面试场景题及答案。...

Java开发网站架构演变过程-从单体应用到微服务架构详解

Java开发网站架构演变过程,到目前为止,大致分为5个阶段,分别为单体架构、集群架构、分布式架构、SOA架构和微服务架构。下面玄武老师来给大家详细介绍下这5种架构模式的发展背景、各自优缺点以及涉及到的...

本地缓存GuavaCache(一)(guava本地缓存原理)

在并发量、吞吐量越来越大的情况下往往是离不开缓存的,使用缓存能减轻数据库的压力,临时存储数据。根据不同的场景选择不同的缓存,分布式缓存有Redis,Memcached、Tair、EVCache、Aer...