介绍:
地址:C#https://github.com/ruotianluo/ImageCaptioning.pytorch
效果:
测试环境:
vs2019
onnxruntime1.16.3
opencvsharp4.8
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FIRC;
using OpenCvSharp;
namespace imagecaptiondemo
{
public partial class Form1 : Form
{
ImageCaptionManager icm = new ImageCaptionManager();
Mat curMat = new Mat();
public Form1()
{
InitializeComponent();
}
private void btn_select_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "图片文件(*.*)|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
ofd.Multiselect = false;
if (ofd.ShowDialog() != DialogResult.OK) return;
curMat = Cv2.ImRead(ofd.FileName);
pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(curMat);
}
private void Form1_Load(object sender, EventArgs e)
{
icm.LoadWeights();
}
private void button2_Click(object sender, EventArgs e)
{
if(pictureBox1.Image==null)
{
return;
}
Stopwatch sw = new Stopwatch();
sw.Start();
var result = icm.Inference(curMat);
sw.Stop();
tb_res.Text = "推理耗时:" + sw.Elapsed.TotalSeconds+"秒\r\n推理结果:"+result;
}
}
}
?视频演示:
源码下载地址: