LaMa Image Inpainting 图像修复 Onnx Demo

news2024/9/21 11:03:40

目录

介绍

效果 

模型信息

项目

代码

下载


LaMa Image Inpainting 图像修复 Onnx Demo

介绍

gihub地址:https://github.com/advimman/lama

🦙 LaMa Image Inpainting, Resolution-robust Large Mask Inpainting with Fourier Convolutions, WACV 2022

效果 

模型信息

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

Inputs
-------------------------
name:image
tensor:Float[1, 3, 1000, 1504]
name:mask
tensor:Float[1, 1, 1000, 1504]
---------------------------------------------------------------

Outputs
-------------------------
name:inpainted
tensor:Float[1, 1000, 1504, 3]
---------------------------------------------------------------

项目

代码

using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

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

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

        StringBuilder sb = new StringBuilder();

        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 void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

            button2.Enabled = false;
            pictureBox2.Image = null;
            textBox1.Text = "";

            image = new Mat(image_path);
            int w = image.Width;
            int h = image.Height;
            image_mask = new Mat(image_path_mask);

            Common.Preprocess(image, image_mask, input_tensor, input_tensor_mask);

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

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

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

            Mat result = Common.Postprocess(result_infer);

            Cv2.Resize(result, result, new OpenCvSharp.Size(w, h));

            sb.AppendLine("推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");

            pictureBox2.Image = new Bitmap(result.ToMemoryStream());
            textBox1.Text = sb.ToString();

            button2.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            model_path = "model/big_lama_regular_inpaint.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, 3, 1000, 1504 });

            input_tensor_mask = new DenseTensor<float>(new[] { 1, 1, 1000, 1504 });

            // 创建输入容器
            input_container = new List<NamedOnnxValue>();

            image_path = "test_img/test.jpg";
            pictureBox1.Image = new Bitmap(image_path);

            image_path_mask = "test_img/mask.jpg";
            pictureBox3.Image = new Bitmap(image_path_mask);
        }
    }
}

using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

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

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

        StringBuilder sb = new StringBuilder();

        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 void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

            button2.Enabled = false;
            pictureBox2.Image = null;
            textBox1.Text = "";

            image = new Mat(image_path);
            int w = image.Width;
            int h = image.Height;
            image_mask = new Mat(image_path_mask);

            Common.Preprocess(image, image_mask, input_tensor, input_tensor_mask);

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

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

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

            Mat result = Common.Postprocess(result_infer);

            Cv2.Resize(result, result, new OpenCvSharp.Size(w, h));

            sb.AppendLine("推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");

            pictureBox2.Image = new Bitmap(result.ToMemoryStream());
            textBox1.Text = sb.ToString();

            button2.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            model_path = "model/big_lama_regular_inpaint.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, 3, 1000, 1504 });

            input_tensor_mask = new DenseTensor<float>(new[] { 1, 1, 1000, 1504 });

            // 创建输入容器
            input_container = new List<NamedOnnxValue>();

            image_path = "test_img/test.jpg";
            pictureBox1.Image = new Bitmap(image_path);

            image_path_mask = "test_img/mask.jpg";
            pictureBox3.Image = new Bitmap(image_path_mask);
        }
    }
}

Common.cs

using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Onnx_Demo
{
    internal class Common
    {
        public static void Preprocess(Mat image, Mat image_mask,  Tensor<float> input_tensor, Tensor<float> input_tensor_mask)
        {
            Cv2.Resize(image, image, new OpenCvSharp.Size(1504, 1000));
            // 输入Tensor
            for (int y = 0; y < image.Height; y++)
            {
                for (int x = 0; x < image.Width; x++)
                {
                    input_tensor[0, 0, y, x] = image.At<Vec3b>(y, x)[0] / 255.0f;
                    input_tensor[0, 1, y, x] = image.At<Vec3b>(y, x)[1] / 255.0f;
                    input_tensor[0, 2, y, x] = image.At<Vec3b>(y, x)[2] / 255.0f;
                }
            }

            Cv2.Resize(image_mask, image_mask, new OpenCvSharp.Size(1504, 1000));
            //膨胀核函数
            Mat element1 = new Mat();
            OpenCvSharp.Size size1 = new OpenCvSharp.Size(11, 11);
            element1 = Cv2.GetStructuringElement(MorphShapes.Rect, size1);

            //膨胀一次,让轮廓突出
            Mat dilation = new Mat();
            Cv2.Dilate(image_mask, image_mask, element1);

            //输入Tensor
            for (int y = 0; y < image_mask.Height; y++)
            {
                for (int x = 0; x < image_mask.Width; x++)
                {
                    float v = image_mask.At<Vec3b>(y, x)[0];
                    if (v > 127)
                    {
                        input_tensor_mask[0, 0, y, x] = 1.0f;
                    }
                    else
                    {
                        input_tensor_mask[0, 0, y, x] = 0.0f;
                    }
                }
            }

        }

        public static Mat Postprocess(IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer)
        {
            // 将输出结果转为DisposableNamedOnnxValue数组
            DisposableNamedOnnxValue[] results_onnxvalue = result_infer.ToArray();

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

            float[] result_array = result_tensors.ToArray();

            for (int i = 0; i < result_array.Length; i++)
            {
                result_array[i] = Math.Max(0, Math.Min(255, result_array[i]));
            }

            Mat result = new Mat(1000, 1504, MatType.CV_32FC3, result_array);

            return result;
        }
    }
}

下载

源码下载

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【InternLM 实战营笔记】浦语·灵笔的图文理解及创作部署、 Lagent 工具调用 Demo

浦语灵笔的图文理解及创作部署 浦语灵笔是基于书生浦语大语言模型研发的视觉-语言大模型&#xff0c;提供出色的图文理解和创作能力&#xff0c;结合了视觉和语言的先进技术&#xff0c;能够实现图像到文本、文本到图像的双向转换。使用浦语灵笔大模型可以轻松的创作一篇图文推…

CrossOver 24下载-CrossOver 24 for Mac下载 v24.0.0中文永久版

CrossOver 24是一款可以让mac用户能够自由运行和游戏windows游戏软件的虚拟机类应用&#xff0c;虽然能够虚拟windows但是却并不是一款虚拟机&#xff0c;也不需要重启系统或者启动虚拟机&#xff0c;类似于一种能够让mac系统直接运行windows软件的插件。它以其出色的跨平台兼容…

(二十二)devops持续集成开发——jenkins服务代理Agent搭建

前言 在Jenkins 中&#xff0c;代理&#xff08;Agent&#xff09;是一种用于执行构建、部署和其他任务的计算节点。代理节点可以是物理机器、虚拟机或容器&#xff0c;它们负责接收 Jenkins 主控节点委派的任务并执行这些任务。通过使用代理节点&#xff0c;可以有效地分担Je…

java基础(4)注解,集合,

注解 什么是注解&#xff08;Annotation&#xff09;&#xff1f;注解是放在Java源码的类、方法、字段、参数前的一种特殊“注释” // this is a component: Resource("hello") public class Hello {Injectint n;PostConstructpublic void hello(Param String name…

【嵌入式——QT】数值输入和显示组件

数值输入和显示组件 QSlider&#xff1a;滑动条&#xff0c;通过滑动来设置数值&#xff1b;QScrollBar&#xff1a;卷滚条&#xff0c;与QSlider类似&#xff0c;还可以用于卷滚区域&#xff1b;QProgressBar&#xff1a;进度条&#xff0c;一般用于显示任务进度&#xff0c;…

2024年3月2日 十二生肖 今日运势

小运播报&#xff1a;2024年3月2日&#xff0c;星期六&#xff0c;农历正月廿二 &#xff08;甲辰年丙寅月乙丑日&#xff09;&#xff0c;法定节假日。 红榜生肖&#xff1a;鸡、蛇、鼠 需要注意&#xff1a;狗、马、羊 喜神方位&#xff1a;西北方 财神方位&#xff1a;东…

AI大模型提供商有哪些?

AI大模型提供商&#xff1a;引领人工智能创新浪潮 随着人工智能技术的迅猛发展&#xff0c;AI大模型成为了推动行业变革和创新的核心驱动力之一。作为AI领域的重要参与者&#xff0c;AI大模型提供商扮演着关键的角色。本文将围绕这一主题&#xff0c;介绍几家在AI大模型领域具…

SpringBoot3-数据访问

整合SSM场景 SpringBoot 整合 Spring、SpringMVC、MyBatis 进行数据访问场景开发 1. 创建SSM整合项目 <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency><groupId>org.mybatis.spring.boot&…

论文阅读:2020GhostNet华为轻量化网络

创新&#xff1a;&#xff08;1&#xff09;对卷积进行改进&#xff08;2&#xff09;加残差连接 1、Ghost Module 1、利用1x1卷积获得输入特征的必要特征浓缩。利用1x1卷积对我们输入进来的特征图进行跨通道的特征提取&#xff0c;进行通道的压缩&#xff0c;获得一个特征浓…

基于springboot+html实现的衣物捐赠平台

一、系统架构 前端&#xff1a;html | layui | jquery | css 后端&#xff1a;springboot | thymeleaf | mybatis 环境&#xff1a;jdk1.8 | mysql | maven 二、代码及数据库 三、功能介绍 01. 登录页 02. 注册 03. web页-首页 04. web页-捐赠衣服 05. web页-论坛交流…

echarts鼠标向右/向左绘制实现放大/还原

echarts toolbox 的datazoom提供了绘制放大的功能&#xff0c;但通过鼠标绘制只能进行放大 应需求放大与还原都通过鼠标行为实现&#xff0c;增加从右往左绘制时还原放大结果 demo 结果 重写datazoom的原型方法实现绘制事件的拦截 const comp myChart._model.getComponent(to…

存储过程基本了解

文章目录 介绍存储过程示例1. 目的2. 输入参数3. 输出参数4. 执行逻辑5. 返回值6. 示例用法7. 注意事项 存储过程的关键字有哪些简单实操 介绍 存储过程是一组预编译的SQL语句&#xff0c;以及流程控制语句&#xff0c;封装在数据库服务器中并可以被重复调用。它们可以接收参数…

仿牛客网项目---私信列表和发送列表功能的实现

这篇文章我们来讲一下我的这个项目的另外一个功能&#xff1a;私信列表和发送列表功能。 先来设计DAO层。 Mapper public interface MessageMapper {// 查询当前用户的会话列表,针对每个会话只返回一条最新的私信.List<Message> selectConversations(int userId, int of…

WIN10 无密码自动登录

1、家里重装了一下WIN10系统&#xff0c;第一次登陆居然用了微软网站账号&#xff0c;结果密码忘记了&#xff0c;后面只能用PIN码登陆系统。 2、需要登录微软的网站修改密码&#xff1a; Microsoft account | Sign In or Create Your Account Today – Microsoft 3、在运行…

精品ssm的社区团购系统购物商城小程序

《[含文档PPT源码等]精品基于ssm的社区团购系统[包运行成功]》该项目含有源码、文档、PPT、配套开发软件、软件安装教程、项目发布教程、包运行成功&#xff01; 软件开发环境及开发工具&#xff1a; Java——涉及技术&#xff1a; 前端使用技术&#xff1a;HTML5,CSS3、Jav…

SpringBoot 整合WebService

文章目录 WebService1.简单介绍WebService1.1. 类型1.2. 架构1.3. 主要特点1.4. 使用场景1.5. Web服务标准和技术 2.案例-WebServiceDemo2.1.引入配置文件2.2.创建接口2.3.创建接口实现类2.4.创建WebService配置类2.5.测试 WebService Web服务&#xff08;Web Services&#xf…

kotlin安卓开发教程视频,2024年Android开发陷入饱和

Android基础 1、什么是ANR 如何避免它&#xff1f; 如果耗时操作需要让用户等待&#xff0c;那么可以在界面上显示进度条。 2、View的绘制流程&#xff1b;自定义View如何考虑机型适配&#xff1b;自定义View的事件 3、分发机制&#xff1b;View和ViewGroup分别有哪些事件分…

蓝桥杯备战刷题three(自用)

1.合法日期 #include <iostream> #include <map> #include <string> using namespace std; int main() {map<string,int>mp;int days[13]{0,31,28,31,30,31,30,31,31,30,31,30,31};for(int i1;i<12;i){for(int j1;j<days[i];j){string sto_strin…

在线ai写作,让你随时随地创作优质内容

如今的ai技术已经渗透到我们生活的方方面面。其中&#xff0c;AI写作成为了一个备受关注的领域。如今&#xff0c;我们可以利用在线ai写作在任何时间、任何地点创作出优质的内容。 传统的写作过程需要大量的时间和精力。从构思到写作再到修改&#xff0c;每一个环节都需要我们投…

C# 解决uploadify插件上传时造成session丢失问题

出现的问题&#xff1a; 在应用uploadify插件实现上传图片时&#xff0c;报了HTTP Error&#xff0c;经过在Network查看上传方法报错码是302&#xff0c;那这里就可以知道问题是什么了&#xff0c;HTTP 302是请求被重定向&#xff0c;如果你的uploadify处理上传方法有session验…