C# OpenVINO 直接读取百度模型实现印章检测

2023-12-15 04:15

本文主要是介绍C# OpenVINO 直接读取百度模型实现印章检测,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

效果

模型信息

项目

代码

下载

其他


C# OpenVINO 直接读取百度模型实现印章检测

效果

模型信息

Inputs
-------------------------
name:scale_factor
tensor:F32[?, 2]

name:image
tensor:F32[?, 3, 608, 608]

name:im_shape
tensor:F32[?, 2]

---------------------------------------------------------------

Outputs
-------------------------
name:multiclass_nms3_0.tmp_0
tensor:F32[?, 6]

name:multiclass_nms3_0.tmp_2
tensor:I32[?]

---------------------------------------------------------------

项目

代码

using OpenCvSharp;
using Sdcb.OpenVINO;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
 
namespace OpenVINO_Det_物体检测
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        string startupPath;
        string model_path;
        Mat src;
        string[] dicts;
 
        StringBuilder sb = new StringBuilder();
 
        float confidence = 0.75f;
 
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox1.Image = null;
            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            textBox1.Text = "";
            src = new Mat(image_path);
            pictureBox2.Image = null;
        }
 
        unsafe private void button2_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
            {
                return;
            }
 
            pictureBox2.Image = null;
            textBox1.Text = "";
            sb.Clear();
 
            src = new Mat(image_path);
            Mat result_image = src.Clone();
 
            model_path = "model/model.pdmodel";
            Model rawModel = OVCore.Shared.ReadModel(model_path);
 
            int inpHeight = 608;
            int inpWidth = 608;
 
            var ad = OVCore.Shared.AvailableDevices;
            Console.WriteLine("可用设备");
            foreach (var item in ad)
            {
                Console.WriteLine(item);
            }
 
            CompiledModel cm = OVCore.Shared.CompileModel(rawModel, "CPU");
            InferRequest ir = cm.CreateInferRequest();
 
            Stopwatch stopwatch = new Stopwatch();
 
            Shape inputShape = new Shape(1, 608, 608);
            Size2f sizeRatio = new Size2f(1f * src.Width / inputShape[2], 1f * src.Height / inputShape[1]);
            Cv2.CvtColor(src, src, ColorConversionCodes.BGR2RGB);
 
            Point2f scaleRate = new Point2f(1f * inpWidth / src.Width, 1f * inpHeight / src.Height);
 
            Cv2.Resize(src, src, new OpenCvSharp.Size(), scaleRate.X, scaleRate.Y);
 
            Common.Normalize(src);
 
            float[] input_tensor_data = Common.ExtractMat(src);
 
            /*
             scale_factor   1,2
             image          1,3,608,608
             im_shape       1,2 
             */
            Tensor input_scale_factor = Tensor.FromArray(new float[] { scaleRate.Y, scaleRate.X }, new Shape(1, 2));
            Tensor input_image = Tensor.FromArray(input_tensor_data, new Shape(1, 3, 608, 608));
            Tensor input_im_shape = Tensor.FromArray(new float[] { 608, 608 }, new Shape(1, 2));
 
            ir.Inputs[0] = input_scale_factor;
            ir.Inputs[1] = input_image;
            ir.Inputs[2] = input_im_shape;
 
            double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Restart();
 
            ir.Run();
 
            double inferTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Restart();
 
            Tensor output_0 = ir.Outputs[0];
 
            int num = (int)output_0.Shape.Dimensions[0];
 
            float[] output_0_array = output_0.GetData<float>().ToArray();
 
            for (int j = 0; j < num; j++)
            {
                int num12 = (int)Math.Round(output_0_array[j * 6]);
                float score = output_0_array[1 + j * 6];
 
                if (score > this.confidence)
                {
                    int num13 = (int)(output_0_array[2 + j * 6]);
                    int num14 = (int)(output_0_array[3 + j * 6]);
                    int num15 = (int)(output_0_array[4 + j * 6]);
                    int num16 = (int)(output_0_array[5 + j * 6]);
 
                    string ClassName = dicts[num12];
                    Rect r = Rect.FromLTRB(num13, num14, num15, num16);
                    sb.AppendLine($"{ClassName}:{score:P0}");
                    Cv2.PutText(result_image, $"{ClassName}:{score:P0}", new OpenCvSharp.Point(r.TopLeft.X, r.TopLeft.Y - 10), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
                    Cv2.Rectangle(result_image, r, Scalar.Red, thickness: 2);
                }
            }
 
            double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Stop();
            double totalTime = preprocessTime + inferTime + postprocessTime;
 
            pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
 
            sb.AppendLine($"Preprocess: {preprocessTime:F2}ms");
            sb.AppendLine($"Infer: {inferTime:F2}ms");
            sb.AppendLine($"Postprocess: {postprocessTime:F2}ms");
            sb.AppendLine($"Total: {totalTime:F2}ms");
 
            textBox1.Text = sb.ToString();
 
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            startupPath = Application.StartupPath;
 
            string classer_path = "lable.txt";
            List<string> str = new List<string>();
            StreamReader sr = new StreamReader(classer_path);
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                str.Add(line);
            }
            dicts = str.ToArray();
 
            image_path = "test_img/1.jpg";
            pictureBox1.Image = new Bitmap(image_path);
        }
    }
}

using OpenCvSharp;
using Sdcb.OpenVINO;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;namespace OpenVINO_Det_物体检测
{public partial class Form1 : Form{public Form1(){InitializeComponent();}string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";string image_path = "";string startupPath;string model_path;Mat src;string[] dicts;StringBuilder sb = new StringBuilder();float confidence = 0.75f;private void button1_Click(object sender, EventArgs e){OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = fileFilter;if (ofd.ShowDialog() != DialogResult.OK) return;pictureBox1.Image = null;image_path = ofd.FileName;pictureBox1.Image = new Bitmap(image_path);textBox1.Text = "";src = new Mat(image_path);pictureBox2.Image = null;}unsafe private void button2_Click(object sender, EventArgs e){if (pictureBox1.Image == null){return;}pictureBox2.Image = null;textBox1.Text = "";sb.Clear();src = new Mat(image_path);Mat result_image = src.Clone();model_path = "model/model.pdmodel";Model rawModel = OVCore.Shared.ReadModel(model_path);int inpHeight = 608;int inpWidth = 608;var ad = OVCore.Shared.AvailableDevices;Console.WriteLine("可用设备");foreach (var item in ad){Console.WriteLine(item);}CompiledModel cm = OVCore.Shared.CompileModel(rawModel, "CPU");InferRequest ir = cm.CreateInferRequest();Stopwatch stopwatch = new Stopwatch();Shape inputShape = new Shape(1, 608, 608);Size2f sizeRatio = new Size2f(1f * src.Width / inputShape[2], 1f * src.Height / inputShape[1]);Cv2.CvtColor(src, src, ColorConversionCodes.BGR2RGB);Point2f scaleRate = new Point2f(1f * inpWidth / src.Width, 1f * inpHeight / src.Height);Cv2.Resize(src, src, new OpenCvSharp.Size(), scaleRate.X, scaleRate.Y);Common.Normalize(src);float[] input_tensor_data = Common.ExtractMat(src);/*scale_factor   1,2image          1,3,608,608im_shape       1,2 */Tensor input_scale_factor = Tensor.FromArray(new float[] { scaleRate.Y, scaleRate.X }, new Shape(1, 2));Tensor input_image = Tensor.FromArray(input_tensor_data, new Shape(1, 3, 608, 608));Tensor input_im_shape = Tensor.FromArray(new float[] { 608, 608 }, new Shape(1, 2));ir.Inputs[0] = input_scale_factor;ir.Inputs[1] = input_image;ir.Inputs[2] = input_im_shape;double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;stopwatch.Restart();ir.Run();double inferTime = stopwatch.Elapsed.TotalMilliseconds;stopwatch.Restart();Tensor output_0 = ir.Outputs[0];int num = (int)output_0.Shape.Dimensions[0];float[] output_0_array = output_0.GetData<float>().ToArray();for (int j = 0; j < num; j++){int num12 = (int)Math.Round(output_0_array[j * 6]);float score = output_0_array[1 + j * 6];if (score > this.confidence){int num13 = (int)(output_0_array[2 + j * 6]);int num14 = (int)(output_0_array[3 + j * 6]);int num15 = (int)(output_0_array[4 + j * 6]);int num16 = (int)(output_0_array[5 + j * 6]);string ClassName = dicts[num12];Rect r = Rect.FromLTRB(num13, num14, num15, num16);sb.AppendLine($"{ClassName}:{score:P0}");Cv2.PutText(result_image, $"{ClassName}:{score:P0}", new OpenCvSharp.Point(r.TopLeft.X, r.TopLeft.Y - 10), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);Cv2.Rectangle(result_image, r, Scalar.Red, thickness: 2);}}double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;stopwatch.Stop();double totalTime = preprocessTime + inferTime + postprocessTime;pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());sb.AppendLine($"Preprocess: {preprocessTime:F2}ms");sb.AppendLine($"Infer: {inferTime:F2}ms");sb.AppendLine($"Postprocess: {postprocessTime:F2}ms");sb.AppendLine($"Total: {totalTime:F2}ms");textBox1.Text = sb.ToString();}private void Form1_Load(object sender, EventArgs e){startupPath = Application.StartupPath;string classer_path = "lable.txt";List<string> str = new List<string>();StreamReader sr = new StreamReader(classer_path);string line;while ((line = sr.ReadLine()) != null){str.Add(line);}dicts = str.ToArray();image_path = "test_img/1.jpg";pictureBox1.Image = new Bitmap(image_path);}}
}

下载

源码下载

其他

C# PaddleDetection yolo 印章检测-CSDN博客

C# Onnx Yolov8 Detect 印章 指纹捺印 检测-CSDN博客

这篇关于C# OpenVINO 直接读取百度模型实现印章检测的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

基于C++的UDP网络通信系统设计与实现详解

《基于C++的UDP网络通信系统设计与实现详解》在网络编程领域,UDP作为一种无连接的传输层协议,以其高效、低延迟的特性在实时性要求高的应用场景中占据重要地位,下面我们就来看看如何从零开始构建一个完整... 目录前言一、UDP服务器UdpServer.hpp1.1 基本框架设计1.2 初始化函数Init详解

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

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

《C#中checked关键字的使用小结》本文主要介绍了C#中checked关键字的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录✅ 为什么需要checked? 问题:整数溢出是“静默China编程”的(默认)checked的三种用

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

C#中预处理器指令的使用小结

《C#中预处理器指令的使用小结》本文主要介绍了C#中预处理器指令的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 第 1 名:#if/#else/#elif/#endif✅用途:条件编译(绝对最常用!) 典型场景: 示例

基于SpringBoot实现分布式锁的三种方法

《基于SpringBoot实现分布式锁的三种方法》这篇文章主要为大家详细介绍了基于SpringBoot实现分布式锁的三种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、基于Redis原生命令实现分布式锁1. 基础版Redis分布式锁2. 可重入锁实现二、使用Redisso

SpringBoo WebFlux+MongoDB实现非阻塞API过程

《SpringBooWebFlux+MongoDB实现非阻塞API过程》本文介绍了如何使用SpringBootWebFlux和MongoDB实现非阻塞API,通过响应式编程提高系统的吞吐量和响应性能... 目录一、引言二、响应式编程基础2.1 响应式编程概念2.2 响应式编程的优势2.3 响应式编程相关技术

Springboot配置文件相关语法及读取方式详解

《Springboot配置文件相关语法及读取方式详解》本文主要介绍了SpringBoot中的两种配置文件形式,即.properties文件和.yml/.yaml文件,详细讲解了这两种文件的语法和读取方... 目录配置文件的形式语法1、key-value形式2、数组形式读取方式1、通过@value注解2、通过

C#实现将XML数据自动化地写入Excel文件

《C#实现将XML数据自动化地写入Excel文件》在现代企业级应用中,数据处理与报表生成是核心环节,本文将深入探讨如何利用C#和一款优秀的库,将XML数据自动化地写入Excel文件,有需要的小伙伴可以... 目录理解XML数据结构与Excel的对应关系引入高效工具:使用Spire.XLS for .NETC