微信二维码检测的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 Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

C#中lock关键字的使用小结

《C#中lock关键字的使用小结》在C#中,lock关键字用于确保当一个线程位于给定实例的代码块中时,其他线程无法访问同一实例的该代码块,下面就来介绍一下lock关键字的使用... 目录使用方式工作原理注意事项示例代码为什么不能lock值类型在C#中,lock关键字用于确保当一个线程位于给定实例的代码块中时