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

下载

源码下载

相关推荐
sp_fyf_20243 分钟前
浅谈计算机视觉的学习路径1
计算机视觉
极客小张8 分钟前
基于STM32MP157与OpenCV的嵌入式Linux人脸识别系统开发设计流程
linux·stm32·单片机·opencv·物联网
AI王也10 分钟前
ChatGPT 4o 使用指南 (9月更新)
人工智能·chatgpt·prompt·aigc
望繁信科技13 分钟前
望繁信科技受邀出席ACS2023,为汽车行业数智化护航添翼
人工智能·企业数字化转型·流程挖掘·流程智能·数字北极星
木凳子a16 分钟前
给儿童掏耳朵用哪个好?儿童耳勺最建议买的五个牌子
人工智能·安全·信息可视化·智能家居·健康医疗
秋926 分钟前
教师心理学能力研判:多维度视角下的分析,判断教师心理学知识能力强弱,并提出针对性意见
人工智能·心理学研判·教师心理学研判·心理学知识研判
中科微星26 分钟前
相位型SLM硬件产品面型性能提升
图像处理·人工智能·深度学习
AI2024081429 分钟前
众数信科AI智能体政务服务解决方案——寻知智能笔录系统
人工智能·政务
VB.Net1 小时前
EmguCV学习笔记 VB.Net 12.3 OCR
opencv·计算机视觉·c#·ocr·图像·vb.net·emgucv
叫我:松哥1 小时前
基于python flask的高血压疾病预测分析与可视化系统的设计与实现,使用随机森林、决策树、逻辑回归、xgboost等机器学习库预测
python·决策树·随机森林·机器学习·数据分析·flask·逻辑回归