首页 最新 热门 推荐

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

C# OnnxRuntime SAM2

  • 25-02-19 03:42
  • 3765
  • 7096
blog.csdn.net

目录

介绍

效果

模型信息

sam2_hiera_small_encoder.onnx

sam2_hiera_small_decoder.onnx

项目

​编辑

代码

下载

其他


介绍

官网地址:https://github.com/facebookresearch/sam2

Segment Anything Model 2 (SAM 2) is a foundation model towards solving promptable visual segmentation in images and videos. We extend SAM to video by considering images as a video with a single frame. The model design is a simple transformer architecture with streaming memory for real-time video processing. We build a model-in-the-loop data engine, which improves model and data via user interaction, to collect our SA-V dataset, the largest video segmentation dataset to date. SAM 2 trained on our data provides strong performance across a wide range of tasks and visual domains.

效果

模型信息

sam2_hiera_small_encoder.onnx

Model Properties
-------------------------
---------------------------------------------------------------

Inputs
-------------------------
name:image
tensor:Float[1, 3, 1024, 1024]
---------------------------------------------------------------

Outputs
-------------------------
name:high_res_feats_0
tensor:Float[1, 32, 256, 256]
name:high_res_feats_1
tensor:Float[1, 64, 128, 128]
name:image_embed
tensor:Float[1, 256, 64, 64]
---------------------------------------------------------------

sam2_hiera_small_decoder.onnx

Model Properties
-------------------------
---------------------------------------------------------------

Inputs
-------------------------
name:image_embed
tensor:Float[1, 256, 64, 64]
name:high_res_feats_0
tensor:Float[1, 32, 256, 256]
name:high_res_feats_1
tensor:Float[1, 64, 128, 128]
name:point_coords
tensor:Float[-1, -1, 2]
name:point_labels
tensor:Float[-1, -1]
name:mask_input
tensor:Float[-1, 1, 256, 256]
name:has_mask_input
tensor:Float[-1]
name:orig_im_size
tensor:Int32[2]
---------------------------------------------------------------

Outputs
-------------------------
name:masks
tensor:Float[-1, -1, -1, -1]
name:iou_predictions
tensor:Float[-1, -1]
---------------------------------------------------------------

项目

代码

using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;
using System.Windows.Forms;

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

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;
        Mat image;

        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 = "";
            image = new Mat(image_path);
            pictureBox2.Image = null;
            pictureBox3.Image = null;

            sam.SetImage(image_path);
            image = new Mat(image_path);
            pictureBox1.Image = new Bitmap(image_path);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            pictureBox3.Image = null;
            button2.Enabled = false;
            Application.DoEvents();
            List masks_list = sam.GetPointMask(roi);
            if (masks_list.Count > 0)
            {
                int MaxInde = 0;
                float maxiou_pred = masks_list[0].iou_pred;
                for (int i = 0; i < masks_list.Count; i++)
                {
                    float temp = masks_list[i].iou_pred;
                    if (temp > maxiou_pred)
                    {
                        MaxInde = i;
                    }
                }
                pictureBox3.Image = BitmapConverter.ToBitmap(masks_list[MaxInde].mask);
            }
            button2.Enabled = true;
        }

        SamInferenceSession sam;

        private void Form1_Load(object sender, EventArgs e)
        {
            string encoderPath = "model/sam2_hiera_small_encoder.onnx";
            string decoderPath = "model/sam2_hiera_small_decoder.onnx";

            sam = new SamInferenceSession(encoderPath, decoderPath);
            sam.Initialize();

            image_path = "test_img/1.jpg";

            sam.SetImage(image_path);
            image = new Mat(image_path);
            pictureBox1.Image = new Bitmap(image_path);

        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            Common.ShowNormalImg(pictureBox1.Image);
        }

        private void pictureBox2_DoubleClick(object sender, EventArgs e)
        {
            Common.ShowNormalImg(pictureBox2.Image);
        }

        SaveFileDialog sdf = new SaveFileDialog();
        private void button3_Click(object sender, EventArgs e)
        {
            if (pictureBox2.Image == null)
            {
                return;
            }
            Bitmap output = new Bitmap(pictureBox2.Image);
            sdf.Title = "保存";
            sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";
            if (sdf.ShowDialog() == DialogResult.OK)
            {
                switch (sdf.FilterIndex)
                {
                    case 1:
                        {
                            output.Save(sdf.FileName, ImageFormat.Jpeg);
                            break;
                        }
                    case 2:
                        {
                            output.Save(sdf.FileName, ImageFormat.Png);
                            break;
                        }
                    case 3:
                        {
                            output.Save(sdf.FileName, ImageFormat.Bmp);
                            break;
                        }
                    case 4:
                        {
                            output.Save(sdf.FileName, ImageFormat.Emf);
                            break;
                        }
                    case 5:
                        {
                            output.Save(sdf.FileName, ImageFormat.Exif);
                            break;
                        }
                    case 6:
                        {
                            output.Save(sdf.FileName, ImageFormat.Gif);
                            break;
                        }
                    case 7:
                        {
                            output.Save(sdf.FileName, ImageFormat.Icon);
                            break;
                        }

                    case 8:
                        {
                            output.Save(sdf.FileName, ImageFormat.Tiff);
                            break;
                        }
                    case 9:
                        {
                            output.Save(sdf.FileName, ImageFormat.Wmf);
                            break;
                        }
                }
                MessageBox.Show("保存成功,位置:" + sdf.FileName);
            }
        }

        bool m_mouseDown = false;
        bool m_mouseMove = false;

        System.Drawing.Point startPoint = new System.Drawing.Point();
        System.Drawing.Point endPoint = new System.Drawing.Point();

        Mat currentFrame = new Mat();
        Rect roi = new Rect();

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (pictureBox1.Image == null)
                return;
            if (!m_mouseDown) return;

            m_mouseMove = true;
            endPoint = e.Location;

            pictureBox1.Invalidate();

        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (!m_mouseDown || !m_mouseMove)
                return;
            Graphics g = e.Graphics;
            Pen p = new Pen(Color.Blue, 2);
            Rectangle rect = new Rectangle(startPoint.X, startPoint.Y, (endPoint.X - startPoint.X), (endPoint.Y - startPoint.Y));
            g.DrawRectangle(p, rect);
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (!m_mouseDown || !m_mouseMove)
                return;
            m_mouseDown = false;
            m_mouseMove = false;

            System.Drawing.Point image_startPoint = ConvertCooridinate(startPoint);
            System.Drawing.Point image_endPoint = ConvertCooridinate(endPoint);
            if (image_startPoint.X < 0)
                image_startPoint.X = 0;
            if (image_startPoint.Y < 0)
                image_startPoint.Y = 0;
            if (image_endPoint.X < 0)
                image_endPoint.X = 0;
            if (image_endPoint.Y < 0)
                image_endPoint.Y = 0;
            if (image_startPoint.X > image.Cols)
                image_startPoint.X = image.Cols;
            if (image_startPoint.Y > image.Rows)
                image_startPoint.Y = image.Rows;
            if (image_endPoint.X > image.Cols)
                image_endPoint.X = image.Cols;
            if (image_endPoint.Y > image.Rows)
                image_endPoint.Y = image.Rows;

            label1.Text = String.Format("ROI:({0},{1})-({2},{3})", image_startPoint.X, image_startPoint.Y, image_endPoint.X, image_endPoint.Y);
            int w = (image_endPoint.X - image_startPoint.X);
            int h = (image_endPoint.Y - image_startPoint.Y);
            if (w > 10 && h > 10)
            {
                roi = new Rect(image_startPoint.X, image_startPoint.Y, w, h);
                //Console.WriteLine(String.Format("ROI:({0},{1})-({2},{3})", image_startPoint.X, image_startPoint.Y, image_endPoint.X, image_endPoint.Y));
                //test
                //OpenCvSharp.Point pointinfo = new OpenCvSharp.Point(910, 641);
                //roi = new Rect(pointinfo.X - 160, pointinfo.Y - 430, 380, 940);
                Mat roi_mat = image[roi];
                pictureBox2.Image = BitmapConverter.ToBitmap(roi_mat);
            }
            //pictureBox1.Invalidate();

        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (pictureBox1.Image == null)
                return;
            m_mouseDown = true;
            startPoint = e.Location;
        }

        private System.Drawing.Point ConvertCooridinate(System.Drawing.Point point)
        {
            System.Reflection.PropertyInfo rectangleProperty = this.pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);
            Rectangle pictureBox = (Rectangle)rectangleProperty.GetValue(this.pictureBox1, null);

            int zoomedWidth = pictureBox.Width;
            int zoomedHeight = pictureBox.Height;

            int imageWidth = pictureBox1.Image.Width;
            int imageHeight = pictureBox1.Image.Height;

            double zoomRatex = (double)(zoomedWidth) / (double)(imageWidth);
            double zoomRatey = (double)(zoomedHeight) / (double)(imageHeight);
            int black_left_width = (zoomedWidth == this.pictureBox1.Width) ? 0 : (this.pictureBox1.Width - zoomedWidth) / 2;
            int black_top_height = (zoomedHeight == this.pictureBox1.Height) ? 0 : (this.pictureBox1.Height - zoomedHeight) / 2;

            int zoomedX = point.X - black_left_width;
            int zoomedY = point.Y - black_top_height;

            System.Drawing.Point outPoint = new System.Drawing.Point();
            outPoint.X = (int)((double)zoomedX / zoomRatex);
            outPoint.Y = (int)((double)zoomedY / zoomRatey);

            return outPoint;
        }

    }
}
 

  1. using OpenCvSharp;
  2. using OpenCvSharp.Extensions;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.Reflection;
  8. using System.Windows.Forms;
  9. namespace Onnx_Demo
  10. {
  11. public partial class Form1 : Form
  12. {
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. }
  17. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
  18. string image_path = "";
  19. DateTime dt1 = DateTime.Now;
  20. DateTime dt2 = DateTime.Now;
  21. Mat image;
  22. private void button1_Click(object sender, EventArgs e)
  23. {
  24. OpenFileDialog ofd = new OpenFileDialog();
  25. ofd.Filter = fileFilter;
  26. if (ofd.ShowDialog() != DialogResult.OK) return;
  27. pictureBox1.Image = null;
  28. image_path = ofd.FileName;
  29. pictureBox1.Image = new Bitmap(image_path);
  30. textBox1.Text = "";
  31. image = new Mat(image_path);
  32. pictureBox2.Image = null;
  33. pictureBox3.Image = null;
  34. sam.SetImage(image_path);
  35. image = new Mat(image_path);
  36. pictureBox1.Image = new Bitmap(image_path);
  37. }
  38. private void button2_Click(object sender, EventArgs e)
  39. {
  40. pictureBox3.Image = null;
  41. button2.Enabled = false;
  42. Application.DoEvents();
  43. List<MatInfo> masks_list = sam.GetPointMask(roi);
  44. if (masks_list.Count > 0)
  45. {
  46. int MaxInde = 0;
  47. float maxiou_pred = masks_list[0].iou_pred;
  48. for (int i = 0; i < masks_list.Count; i++)
  49. {
  50. float temp = masks_list[i].iou_pred;
  51. if (temp > maxiou_pred)
  52. {
  53. MaxInde = i;
  54. }
  55. }
  56. pictureBox3.Image = BitmapConverter.ToBitmap(masks_list[MaxInde].mask);
  57. }
  58. button2.Enabled = true;
  59. }
  60. SamInferenceSession sam;
  61. private void Form1_Load(object sender, EventArgs e)
  62. {
  63. string encoderPath = "model/sam2_hiera_small_encoder.onnx";
  64. string decoderPath = "model/sam2_hiera_small_decoder.onnx";
  65. sam = new SamInferenceSession(encoderPath, decoderPath);
  66. sam.Initialize();
  67. image_path = "test_img/1.jpg";
  68. sam.SetImage(image_path);
  69. image = new Mat(image_path);
  70. pictureBox1.Image = new Bitmap(image_path);
  71. }
  72. private void pictureBox1_DoubleClick(object sender, EventArgs e)
  73. {
  74. Common.ShowNormalImg(pictureBox1.Image);
  75. }
  76. private void pictureBox2_DoubleClick(object sender, EventArgs e)
  77. {
  78. Common.ShowNormalImg(pictureBox2.Image);
  79. }
  80. SaveFileDialog sdf = new SaveFileDialog();
  81. private void button3_Click(object sender, EventArgs e)
  82. {
  83. if (pictureBox2.Image == null)
  84. {
  85. return;
  86. }
  87. Bitmap output = new Bitmap(pictureBox2.Image);
  88. sdf.Title = "保存";
  89. sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";
  90. if (sdf.ShowDialog() == DialogResult.OK)
  91. {
  92. switch (sdf.FilterIndex)
  93. {
  94. case 1:
  95. {
  96. output.Save(sdf.FileName, ImageFormat.Jpeg);
  97. break;
  98. }
  99. case 2:
  100. {
  101. output.Save(sdf.FileName, ImageFormat.Png);
  102. break;
  103. }
  104. case 3:
  105. {
  106. output.Save(sdf.FileName, ImageFormat.Bmp);
  107. break;
  108. }
  109. case 4:
  110. {
  111. output.Save(sdf.FileName, ImageFormat.Emf);
  112. break;
  113. }
  114. case 5:
  115. {
  116. output.Save(sdf.FileName, ImageFormat.Exif);
  117. break;
  118. }
  119. case 6:
  120. {
  121. output.Save(sdf.FileName, ImageFormat.Gif);
  122. break;
  123. }
  124. case 7:
  125. {
  126. output.Save(sdf.FileName, ImageFormat.Icon);
  127. break;
  128. }
  129. case 8:
  130. {
  131. output.Save(sdf.FileName, ImageFormat.Tiff);
  132. break;
  133. }
  134. case 9:
  135. {
  136. output.Save(sdf.FileName, ImageFormat.Wmf);
  137. break;
  138. }
  139. }
  140. MessageBox.Show("保存成功,位置:" + sdf.FileName);
  141. }
  142. }
  143. bool m_mouseDown = false;
  144. bool m_mouseMove = false;
  145. System.Drawing.Point startPoint = new System.Drawing.Point();
  146. System.Drawing.Point endPoint = new System.Drawing.Point();
  147. Mat currentFrame = new Mat();
  148. Rect roi = new Rect();
  149. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  150. {
  151. if (pictureBox1.Image == null)
  152. return;
  153. if (!m_mouseDown) return;
  154. m_mouseMove = true;
  155. endPoint = e.Location;
  156. pictureBox1.Invalidate();
  157. }
  158. private void pictureBox1_Paint(object sender, PaintEventArgs e)
  159. {
  160. if (!m_mouseDown || !m_mouseMove)
  161. return;
  162. Graphics g = e.Graphics;
  163. Pen p = new Pen(Color.Blue, 2);
  164. Rectangle rect = new Rectangle(startPoint.X, startPoint.Y, (endPoint.X - startPoint.X), (endPoint.Y - startPoint.Y));
  165. g.DrawRectangle(p, rect);
  166. }
  167. private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  168. {
  169. if (!m_mouseDown || !m_mouseMove)
  170. return;
  171. m_mouseDown = false;
  172. m_mouseMove = false;
  173. System.Drawing.Point image_startPoint = ConvertCooridinate(startPoint);
  174. System.Drawing.Point image_endPoint = ConvertCooridinate(endPoint);
  175. if (image_startPoint.X < 0)
  176. image_startPoint.X = 0;
  177. if (image_startPoint.Y < 0)
  178. image_startPoint.Y = 0;
  179. if (image_endPoint.X < 0)
  180. image_endPoint.X = 0;
  181. if (image_endPoint.Y < 0)
  182. image_endPoint.Y = 0;
  183. if (image_startPoint.X > image.Cols)
  184. image_startPoint.X = image.Cols;
  185. if (image_startPoint.Y > image.Rows)
  186. image_startPoint.Y = image.Rows;
  187. if (image_endPoint.X > image.Cols)
  188. image_endPoint.X = image.Cols;
  189. if (image_endPoint.Y > image.Rows)
  190. image_endPoint.Y = image.Rows;
  191. label1.Text = String.Format("ROI:({0},{1})-({2},{3})", image_startPoint.X, image_startPoint.Y, image_endPoint.X, image_endPoint.Y);
  192. int w = (image_endPoint.X - image_startPoint.X);
  193. int h = (image_endPoint.Y - image_startPoint.Y);
  194. if (w > 10 && h > 10)
  195. {
  196. roi = new Rect(image_startPoint.X, image_startPoint.Y, w, h);
  197. //Console.WriteLine(String.Format("ROI:({0},{1})-({2},{3})", image_startPoint.X, image_startPoint.Y, image_endPoint.X, image_endPoint.Y));
  198. //test
  199. //OpenCvSharp.Point pointinfo = new OpenCvSharp.Point(910, 641);
  200. //roi = new Rect(pointinfo.X - 160, pointinfo.Y - 430, 380, 940);
  201. Mat roi_mat = image[roi];
  202. pictureBox2.Image = BitmapConverter.ToBitmap(roi_mat);
  203. }
  204. //pictureBox1.Invalidate();
  205. }
  206. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  207. {
  208. if (pictureBox1.Image == null)
  209. return;
  210. m_mouseDown = true;
  211. startPoint = e.Location;
  212. }
  213. private System.Drawing.Point ConvertCooridinate(System.Drawing.Point point)
  214. {
  215. System.Reflection.PropertyInfo rectangleProperty = this.pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);
  216. Rectangle pictureBox = (Rectangle)rectangleProperty.GetValue(this.pictureBox1, null);
  217. int zoomedWidth = pictureBox.Width;
  218. int zoomedHeight = pictureBox.Height;
  219. int imageWidth = pictureBox1.Image.Width;
  220. int imageHeight = pictureBox1.Image.Height;
  221. double zoomRatex = (double)(zoomedWidth) / (double)(imageWidth);
  222. double zoomRatey = (double)(zoomedHeight) / (double)(imageHeight);
  223. int black_left_width = (zoomedWidth == this.pictureBox1.Width) ? 0 : (this.pictureBox1.Width - zoomedWidth) / 2;
  224. int black_top_height = (zoomedHeight == this.pictureBox1.Height) ? 0 : (this.pictureBox1.Height - zoomedHeight) / 2;
  225. int zoomedX = point.X - black_left_width;
  226. int zoomedY = point.Y - black_top_height;
  227. System.Drawing.Point outPoint = new System.Drawing.Point();
  228. outPoint.X = (int)((double)zoomedX / zoomRatex);
  229. outPoint.Y = (int)((double)zoomedY / zoomRatey);
  230. return outPoint;
  231. }
  232. }
  233. }

下载

源码下载

其他

C# OnnxRuntime SAM

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

/ 登录

评论记录:

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

分类栏目

后端 (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