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 矫正后的子图区域像素值

所有字段第一个维度的长度相等且一一对应,为图片中票证的数量。比如polygons0、scores0、labels0、layout0、output_imgs0表示第一个子图的坐标、置信度、方向、是否复印件、拉平后的子图。

效果

模型信息

Model Properties



Inputs


name:input

tensor:Float1, 3, 768, 768


Outputs


name:output

tensor:Float1, 1, 192, 192

name:286

tensor:Float1, 4, 192, 192

name:289

tensor:Float1, 2, 192, 192

name:292

tensor:Float1, 8, 192, 192

name:295

tensor:Float1, 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

相关推荐
东鸿电子Eastron7 分钟前
工厂节能降本落地指南:从精准能源计量搭建数字化管理基础
大数据·人工智能·能源·智能电表·能耗管理
师夷长技以自愈14 分钟前
手搓 AI Agent 第 6 篇:先别动刀——Plan Mode 让 AI 报完方案才动手
人工智能
Sincerelyplz15 分钟前
Voice Agent 实践与落地(二):从唤醒到多轮对话,拆解实时语音链路
人工智能
guuguo17 分钟前
Gemini 4 屠榜后连夜切到 Antigravity,顺手用一行命令搞定全中文界面与思考
人工智能·开源·github
码云社区17 分钟前
能碳管理系统开发要关注什么?能源数据、统计口径与AI分析的衔接
java·人工智能·ai·能源·能源管理系统·能碳管理系统·能碳管理系统源码
科研小刘带你玩学术21 分钟前
【科研快讯】OpenAI推出ChatGPT Wallet:AI Agent不再只会推荐商品,还能替用户完成支付
人工智能·数字经济·智能体·科技前沿·aiagent·ai支付·智能商务
飞猫的边缘AI28 分钟前
SoC和SoC FPGA的野心与市场渗透
人工智能·科技·soc芯片·边缘ai
泯泷29 分钟前
AI 社交的真问题:助理拥有“我”,平台拥有“别人”
人工智能·llm·agent
@嵌入式扫地僧30 分钟前
边缘 AI 面试实战:模型剪枝核心方法
人工智能·结构化剪枝·模型剪枝·边缘 ai·嵌入式 ai 部署
Yuzhiyuxia1 小时前
【AI Agent面试系列】AI Agent实战设计
人工智能·面试·职场和发展