C# OpenCvSharp 部署读光-票证检测矫正模型(cv_resnet18_card_correction)

目录

说明

算法流程

输出字段定义

效果

模型信息

项目

代码

下载

参考


C# OpenCvSharp 部署读光-票证检测矫正模型(cv_resnet18_card_correction)

说明

地址:https://modelscope.cn/models/iic/cv_resnet18_card_correction

票证检测矫正模型在实际生活中有着广泛的需求,例如信息抽取、图像质量判断、证件扫描、票据审计等领等场景,可以大幅提高工作效率和准确性。本次 读光团队 开源了商用票证检测矫正模型,基于海量的真实数据训练,可以从容应对多种复杂场景的票证检测矫正任务,该模型具有以下优点:

  • 支持任意角度、多卡证票据等混贴场景,同时检测输入图像任意角度的多个子图区域;
  • 基于海量真实数据训练,效果满足国内常见的卡证票据的检测矫正需求;
  • 支持子图区域复印件判断、四方向判断,准确率高达 99%;
  • 矫正效果、推理速度远高于 modelScope 同类模型,详见本文测试报告。

输入图片,基于 Resnet18-FPN 提取特征后,在 1/4 尺寸处通过三条分支分别识别出票证的中心点、偏移量(中心点到4个顶点距离)、中心点偏移量(为了得到精准的中心点),即可解码数出票证区域的四边形框;再用透视变换将票证拉平得到矫正后的票证信息;与此同时,分类分支识别出子图朝向,用于而切割的子图转正。

算法流程

测试时的主要预处理和后处理如下:

  • 图像预处理:将输入图片按照比例缩放,长边 Resize 到 768,短边 Pad 到长短边相等,同时有减均值、除方差等归一化操作。
  • 模型卡证区域检测:对输入图像中的卡证票据区域进行检测,并对 卡证票据的方向复印件类型 进行分类;
  • 后处理矫正:根据 卡证区域检测框卡证票据的方向 对卡证区域进行透视变化,并转为水平方向;

输出字段定义

字段名称 说明
polygons 框检得到的任意四边形四个顶点,依次为左上、右上、右下、左下
scores 框检置信度,标识检测的可行度,值域 0 到 1 之间
labels 卡证方向分类,枚举类型,0、1、2、3 依次表示卡证顺时针旋转 90度、180度、270度
layout 复印件分类,枚举类型,0 表示非复印件,1 表示复印件
output_imgs 矫正后的子图区域像素值

所有字段第一个维度的长度相等且一一对应,为图片中票证的数量。比如polygons[0]、scores[0]、labels[0]、layout[0]、output_imgs[0]表示第一个子图的坐标、置信度、方向、是否复印件、拉平后的子图。

效果

模型信息

Model Properties



Inputs


name:input

tensor:Float[1, 3, 768, 768]


Outputs


name:output

tensor:Float[1, 1, 192, 192]

name:286

tensor:Float[1, 4, 192, 192]

name:289

tensor:Float[1, 2, 192, 192]

name:292

tensor:Float[1, 8, 192, 192]

name:295

tensor:Float[1, 2, 192, 192]


项目

代码

复制代码
using OpenCvSharp;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;


namespace C__OpenCvSharp_DNN_卡证检测矫正
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

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

        //校准
        extern "C" _declspec(dllexport) int __cdecl  correction(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 model_path, StringBuilder msg);

        [DllImport(DllName, EntryPoint = "correction", CallingConvention = CallingConvention.Cdecl)]
        internal extern static int correction(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;

            model_path = startupPath + "\\model\\cv_resnet18_card_correction.onnx";

            StringBuilder msg = new StringBuilder(512);

            int res = init(ref engine, model_path, msg);
            if (res == -1)
            {
                MessageBox.Show(msg.ToString());
                return;
            }
            else
            {
                Console.WriteLine(msg.ToString());
            }
            image_path = startupPath + "\\test_img\\1.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);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            destroy(engine);
        }

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

            int res = correction(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;
        }
    }
}

下载

源码下载

参考

https://github.com/hpc203/cv_resnet18_card_correction-opencv-dnn

相关推荐
呆头鹅AI工作室4 分钟前
[2025CVPR]SEEN-DA:基于语义熵引导的领域感知注意力机制
人工智能·深度学习·机器学习
吴佳浩7 分钟前
Python入门指南-AI番外-MCP完整教程:从零开始学会Model Context Protocol
人工智能·python·mcp
加油吧zkf19 分钟前
目标检测新纪元:DETR到Mamba实战解析
图像处理·人工智能·python·目标检测·分类
西柚小萌新25 分钟前
【深度学习:进阶篇】--4.3.seq2seq与Attention机制
人工智能·深度学习
求索小沈26 分钟前
ubuntu22.04 安装cuda cudnn
人工智能·深度学习
Dm_dotnet28 分钟前
C#:wpf ui 4.0 是如何实现页面导航的?
c#
阿里云大数据AI技术35 分钟前
AI搜索 MCP最佳实践
数据库·人工智能·搜索引擎
大千AI助手36 分钟前
蒙特卡洛方法:随机抽样的艺术与科学
人工智能·机器学习·贝叶斯·概率·蒙特卡洛·随机
山顶望月川38 分钟前
并行科技MaaS平台支持文心4.5系列开源模型调用
人工智能·机器学习·编辑器
顾道长生'1 小时前
(Arxiv-2024)自回归模型优于扩散:Llama用于可扩展的图像生成
计算机视觉·数据挖掘·llama·自回归模型·多模态生成与理解