目录
说明
算法流程
输出字段定义
效果
模型信息
项目
代码
下载
参考
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