C# Onnx 使用onnxruntime部署实时视频帧插值

news/发布时间2024/5/15 12:37:15

目录

介绍

效果

模型信息

项目

代码

下载


C# Onnx 使用onnxruntime部署实时视频帧插值

介绍

github地址:https://github.com/google-research/frame-interpolation

FILM: Frame Interpolation for Large Motion, In ECCV 2022.

The official Tensorflow 2 implementation of our high quality frame interpolation neural network. We present a unified single-network approach that doesn't use additional pre-trained networks, like optical flow or depth, and yet achieve state-of-the-art results. We use a multi-scale feature extractor that shares the same convolution weights across the scales. Our model is trainable from frame triplets alone.

FILM transforms near-duplicate photos into a slow motion footage that look like it is shot with a video camera.

效果

模型信息

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

Inputs
-------------------------
name:I0
tensor:Float[1, 3, -1, -1]
name:I1
tensor:Float[1, 3, -1, -1]
---------------------------------------------------------------

Outputs
-------------------------
name:merged
tensor:Float[1, -1, -1, -1]
---------------------------------------------------------------

项目

代码

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.Linq;
using System.Numerics;
using System.Windows.Forms;namespace Onnx_Yolov8_Demo
{public partial class Form1 : Form{public Form1(){InitializeComponent();}string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";string image_path = "";string startupPath;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;Tensor<float> input_tensor2;List<NamedOnnxValue> input_container;IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;DisposableNamedOnnxValue[] results_onnxvalue;Tensor<float> result_tensors;float[] result_array;float[] input1_image;float[] input2_image;int inpWidth;int inpHeight;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;}void Preprocess(Mat img, ref float[] input_img){Mat rgbimg = new Mat();Cv2.CvtColor(img, rgbimg, ColorConversionCodes.BGR2RGB);int h = rgbimg.Rows;int w = rgbimg.Cols;int align = 32;if (h % align != 0 || w % align != 0){int ph = ((h - 1) / align + 1) * align;int pw = ((w - 1) / align + 1) * align;Cv2.CopyMakeBorder(rgbimg, rgbimg, 0, ph - h, 0, pw - w, BorderTypes.Constant, 0);}inpHeight = rgbimg.Rows;inpWidth = rgbimg.Cols;rgbimg.ConvertTo(rgbimg, MatType.CV_32FC3, 1 / 255.0);int image_area = rgbimg.Rows * rgbimg.Cols;//input_img = new float[3 * image_area];input_img = Common.ExtractMat(rgbimg);}Mat Interpolate(Mat srcimg1, Mat srcimg2){int srch = srcimg1.Rows;int srcw = srcimg1.Cols;Preprocess(srcimg1, ref input1_image);Preprocess(srcimg2, ref input2_image);// 输入Tensorinput_tensor = new DenseTensor<float>(input1_image, new[] { 1, 3, inpHeight, inpWidth });input_tensor2 = new DenseTensor<float>(input2_image, new[] { 1, 3, inpHeight, inpWidth });//将tensor 放入一个输入参数的容器,并指定名称input_container.Add(NamedOnnxValue.CreateFromTensor("I0", input_tensor));input_container.Add(NamedOnnxValue.CreateFromTensor("I1", input_tensor2));//运行 Inference 并获取结果result_infer = onnx_session.Run(input_container);// 将输出结果转为DisposableNamedOnnxValue数组results_onnxvalue = result_infer.ToArray();// 读取第一个节点输出并转为Tensor数据result_tensors = results_onnxvalue[0].AsTensor<float>();int out_h = results_onnxvalue[0].AsTensor<float>().Dimensions[2];int out_w = results_onnxvalue[0].AsTensor<float>().Dimensions[3];result_array = result_tensors.ToArray();for (int i = 0; i < result_array.Length; i++){result_array[i] = result_array[i] * 255;if (result_array[i] < 0){result_array[i] = 0;}else if (result_array[i] > 255){result_array[i] = 255;}result_array[i] = result_array[i] + 0.5f;}float[] temp_r = new float[out_h * out_w];float[] temp_g = new float[out_h * out_w];float[] temp_b = new float[out_h * out_w];Array.Copy(result_array, temp_r, out_h * out_w);Array.Copy(result_array, out_h * out_w, temp_g, 0, out_h * out_w);Array.Copy(result_array, out_h * out_w * 2, temp_b, 0, out_h * out_w);Mat rmat = new Mat(out_h, out_w, MatType.CV_32F, temp_r);Mat gmat = new Mat(out_h, out_w, MatType.CV_32F, temp_g);Mat bmat = new Mat(out_h, out_w, MatType.CV_32F, temp_b);result_image = new Mat();Cv2.Merge(new Mat[] { bmat, gmat, rmat }, result_image);result_image.ConvertTo(result_image, MatType.CV_8UC3);Mat mid_img = new Mat(result_image, new Rect(0, 0, srcw, srch));return mid_img;}private void button2_Click(object sender, EventArgs e){button2.Enabled = false;pictureBox2.Image = null;textBox1.Text = "正在运行,请稍后……";Application.DoEvents();dt1 = DateTime.Now;List<String> inputs_imgpath = new List<String>() { "test_img/frame07.png", "test_img/frame08.png", "test_img/frame09.png", "test_img/frame10.png", "test_img/frame11.png", "test_img/frame12.png", "test_img/frame13.png", "test_img/frame14.png" };int imgnum = inputs_imgpath.Count();for (int i = 0; i < imgnum - 1; i++){Mat srcimg1 = Cv2.ImRead(inputs_imgpath[i]);Mat srcimg2 = Cv2.ImRead(inputs_imgpath[i + 1]);Mat mid_img = Interpolate(srcimg1, srcimg2);string save_imgpath = "imgs_results/mid" + i + ".jpg";Cv2.ImWrite(save_imgpath, mid_img);}dt2 = DateTime.Now;textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";button2.Enabled = true;}private void Form1_Load(object sender, EventArgs e){model_path = "model/RIFE_HDv3.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模型文件的路径// 创建输入容器input_container = new List<NamedOnnxValue>();pictureBox1.Image = new Bitmap("test_img/frame11.png");pictureBox3.Image = new Bitmap("test_img/frame12.png");}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);}}private void button4_Click(object sender, EventArgs e){button2.Enabled = false;pictureBox2.Image = null;textBox1.Text = "正在运行,请稍后……";Application.DoEvents();dt1 = DateTime.Now;Mat srcimg1 = Cv2.ImRead("test_img/frame11.png");Mat srcimg2 = Cv2.ImRead("test_img/frame12.png");Mat mid_img = Interpolate(srcimg1, srcimg2);dt2 = DateTime.Now;pictureBox2.Image = new Bitmap(mid_img.ToMemoryStream());textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";button2.Enabled = true;}}
}

下载

源码下载

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.bcls.cn/OloF/9405.shtml

如若内容造成侵权/违法违规/事实不符,请联系编程老四网进行投诉反馈email:xxxxxxxx@qq.com,一经查实,立即删除!

相关文章

五.AV Foundation 视频播放 - 标题和字幕

引言 本篇博客主要介绍使用AV Foundation加载视频资源的时候&#xff0c;如何获取视频标题&#xff0c;获取字幕并让其显示到播放界面。 设置标题 资源标题的元数据内容&#xff0c;我们需要从资源的commonMetadata中获取&#xff0c;在加载AVPlayerItem的时候我们已经指定了…

【Vue】路由

&#x1f4dd;个人主页&#xff1a;五敷有你 &#x1f525;系列专栏&#xff1a;Vue ⛺️稳中求进&#xff0c;晒太阳 目录 路由 单页应用程序 总结&#xff1a; VueRouter 核心步骤&#xff1a; 组件存放目录的问题 路由的封装 声明式导航 声明式导航 - 导航链…

二叉树(1)

目录 1. 树型结构 1.1 概念 1.2 概念 1.3 树的表示形式 ​编辑 2. 二叉树 2.1 概念 2.2 两种特殊的二叉树 2.3 二叉树的性质 2.4 二叉树的存储 2.5 二叉树的基本操作 2.5.1 前置说明 2.5.2 二叉树的遍历 1. NLR&#xff1a;前序遍历(亦称先序遍历): 2. LNR&#xff1a;中序遍历…

Unity中URP实现水体(整理优化)

文章目录 前言一、优化水的深度1、我们把 水流动的方向 和 水深浅过渡值&#xff0c;整合到一个四维变量中2、修改 水体流动方向3、在片元着色器中&#xff0c;修改使用过渡变量 二、优化泡沫三、优化水下的扭曲1、修复原本扰动UV的计算 四、优化水面高光1、把高光强度、光滑度…

Leetcoder Day29| 贪心算法part03

1005.K次取反后最大化的数组和 给定一个整数数组 A&#xff0c;我们只能用以下方法修改该数组&#xff1a;我们选择某个索引 i 并将 A[i] 替换为 -A[i]&#xff0c;然后总共重复这个过程 K 次。&#xff08;我们可以多次选择同一个索引 i。&#xff09; 以这种方式修改数组后&a…

基于springboot+vue的街球社区网站(源码+论文)

文章目录 前言 一、功能设计 二、功能实现 系统首页设计 系统前台基本功能设计与实现 ​编辑 系统后台管理功能设计与实现 三、库表设计 四、论文 前言 本文主要讲述了基于SpringBootVue模式的街球社区网站的设计与实现。这里所谓的街球社区网站是通过类似于百度贴吧之类的网上…

大数据开发项目--音乐排行榜

环境&#xff1a;windows10&#xff0c;centos7.9&#xff0c;hadoop3.2、hbase2.5.3和zookeeper3.8完全分布式&#xff1b; 环境搭建具体操作请参考以下文章&#xff1a; CentOS7 Hadoop3.X完全分布式环境搭建 Hadoop3.x完全分布式环境搭建Zookeeper和Hbase 1. 集成MapReduce…

【设计模式】5种创建型模式详解

创建型模式提供创建对象的机制,能够提升已有代码的灵活性和复用性。 常用的有&#xff1a;单例模式、工厂模式&#xff08;工厂方法和抽象工厂&#xff09;、建造者模式。不常用的有&#xff1a;原型模式。 一、单例模式 1.1 单例模式介绍 1 ) 定义 单例模式&#xff08;Si…

Docker实战

目录 docker简介docker体系架构与基本概念安装docker使用APT方式安装Docker使用二进制文件方式安装&#xff08;可自行尝试&#xff09; Docker镜像什么是Docker镜像使用 Docker 默认的镜像存储路径自定义 Docker 的镜像存储路径&#xff08;不推荐&#xff0c;故不做演示&…

git之分支管理

一.理解分支 我们看下面这张图片&#xff1a; 在版本回退⾥&#xff0c;你已经知道&#xff0c;每次提交&#xff0c;Git都把它们串成⼀条时间线&#xff0c;这条时间线就可以理解为是⼀个分⽀。截⽌到⽬前&#xff0c;只有⼀条时间线&#xff0c;在Git⾥&#xff0c;这个分⽀…

Leetcode : 215. 数组中的第 K 个最大元素

给定整数数组 nums 和整数 k&#xff0c;请返回数组中第 k 个最大的元素。 请注意&#xff0c;你需要找的是数组排序后的第 k 个最大的元素&#xff0c;而不是第 k 个不同的元素。 你必须设计并实现时间复杂度为 O(n) 的算法解决此问题。 思路&#xff1a;最开始排序算法&…

Python多功能课堂点名器、抽签工具

一、问题缘起 去年&#xff0c;ChatGPT浪潮袭来&#xff0c;我懂简单的Python基础语法&#xff0c;又有一些点子&#xff0c;于是借助于人工智能问答工具&#xff0c;一步一步地制作了一个点名器&#xff0c;也可以用于抽签。当时&#xff0c;我已经设计好页面和基础的功能&am…

Windows虚拟主机如何开启网页debug模式

前不久&#xff0c;有客户咨询想要知道如何开启网页debug模式,以便后期他网站出现异常可以自行排查。这边了解到他当前使用的是Hostease 的Windows 虚拟主机&#xff0c;而开启网页debug模式的操作步骤如下&#xff1a; 1.Hostease的Windows虚拟主机都是带Plesk面板的,因此需要…

什么是物联网网关?

随着物联网的发展&#xff0c;企业发现自己面临着集成多种设备和协议的挑战&#xff0c;其中许多设备和协议具有不同的功率和连接要求。这种组合还可能包括遗留技术。物联网网关正在成为构建强大物联网和在边缘计算场景中提供计算能力的重要组成部分。 物联网网关执行哪些功能&…

spring Boot快速入门

快速入门为主主要届介绍java web接口API的编写 java编辑器首选IntelliJ IDEA 官方链接&#xff1a;https://www.jetbrains.com/idea/ IEDA 前言 实例项目主要是web端API接口的使用&#xff0c;项目使用mysql数据库&#xff0c;把从数据库中的数据的查询出来后通过接口json数…

数据结构day4

实现创建单向循环链表、创建结点、判空、输出、头插、按位置插入、尾删、按位置删除 loop_list.c #include "loop_list.h" loop_p create_head() {loop_p L(loop_p)malloc(sizeof(loop_list));if(LNULL){printf("空间申请失败\n");return NULL;}L->le…

网络原理 - HTTP/HTTPS(5)

HTTPS HTTPS也是一个应用层协议.在HTTP协议的基础上引入了一个加密层. HTTP协议内容都是按照文本的方式明文传输的. 这就导致了在传输过程中出现了一些被篡改的情况. 臭名昭著的"运营商劫持" 下载一个天天动听. 未被劫持的效果,点击下载按钮,就会弹出天天动听的…

解决鸿蒙模拟器卡顿的问题

缘起 最近在学习鸿蒙的时候&#xff0c;发现模拟器非常卡&#xff0c;不要说体验到鸿蒙的丝滑&#xff0c;甚至到严重影响使用的程度。 根据我开发Android的经验和在论坛翻了一圈&#xff0c;最终总结出了以下几个方案。 创建模拟器 1、在DevEco Virtual Device Configurat…

JOISC2022 复制粘贴(区间DP,字符串hash)

题目描述 题面 分析 这道题考场没有任何头绪&#xff0c;赛后也是看了许多题解才明白状态设计和转移的一步步思考过程。 首先我们需要想到 无论是屏幕上的字符串&#xff0c;还是剪切板上的字符串&#xff0c;在任何时候都必须是目标串的子串。这个非常好像&#xff0c;如果不…

自动驾驶消息传输机制-LCM

需要用到LCM消息通讯&#xff0c;遂研究下。 这里写目录标题 1 LCM简介2. LCM源码分析3 LCM C教程与实例3.1 安装配置及介绍3.2 创建类型定义3.3 初始化LCM3.4 发布publish一个消息3.5 订阅和接收一个消息3.6 LCM进程间通讯3.7 注意事项&#xff1f;3.7.1 当数据结构定义的是数…
推荐文章