微信二维码检测的C# 实现——opencvsharp Dnn Caffe推理部署

本文主要是介绍微信二维码检测的C# 实现——opencvsharp Dnn Caffe推理部署,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

早些时候微信二维码开源在opencv, 找码快解码强,最近我研究DataMartix解码库libdmtx的时候,发现它解码还行,找码有点慢,心想何不让深度学习助它一臂之力?于有了这个;

 internal class SSDDetector{private static float CLIP(float x, float x1, float x2) => Math.Max(x1, Math.Min(x, x2));public SSDDetector(string proto_path, string model_path){net_ = CvDnn.ReadNetFromCaffe(proto_path, model_path);}private Net net_;unsafe public List<Mat> Forward(Mat img, int target_width, int target_height){int img_w = img.Cols;int img_h = img.Rows;using Mat input = new();Cv2.Resize(img, input, new Size(target_width, target_height), 0, 0, InterpolationFlags.Cubic);using var blob = CvDnn.BlobFromImage(input, 1.0 / 255, new Size(input.Cols, input.Rows), new Scalar(0f, 0f, 0f),false, false);net_.SetInput(blob, "data");using var prob = net_.Forward("detection_output");List<Mat> point_list = new();// the shape is (1,1,100,7)=>(batch,channel,count,dim)for (int row = 0; row < prob.Size(2); row++){float* prob_score = (float*)prob.Ptr(0, 0, row).ToPointer();// prob_score[0] is not used.// prob_score[1]==1 stands for qrcodeif (prob_score[1] == 1 && prob_score[2] > 1E-5){// add a safe score threshold due to https://github.com/opencv/opencv_contrib/issues/2877// prob_score[2] is the probability of the qrcode, which is not used.var point = new Mat(4, 2, MatType.CV_32FC1);float x0 = CLIP(prob_score[3] * img_w, 0.0f, img_w - 1.0f);float y0 = CLIP(prob_score[4] * img_h, 0.0f, img_h - 1.0f);float x1 = CLIP(prob_score[5] * img_w, 0.0f, img_w - 1.0f);float y1 = CLIP(prob_score[6] * img_h, 0.0f, img_h - 1.0f);point.At<float>(0, 0) = x0;point.At<float>(0, 1) = y0;point.At<float>(1, 0) = x1;point.At<float>(1, 1) = y0;point.At<float>(2, 0) = x1;point.At<float>(2, 1) = y1;point.At<float>(3, 0) = x0;point.At<float>(3, 1) = y1;point_list.Add(point);}}net_.Dispose();return point_list;}}
   private static void Main(string[] args){Mat src = Cv2.ImRead("dm.bmp");int img_w = src.Cols;int img_h = src.Rows;// hard code input sizeint minInputSize = 1600;float resizeRatio = (float)Math.Sqrt(img_w * img_h * 1.0 / (minInputSize * minInputSize));int detect_width = (int)(img_w / resizeRatio);int detect_height = (int)(img_h / resizeRatio);var key = Cv2.WaitKey(1);int fconut = 0;Cv2.NamedWindow("img", WindowFlags.FreeRatio);int windowH = 1200 * img_h / img_w;Cv2.ResizeWindow("img", new(1200, windowH));Cv2.MoveWindow("img", 200, 20);while (key != 113) // q 退出{fconut++; Scalar scalar = Scalar.RandomColor();int thickness = 2;using Mat img = src.Clone();using Mat gray = src.CvtColor(ColorConversionCodes.BGR2GRAY);SSDDetector SSDD = new("detect.prototxt", "detect.caffemodel");var pointslist = SSDD.Forward(gray, detect_width, detect_height);foreach (var points in pointslist){img.Line((int)points.At<float>(0, 0), (int)points.At<float>(0, 1),(int)points.At<float>(1, 0), (int)points.At<float>(1, 1),scalar, thickness);img.Line((int)points.At<float>(1, 0), (int)points.At<float>(1, 1),(int)points.At<float>(2, 0), (int)points.At<float>(2, 1),scalar, thickness);img.Line((int)points.At<float>(2, 0), (int)points.At<float>(2, 1),(int)points.At<float>(3, 0), (int)points.At<float>(3, 1),scalar, thickness);img.Line((int)points.At<float>(3, 0), (int)points.At<float>(3, 1),(int)points.At<float>(0, 0), (int)points.At<float>(0, 1),scalar, thickness);}img.PutText(fconut.ToString(), new(20, 20), HersheyFonts.HersheyDuplex, 1, Scalar.Red);img.PutText("q : quit", new(20, 60), HersheyFonts.HersheyDuplex, 1, Scalar.Red);Cv2.ImShow("img", img);key = Cv2.WaitKey();}}

 原连接:

https://github.com/opencv/opencv_contrib/blob/master/modules/wechat_qrcode/src/detector/ssd_detector.cpp

欢迎加入 opencvsharp 技术交流群: 827888895 进群暗号:shimat

这篇关于微信二维码检测的C# 实现——opencvsharp Dnn Caffe推理部署的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/848984

相关文章

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

利用python实现对excel文件进行加密

《利用python实现对excel文件进行加密》由于文件内容的私密性,需要对Excel文件进行加密,保护文件以免给第三方看到,本文将以Python语言为例,和大家讲讲如何对Excel文件进行加密,感兴... 目录前言方法一:使用pywin32库(仅限Windows)方法二:使用msoffcrypto-too

C#使用StackExchange.Redis实现分布式锁的两种方式介绍

《C#使用StackExchange.Redis实现分布式锁的两种方式介绍》分布式锁在集群的架构中发挥着重要的作用,:本文主要介绍C#使用StackExchange.Redis实现分布式锁的... 目录自定义分布式锁获取锁释放锁自动续期StackExchange.Redis分布式锁获取锁释放锁自动续期分布式

springboot使用Scheduling实现动态增删启停定时任务教程

《springboot使用Scheduling实现动态增删启停定时任务教程》:本文主要介绍springboot使用Scheduling实现动态增删启停定时任务教程,具有很好的参考价值,希望对大家有... 目录1、配置定时任务需要的线程池2、创建ScheduledFuture的包装类3、注册定时任务,增加、删

SpringBoot整合mybatisPlus实现批量插入并获取ID详解

《SpringBoot整合mybatisPlus实现批量插入并获取ID详解》这篇文章主要为大家详细介绍了SpringBoot如何整合mybatisPlus实现批量插入并获取ID,文中的示例代码讲解详细... 目录【1】saveBATch(一万条数据总耗时:2478ms)【2】集合方式foreach(一万条数

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1

PyQt6/PySide6中QTableView类的实现

《PyQt6/PySide6中QTableView类的实现》本文主要介绍了PyQt6/PySide6中QTableView类的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学... 目录1. 基本概念2. 创建 QTableView 实例3. QTableView 的常用属性和方法

PyQt6/PySide6中QTreeView类的实现

《PyQt6/PySide6中QTreeView类的实现》QTreeView是PyQt6或PySide6库中用于显示分层数据的控件,本文主要介绍了PyQt6/PySide6中QTreeView类的实现... 目录1. 基本概念2. 创建 QTreeView 实例3. QTreeView 的常用属性和方法属性

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

pandas中位数填充空值的实现示例

《pandas中位数填充空值的实现示例》中位数填充是一种简单而有效的方法,用于填充数据集中缺失的值,本文就来介绍一下pandas中位数填充空值的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是中位数填充?为什么选择中位数填充?示例数据结果分析完整代码总结在数据分析和机器学习过程中,处理缺失数