图片
图片
先容github地址
https://github.com/facebookresearch/segment-anythingThe repository provides code for running inference with the SegmentAnything Model (SAM), links for downloading the trained model checkpoints, and example notebooks that show how to use the model.
国产三级模子信息sam_vit_b_decoder.onnx
Model Properties----------------------------------------------------------------------------------------Inputs-------------------------name:image_embeddingstensor:Float[1, 256, 64, 64]name:point_coordstensor:Float[1, -1, 2]name:point_labelstensor:Float[1, -1]name:mask_inputtensor:Float[1, 1, 256, 256]name:has_mask_inputtensor:Float[1]name:orig_im_sizetensor:Float[2]---------------------------------------------------------------Outputs-------------------------name:maskstensor:Float[-1, -1, -1, -1]name:iou_predictionstensor:Float[-1, 4]name:low_res_maskstensor:Float[-1, -1, -1, -1]---------------------------------------------------------------
sam_vit_b_encoder.onnx
Model Properties----------------------------------------------------------------------------------------Inputs-------------------------name:imagetensor:Float[1, 3, 1024, 1024]---------------------------------------------------------------Outputs-------------------------name:image_embeddingstensor:Float[1, 256, 64, 64]---------------------------------------------------------------技俩图片
如上成果所示age 动漫,鼠标接受对象,西西另类人体艺术摄影然后点击分割,愚弄模子推理,生成mask。
代码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<MatInfo> 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/sam_vit_b_encoder.onnx'; string decoderPath = 'model/sam_vit_b_decoder.onnx'; sam = new SamInferenceSession(encoderPath, decoderPath); sam.Initialize(); image_path = 'test_img/test.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(); 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