首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

C# OpenVINO Crack Seg 裂缝分割 裂缝检测

  • 25-02-19 03:01
  • 2373
  • 12202
blog.csdn.net

目录

效果

模型信息

项目

代码

数据集

下载


C# OpenVINO Crack Seg 裂缝分割  裂缝检测

效果

模型信息

Model Properties
-------------------------
date:2024-02-29T16:35:48.364242
author:Ultralytics
task:segment
version:8.1.18
stride:32
batch:1
imgsz:[640, 640]
names:{0: 'crack'}
---------------------------------------------------------------

Inputs
-------------------------
name:images
tensor:Float[1, 3, 640, 640]
---------------------------------------------------------------

Outputs
-------------------------
name:output0
tensor:Float[1, 37, 8400]
name:output1
tensor:Float[1, 32, 160, 160]
---------------------------------------------------------------

项目

代码

using OpenCvSharp;
using Sdcb.OpenVINO;
using Sdcb.OpenVINO.Natives;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace OpenVINO_Seg
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        string startupPath;
        string model_path;
        string classer_path;

        Mat src;

        SegmentationResult result_pro;
        Mat result_image;
        Result seg_result;

        StringBuilder sb = new StringBuilder();

        float[] det_result_array = new float[8400 * 37];
        float[] proto_result_array = new float[32 * 160 * 160];

        // 识别结果类型
        public string[] class_names;

        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);

            Model rawModel = OVCore.Shared.ReadModel(model_path);
            PrePostProcessor pp = rawModel.CreatePrePostProcessor();
            PreProcessInputInfo inputInfo = pp.Inputs.Primary;

            inputInfo.TensorInfo.Layout = Sdcb.OpenVINO.Layout.NHWC;
            inputInfo.ModelInfo.Layout = Sdcb.OpenVINO.Layout.NCHW;

            Model m = pp.BuildModel();
            CompiledModel cm = OVCore.Shared.CompileModel(m, "CPU");
            InferRequest ir = cm.CreateInferRequest();

            Shape inputShape = m.Inputs[0].Shape;

            float[] factors = new float[4];
            factors[0] = 1f * src.Width / inputShape[2];
            factors[1] = 1f * src.Height / inputShape[1];
            factors[2] = src.Rows;
            factors[3] = src.Cols;

            result_pro = new SegmentationResult(class_names, factors,0.3f,0.5f);

            Stopwatch stopwatch = new Stopwatch();
            Mat resized = src.Resize(new OpenCvSharp.Size(inputShape[2], inputShape[1]));
            Mat f32 = new Mat();
            resized.ConvertTo(f32, MatType.CV_32FC3, 1.0 / 255);

            using (Tensor input = Tensor.FromRaw(
                 new ReadOnlySpan((void*)f32.Data, (int)((long)f32.DataEnd - (long)f32.DataStart)),
                new Shape(1, f32.Rows, f32.Cols, 3),
                ov_element_type_e.F32))
            {
                ir.Inputs.Primary = input;
            }
            double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Restart();

            ir.Run();
            double inferTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Restart();

            using (Tensor output_det = ir.Outputs[0])
            using (Tensor output_proto = ir.Outputs[1])
            {
                det_result_array = output_det.GetData().ToArray();
                proto_result_array = output_proto.GetData().ToArray();

                seg_result = result_pro.process_result(det_result_array, proto_result_array);

                double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;
                stopwatch.Stop();

                double totalTime = preprocessTime + inferTime + postprocessTime;

                result_image = src.Clone();
                Mat masked_img = new Mat();

                // 将识别结果绘制到图片上
                for (int i = 0; i < seg_result.length; i++)
                {
                    Cv2.Rectangle(result_image, seg_result.rects[i], new Scalar(0, 0, 255), 2, LineTypes.Link8);
                    Cv2.Rectangle(result_image, new OpenCvSharp.Point(seg_result.rects[i].TopLeft.X, seg_result.rects[i].TopLeft.Y - 20),
                        new OpenCvSharp.Point(seg_result.rects[i].BottomRight.X, seg_result.rects[i].TopLeft.Y), new Scalar(0, 255, 255), -1);
                    Cv2.PutText(result_image, seg_result.classes[i] + "-" + seg_result.scores[i].ToString("0.00"),
                        new OpenCvSharp.Point(seg_result.rects[i].X, seg_result.rects[i].Y - 5),
                        HersheyFonts.HersheySimplex, 0.6, new Scalar(0, 0, 0), 1);
                    Cv2.AddWeighted(result_image, 0.5, seg_result.masks[i], 0.5, 0, masked_img);

                    sb.AppendLine($"{seg_result.classes[i]}:{seg_result.scores[i]:P0}");
                }

                if (seg_result.length > 0)
                {
                    if (pictureBox2.Image != null)
                    {
                        pictureBox2.Image.Dispose();
                    }
                    pictureBox2.Image = new Bitmap(masked_img.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();
                }
                else
                {
                    textBox1.Text = "无信息";
                }

                masked_img.Dispose();
                result_image.Dispose();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            image_path = "1.jpg";
            pictureBox1.Image = new Bitmap(image_path);

            startupPath = Application.StartupPath;

            model_path = startupPath + "\\crack_m_best.onnx";
            classer_path = startupPath + "\\lable.txt";

            List str = new List();
            StreamReader sr = new StreamReader(classer_path);
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                str.Add(line);
            }
            class_names = str.ToArray();

        }
    }
}

  1. using OpenCvSharp;
  2. using Sdcb.OpenVINO;
  3. using Sdcb.OpenVINO.Natives;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Text;
  10. using System.Windows.Forms;
  11. namespace OpenVINO_Seg
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
  20. string image_path = "";
  21. string startupPath;
  22. string model_path;
  23. string classer_path;
  24. Mat src;
  25. SegmentationResult result_pro;
  26. Mat result_image;
  27. Result seg_result;
  28. StringBuilder sb = new StringBuilder();
  29. float[] det_result_array = new float[8400 * 37];
  30. float[] proto_result_array = new float[32 * 160 * 160];
  31. // 识别结果类型
  32. public string[] class_names;
  33. private void button1_Click(object sender, EventArgs e)
  34. {
  35. OpenFileDialog ofd = new OpenFileDialog();
  36. ofd.Filter = fileFilter;
  37. if (ofd.ShowDialog() != DialogResult.OK) return;
  38. pictureBox1.Image = null;
  39. image_path = ofd.FileName;
  40. pictureBox1.Image = new Bitmap(image_path);
  41. textBox1.Text = "";
  42. src = new Mat(image_path);
  43. pictureBox2.Image = null;
  44. }
  45. unsafe private void button2_Click(object sender, EventArgs e)
  46. {
  47. if (pictureBox1.Image == null)
  48. {
  49. return;
  50. }
  51. pictureBox2.Image = null;
  52. textBox1.Text = "";
  53. sb.Clear();
  54. src = new Mat(image_path);
  55. Model rawModel = OVCore.Shared.ReadModel(model_path);
  56. PrePostProcessor pp = rawModel.CreatePrePostProcessor();
  57. PreProcessInputInfo inputInfo = pp.Inputs.Primary;
  58. inputInfo.TensorInfo.Layout = Sdcb.OpenVINO.Layout.NHWC;
  59. inputInfo.ModelInfo.Layout = Sdcb.OpenVINO.Layout.NCHW;
  60. Model m = pp.BuildModel();
  61. CompiledModel cm = OVCore.Shared.CompileModel(m, "CPU");
  62. InferRequest ir = cm.CreateInferRequest();
  63. Shape inputShape = m.Inputs[0].Shape;
  64. float[] factors = new float[4];
  65. factors[0] = 1f * src.Width / inputShape[2];
  66. factors[1] = 1f * src.Height / inputShape[1];
  67. factors[2] = src.Rows;
  68. factors[3] = src.Cols;
  69. result_pro = new SegmentationResult(class_names, factors,0.3f,0.5f);
  70. Stopwatch stopwatch = new Stopwatch();
  71. Mat resized = src.Resize(new OpenCvSharp.Size(inputShape[2], inputShape[1]));
  72. Mat f32 = new Mat();
  73. resized.ConvertTo(f32, MatType.CV_32FC3, 1.0 / 255);
  74. using (Tensor input = Tensor.FromRaw(
  75. new ReadOnlySpan<byte>((void*)f32.Data, (int)((long)f32.DataEnd - (long)f32.DataStart)),
  76. new Shape(1, f32.Rows, f32.Cols, 3),
  77. ov_element_type_e.F32))
  78. {
  79. ir.Inputs.Primary = input;
  80. }
  81. double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
  82. stopwatch.Restart();
  83. ir.Run();
  84. double inferTime = stopwatch.Elapsed.TotalMilliseconds;
  85. stopwatch.Restart();
  86. using (Tensor output_det = ir.Outputs[0])
  87. using (Tensor output_proto = ir.Outputs[1])
  88. {
  89. det_result_array = output_det.GetData<float>().ToArray();
  90. proto_result_array = output_proto.GetData<float>().ToArray();
  91. seg_result = result_pro.process_result(det_result_array, proto_result_array);
  92. double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;
  93. stopwatch.Stop();
  94. double totalTime = preprocessTime + inferTime + postprocessTime;
  95. result_image = src.Clone();
  96. Mat masked_img = new Mat();
  97. // 将识别结果绘制到图片上
  98. for (int i = 0; i < seg_result.length; i++)
  99. {
  100. Cv2.Rectangle(result_image, seg_result.rects[i], new Scalar(0, 0, 255), 2, LineTypes.Link8);
  101. Cv2.Rectangle(result_image, new OpenCvSharp.Point(seg_result.rects[i].TopLeft.X, seg_result.rects[i].TopLeft.Y - 20),
  102. new OpenCvSharp.Point(seg_result.rects[i].BottomRight.X, seg_result.rects[i].TopLeft.Y), new Scalar(0, 255, 255), -1);
  103. Cv2.PutText(result_image, seg_result.classes[i] + "-" + seg_result.scores[i].ToString("0.00"),
  104. new OpenCvSharp.Point(seg_result.rects[i].X, seg_result.rects[i].Y - 5),
  105. HersheyFonts.HersheySimplex, 0.6, new Scalar(0, 0, 0), 1);
  106. Cv2.AddWeighted(result_image, 0.5, seg_result.masks[i], 0.5, 0, masked_img);
  107. sb.AppendLine($"{seg_result.classes[i]}:{seg_result.scores[i]:P0}");
  108. }
  109. if (seg_result.length > 0)
  110. {
  111. if (pictureBox2.Image != null)
  112. {
  113. pictureBox2.Image.Dispose();
  114. }
  115. pictureBox2.Image = new Bitmap(masked_img.ToMemoryStream());
  116. sb.AppendLine($"Preprocess: {preprocessTime:F2}ms");
  117. sb.AppendLine($"Infer: {inferTime:F2}ms");
  118. sb.AppendLine($"Postprocess: {postprocessTime:F2}ms");
  119. sb.AppendLine($"Total: {totalTime:F2}ms");
  120. textBox1.Text = sb.ToString();
  121. }
  122. else
  123. {
  124. textBox1.Text = "无信息";
  125. }
  126. masked_img.Dispose();
  127. result_image.Dispose();
  128. }
  129. }
  130. private void Form1_Load(object sender, EventArgs e)
  131. {
  132. image_path = "1.jpg";
  133. pictureBox1.Image = new Bitmap(image_path);
  134. startupPath = Application.StartupPath;
  135. model_path = startupPath + "\\crack_m_best.onnx";
  136. classer_path = startupPath + "\\lable.txt";
  137. List<string> str = new List<string>();
  138. StreamReader sr = new StreamReader(classer_path);
  139. string line;
  140. while ((line = sr.ReadLine()) != null)
  141. {
  142. str.Add(line);
  143. }
  144. class_names = str.ToArray();
  145. }
  146. }
  147. }

数据集

下载

裂纹数据集带标注信息下载

源码下载

天天代码码天天
微信公众号
.NET 人工智能实践
注:本文转载自blog.csdn.net的天天代码码天天的文章"https://lw112190.blog.csdn.net/article/details/136376122"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top