当前位置:   article > 正文

C# Onnx GroundingDINO 开放世界目标检测

C# Onnx GroundingDINO 开放世界目标检测

目录

介绍

效果

模型信息

项目

代码

下载


介绍

地址:https://github.com/IDEA-Research/GroundingDINO

Official implementation of the paper "Grounding DINO: Marrying DINO with Grounded Pre-Training for Open-Set Object Detection"

效果

在运行程序时,要注意输入的提示词的格式,类别之间以" . "隔开,并且确保类别名称在词典文件 vocab.txt里是存在的,而且输入提示词里的类别名称是你想要检测的目标类别,否则可能会检测不到目标的。 

模型信息

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

Inputs
-------------------------
name:img
tensor:Float[-1, 3, -1, -1]
name:input_ids
tensor:Int64[-1, -1]
name:attention_mask
tensor:Bool[-1, -1]
name:position_ids
tensor:Int64[-1, -1]
name:token_type_ids
tensor:Int64[-1, -1]
name:text_token_mask
tensor:Bool[-1, -1, -1]
---------------------------------------------------------------

Outputs
-------------------------
name:logits
tensor:Float[-1, -1, -1]
name:boxes
tensor:Float[-1, -1, 4]
---------------------------------------------------------------

项目

代码

  1. using OpenCvSharp;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. namespace Onnx_Demo
  8. {
  9. public partial class Form1 : Form
  10. {
  11. public Form1()
  12. {
  13. InitializeComponent();
  14. }
  15. GroundingDINO groundingDINO = new GroundingDINO("model/groundingdino_swint_ogc.onnx", 0.3f, "model/vocab.txt", 0.25f, true);
  16. string image_path = "";
  17. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
  18. StringBuilder sb = new StringBuilder();
  19. Mat image;
  20. Mat result_image;
  21. private void button2_Click(object sender, EventArgs e)
  22. {
  23. OpenFileDialog ofd = new OpenFileDialog();
  24. ofd.Filter = fileFilter;
  25. if (ofd.ShowDialog() != DialogResult.OK) return;
  26. pictureBox1.Image = null;
  27. pictureBox2.Image = null;
  28. txtInfo.Text = "";
  29. image_path = ofd.FileName;
  30. pictureBox2.Image = new Bitmap(image_path);
  31. image = new Mat(image_path);
  32. }
  33. private void button3_Click(object sender, EventArgs e)
  34. {
  35. if (image_path == "")
  36. {
  37. return;
  38. }
  39. if (String.IsNullOrEmpty(txt_input_text.Text))
  40. {
  41. return;
  42. }
  43. pictureBox1.Image = null;
  44. txtInfo.Text = "检测中,请稍等……";
  45. button3.Enabled = false;
  46. if (pictureBox1.Image != null)
  47. {
  48. pictureBox1.Image.Dispose();
  49. pictureBox1.Image = null;
  50. }
  51. Application.DoEvents();
  52. String text_prompt = txt_input_text.Text;
  53. List<Object> objects = groundingDINO.detect(image, text_prompt);
  54. result_image = image.Clone();
  55. sb.Clear();
  56. for (int i = 0; i < objects.Count; i++)
  57. {
  58. Cv2.Rectangle(result_image, objects[i].box, new Scalar(0, 0, 255), 2);
  59. Cv2.PutText(result_image, objects[i].text + " " + objects[i].prob.ToString("F2"), new OpenCvSharp.Point(objects[i].box.X, objects[i].box.Y), HersheyFonts.HersheySimplex, 1, new Scalar(0, 0, 255), 2); ;
  60. sb.AppendLine(objects[i].text + " " + objects[i].prob.ToString("F2"));
  61. }
  62. pictureBox1.Image = new Bitmap(result_image.ToMemoryStream());
  63. button3.Enabled = true;
  64. txtInfo.Text = sb.ToString();
  65. }
  66. private void Form1_Load(object sender, EventArgs e)
  67. {
  68. image_path = "test_img/cat_dog.jpeg";
  69. pictureBox2.Image = new Bitmap(image_path);
  70. image = new Mat(image_path);
  71. }
  72. }
  73. }

下载

源码下载

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/187071
推荐阅读
相关标签
  

闽ICP备14008679号