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]


项目

代码

Form1

using OpenCvSharp;

using System;

using System.Collections.Generic;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace Onnx_Demo

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

GroundingDINO groundingDINO = new GroundingDINO("model/groundingdino_swint_ogc.onnx", 0.3f, "model/vocab.txt", 0.25f, true);

string image_path = "";

string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";

StringBuilder sb = new StringBuilder();

Mat image;

Mat result_image;

private void button2_Click(object sender, EventArgs e)

{

OpenFileDialog ofd = new OpenFileDialog();

ofd.Filter = fileFilter;

if (ofd.ShowDialog() != DialogResult.OK) return;

pictureBox1.Image = null;

pictureBox2.Image = null;

txtInfo.Text = "";

image_path = ofd.FileName;

pictureBox2.Image = new Bitmap(image_path);

image = new Mat(image_path);

}

private void button3_Click(object sender, EventArgs e)

{

if (image_path == "")

{

return;

}

if (String.IsNullOrEmpty(txt_input_text.Text))

{

return;

}

pictureBox1.Image = null;

txtInfo.Text = "检测中,请稍等......";

button3.Enabled = false;

if (pictureBox1.Image != null)

{

pictureBox1.Image.Dispose();

pictureBox1.Image = null;

}

Application.DoEvents();

String text_prompt = txt_input_text.Text;

List<Object> objects = groundingDINO.detect(image, text_prompt);

result_image = image.Clone();

sb.Clear();

for (int i = 0; i < objects.Count; i++)

{

Cv2.Rectangle(result_image, objects[i].box, new Scalar(0, 0, 255), 2);

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

sb.AppendLine(objects[i].text + " " + objects[i].prob.ToString("F2"));

}

pictureBox1.Image = new Bitmap(result_image.ToMemoryStream());

button3.Enabled = true;

txtInfo.Text = sb.ToString();

}

private void Form1_Load(object sender, EventArgs e)

{

image_path = "test_img/cat_dog.jpeg";

pictureBox2.Image = new Bitmap(image_path);

image = new Mat(image_path);

}

}

}

using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

        GroundingDINO groundingDINO = new GroundingDINO("model/groundingdino_swint_ogc.onnx", 0.3f, "model/vocab.txt", 0.25f, true);

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

        StringBuilder sb = new StringBuilder();

        Mat image;
        Mat result_image;

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;

            pictureBox1.Image = null;
            pictureBox2.Image = null;
            txtInfo.Text = "";

            image_path = ofd.FileName;
            pictureBox2.Image = new Bitmap(image_path);
            image = new Mat(image_path);

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

            if (String.IsNullOrEmpty(txt_input_text.Text))
            {
                return;
            }

            pictureBox1.Image = null;
            txtInfo.Text = "检测中,请稍等......";
            button3.Enabled = false;
            if (pictureBox1.Image != null)
            {
                pictureBox1.Image.Dispose();
                pictureBox1.Image = null;
            }
            Application.DoEvents();

            String text_prompt = txt_input_text.Text;

            List<Object> objects = groundingDINO.detect(image, text_prompt);

            result_image = image.Clone();
            sb.Clear();
            for (int i = 0; i < objects.Count; i++)
            {
                Cv2.Rectangle(result_image, objects[i].box, new Scalar(0, 0, 255), 2);
                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); ;
                sb.AppendLine(objects[i].text + " " + objects[i].prob.ToString("F2"));
            }
            pictureBox1.Image = new Bitmap(result_image.ToMemoryStream());

            button3.Enabled = true;
            txtInfo.Text = sb.ToString();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            image_path = "test_img/cat_dog.jpeg";
            pictureBox2.Image = new Bitmap(image_path);
            image = new Mat(image_path);
        }
    }
}

下载

源码下载

相关推荐
程序小旭1 小时前
机器视觉基础—双目相机
计算机视觉·双目相机
qzhqbb1 小时前
基于统计方法的语言模型
人工智能·语言模型·easyui
冷眼看人间恩怨2 小时前
【话题讨论】AI大模型重塑软件开发:定义、应用、优势与挑战
人工智能·ai编程·软件开发
2401_883041082 小时前
新锐品牌电商代运营公司都有哪些?
大数据·人工智能
AI极客菌3 小时前
Controlnet作者新作IC-light V2:基于FLUX训练,支持处理风格化图像,细节远高于SD1.5。
人工智能·计算机视觉·ai作画·stable diffusion·aigc·flux·人工智能作画
阿_旭3 小时前
一文读懂| 自注意力与交叉注意力机制在计算机视觉中作用与基本原理
人工智能·深度学习·计算机视觉·cross-attention·self-attention
王哈哈^_^3 小时前
【数据集】【YOLO】【目标检测】交通事故识别数据集 8939 张,YOLO道路事故目标检测实战训练教程!
前端·人工智能·深度学习·yolo·目标检测·计算机视觉·pyqt
Power20246664 小时前
NLP论文速读|LongReward:基于AI反馈来提升长上下文大语言模型
人工智能·深度学习·机器学习·自然语言处理·nlp
数据猎手小k4 小时前
AIDOVECL数据集:包含超过15000张AI生成的车辆图像数据集,目的解决旨在解决眼水平分类和定位问题。
人工智能·分类·数据挖掘
好奇龙猫4 小时前
【学习AI-相关路程-mnist手写数字分类-win-硬件:windows-自我学习AI-实验步骤-全连接神经网络(BPnetwork)-操作流程(3) 】
人工智能·算法