C# Image Caption

目录

介绍

效果

模型

decoder_fc_nsc.onnx

encoder.onnx

项目

代码

下载


C# Image Caption

介绍

地址:https://github.com/ruotianluo/ImageCaptioning.pytorch

I decide to sync up this repo and self-critical.pytorch. (The old master is in old master branch for archive)

效果

模型

decoder_fc_nsc.onnx

Inputs


name:fc_feats

tensor:Float[1, 2048]


Outputs


name:seq

tensor:Int64[1, 20]

name:logprobs

tensor:Float[1, 20, 9488]


encoder.onnx

Inputs


name:img

tensor:Float[1, 3, 640, 640]


Outputs


name:fc

tensor:Float[2048]


项目

代码

using Microsoft.ML.OnnxRuntime;

using Microsoft.ML.OnnxRuntime.Tensors;

using OpenCvSharp;

using OpenCvSharp.Dnn;

using System;

using System.Collections.Generic;

using System.Drawing;

using System.Drawing.Imaging;

using System.IO;

using System.Linq;

using System.Windows.Forms;

namespace ImageCaption

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

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

string image_path = "";

string startupPath;

string classer_path;

DateTime dt1 = DateTime.Now;

DateTime dt2 = DateTime.Now;

string model_path;

Mat image;

Mat result_image;

SessionOptions options;

InferenceSession onnx_session;

Tensor<float> input_tensor;

List<NamedOnnxValue> input_container;

IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;

DisposableNamedOnnxValue[] results_onnxvalue;

Tensor<Int64> result_tensors;

Net net;

int feat_len;

int D;

int inpWidth = 640;

int inpHeight = 640;

float[] mean = new float[] { 0.485f, 0.456f, 0.406f };

float[] std = new float[] { 0.229f, 0.224f, 0.225f };

Dictionary<string, string> ix_to_word = new Dictionary<string, string>();

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;

}

private unsafe void button2_Click(object sender, EventArgs e)

{

if (image_path == "")

{

return;

}

button2.Enabled = false;

pictureBox2.Image = null;

textBox1.Text = "";

pictureBox2.Image = null;

Application.DoEvents();

//图片缩放

image = new Mat(image_path);

Mat temp_image = new Mat();

Cv2.Resize(image, temp_image, new OpenCvSharp.Size(inpWidth, inpHeight));

Normalize(temp_image);

Mat blob = CvDnn.BlobFromImage(temp_image);

//配置图片输入数据

net.SetInput(blob);

Mat result_mat = net.Forward();

float* ptr_feat = (float*)result_mat.Data;

for (int i = 0; i < 2048; i++)

{

input_tensor[0, i] = ptr_feat[i];

}

//将 input_tensor 放入一个输入参数的容器,并指定名称

input_container.Add(NamedOnnxValue.CreateFromTensor("fc_feats", input_tensor));

//运行 Inference 并获取结果

result_infer = onnx_session.Run(input_container);

// 将输出结果转为DisposableNamedOnnxValue数组

results_onnxvalue = result_infer.ToArray();

// 读取第一个节点输出并转为Tensor数据

result_tensors = results_onnxvalue[0].AsTensor<Int64>();

Int64[] result_array = result_tensors.ToArray();

string words = "";

for (int k = 0; k < D; k++)

{

if (result_array[k] > 0)

{

if (words.Length > 0)

{

words += " ";

}

words += ix_to_word[result_array[k].ToString()];

}

else

{

break;

}

}

result_image = image.Clone();

Cv2.PutText(result_image, words

, new OpenCvSharp.Point(10, 60)

, HersheyFonts.HersheySimplex

, 1

, new Scalar(0, 0, 255)

, 2

);

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

textBox1.Text = words;

button2.Enabled = true;

}

public void Normalize(Mat src)

{

src.ConvertTo(src, MatType.CV_32FC3, 1.0 / 255);

Mat[] bgr = src.Split();

for (int i = 0; i < bgr.Length; ++i)

{

bgr[i].ConvertTo(bgr[i], MatType.CV_32FC1, 1 / std[i], (0.0 - mean[i]) / std[i]);

}

Cv2.Merge(bgr, src);

foreach (Mat channel in bgr)

{

channel.Dispose();

}

}

private void Form1_Load(object sender, EventArgs e)

{

startupPath = System.Windows.Forms.Application.StartupPath;

model_path = "model/decoder_fc_nsc.onnx";

// 创建输出会话,用于输出模型读取信息

options = new SessionOptions();

options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;

options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行

// 创建推理模型类,读取本地模型文件

onnx_session = new InferenceSession(model_path, options);//model_path 为onnx模型文件的路径

// 输入Tensor

input_tensor = new DenseTensor<float>(new[] { 1, 2048 });

// 创建输入容器

input_container = new List<NamedOnnxValue>();

feat_len = 2048;

D = 20;

//初始化网络类,读取本地模型

net = CvDnn.ReadNetFromOnnx("model/encoder.onnx");

StreamReader sr = new StreamReader("model/vocab.txt");

string line;

while ((line = sr.ReadLine()) != null)

{

ix_to_word.Add(line.Split(':')[0], line.Split(':')[1]);

}

image_path = "test_img/1.jpg";

pictureBox1.Image = new Bitmap(image_path);

image = new Mat(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);

}

}

}

}

cs 复制代码
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Windows.Forms;

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

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

        SessionOptions options;
        InferenceSession onnx_session;
        Tensor<float> input_tensor;
        List<NamedOnnxValue> input_container;
        IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;
        DisposableNamedOnnxValue[] results_onnxvalue;

        Tensor<Int64> result_tensors;

        Net net;

        int feat_len;
        int D;
        int inpWidth = 640;
        int inpHeight = 640;
        float[] mean = new float[] { 0.485f, 0.456f, 0.406f };
        float[] std = new float[] { 0.229f, 0.224f, 0.225f };

        Dictionary<string, string> ix_to_word = new Dictionary<string, string>();

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

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

            button2.Enabled = false;
            pictureBox2.Image = null;
            textBox1.Text = "";
            pictureBox2.Image = null;
            Application.DoEvents();

            //图片缩放
            image = new Mat(image_path);

            Mat temp_image = new Mat();
            Cv2.Resize(image, temp_image, new OpenCvSharp.Size(inpWidth, inpHeight));
            Normalize(temp_image);

            Mat blob = CvDnn.BlobFromImage(temp_image);

            //配置图片输入数据
            net.SetInput(blob);

            Mat result_mat = net.Forward();

            float* ptr_feat = (float*)result_mat.Data;

            for (int i = 0; i < 2048; i++)
            {
                input_tensor[0, i] = ptr_feat[i];
            }

            //将 input_tensor 放入一个输入参数的容器,并指定名称
            input_container.Add(NamedOnnxValue.CreateFromTensor("fc_feats", input_tensor));

            //运行 Inference 并获取结果
            result_infer = onnx_session.Run(input_container);

            // 将输出结果转为DisposableNamedOnnxValue数组
            results_onnxvalue = result_infer.ToArray();

            // 读取第一个节点输出并转为Tensor数据
            result_tensors = results_onnxvalue[0].AsTensor<Int64>();

            Int64[] result_array = result_tensors.ToArray();

            string words = "";
            for (int k = 0; k < D; k++)
            {
                if (result_array[k] > 0)
                {
                    if (words.Length > 0)
                    {
                        words += " ";
                    }
                    words += ix_to_word[result_array[k].ToString()];
                }
                else
                {
                    break;
                }
            }

            result_image = image.Clone();

            Cv2.PutText(result_image, words
                , new OpenCvSharp.Point(10, 60)
                , HersheyFonts.HersheySimplex
                , 1
                , new Scalar(0, 0, 255)
                , 2
                );

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

            textBox1.Text = words;

            button2.Enabled = true;
        }

        public void Normalize(Mat src)
        {
            src.ConvertTo(src, MatType.CV_32FC3, 1.0 / 255);

            Mat[] bgr = src.Split();
            for (int i = 0; i < bgr.Length; ++i)
            {
                bgr[i].ConvertTo(bgr[i], MatType.CV_32FC1, 1 / std[i], (0.0 - mean[i]) / std[i]);
            }

            Cv2.Merge(bgr, src);

            foreach (Mat channel in bgr)
            {
                channel.Dispose();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            startupPath = System.Windows.Forms.Application.StartupPath;

            model_path = "model/decoder_fc_nsc.onnx";

            // 创建输出会话,用于输出模型读取信息
            options = new SessionOptions();
            options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
            options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行

            // 创建推理模型类,读取本地模型文件
            onnx_session = new InferenceSession(model_path, options);//model_path 为onnx模型文件的路径

            // 输入Tensor
            input_tensor = new DenseTensor<float>(new[] { 1, 2048 });
            // 创建输入容器
            input_container = new List<NamedOnnxValue>();

            feat_len = 2048;
            D = 20;

            //初始化网络类,读取本地模型
            net = CvDnn.ReadNetFromOnnx("model/encoder.onnx");

            StreamReader sr = new StreamReader("model/vocab.txt");
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                ix_to_word.Add(line.Split(':')[0], line.Split(':')[1]);
            }

            image_path = "test_img/1.jpg";
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(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);
            }
        }
    }
}

下载

源码下载

相关推荐
DashVector6 分钟前
如何通过HTTP API插入或更新Doc
大数据·数据库·数据仓库·人工智能·http·数据库架构·向量检索
海棠AI实验室8 分钟前
机器学习基础算法 (二)-逻辑回归
人工智能·python·机器学习
利哥AI实例探险14 分钟前
建筑工地AI安全检测系统:YOLO11数据标注训练与PyQt5界面设计应用开发
人工智能·qt·安全
临渊羡鱼的猫18 分钟前
基础优化方法
人工智能·深度学习·机器学习
吃个糖糖22 分钟前
33 Opencv ShiTomasi角点检测
人工智能·opencv·计算机视觉
爱研究的小牛40 分钟前
Pika Labs技术浅析(四):数据可视化
神经网络·计算机视觉·信息可视化·aigc
AI明说44 分钟前
DDI-GPT:使用知识图谱增强的大模型对药物相互作用进行可解释的预测
人工智能·gpt·大模型·知识图谱·数智药师·数智药学
视觉&物联智能1 小时前
【杂谈】-AI搜索引擎如何改变传统SEO及其在内容营销中的作用
人工智能·搜索引擎·ai·aigc·seo
九河云1 小时前
Amazon Bedrock Claude 3 在客户服务自动化中的应用方法
运维·人工智能·自动化·aws
文心快码 Baidu Comate1 小时前
打破视障壁垒,百度文心快码无障碍版本助力视障IT从业者就业无“碍”
人工智能·ai编程·文心快码·智能编程助手·智能代码助手