LaMa Image Inpainting 图像修复 TensorRT Demo

LaMa Image Inpainting 图像修复 TensorRT Demo

目录

效果

项目

代码

下载


效果

项目

NVIDIA GeForce RTX 4060 Laptop GPU
cuda12.1+cudnn 8.8.1

代码

using OpenCvSharp;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using TensorRtSharp.Custom;

namespace LaMa_Image_Inpainting_图像修复_TensorRT_Demo
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        string image_path = "";
        string image_mask_path = "";
        string model_path;
        Mat image;
        Mat image_mask;

        StringBuilder sb = new StringBuilder();

        private Nvinfer predictor;

        private void button2_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
            {
                return;
            }

            pictureBox2.Image = null;
            textBox1.Text = "";
            sb.Clear();

            Application.DoEvents();

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

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            float[] input_tensor_data, input_mask_data;

            Common.Preprocess(image, image_mask, out input_tensor_data, out input_mask_data);

            predictor.LoadInferenceData("image", input_tensor_data);
            predictor.LoadInferenceData("mask", input_mask_data);

            double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Restart();

            predictor.infer();

            double inferTime = stopwatch.Elapsed.TotalMilliseconds;

            stopwatch.Restart();

            float[] outputData = predictor.GetInferenceResult("inpainted");

            Mat result = Common.Postprocess(outputData, w, h);

            double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Stop();

            double totalTime = preprocessTime + inferTime + postprocessTime;

            pictureBox2.Image = new Bitmap(result.ToMemoryStream());
            sb.AppendLine($"Preprocess: {preprocessTime:F2}ms");
            sb.AppendLine($"Infer: {inferTime:F2}ms");
            sb.AppendLine($"Postprocess: {postprocessTime:F2}ms");
            sb.AppendLine($"Total: {totalTime:F2}ms");
            textBox1.Text = sb.ToString();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            image_path = "test_img/test.jpg";
            pictureBox1.Image = new Bitmap(image_path);

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

            model_path = "model/big_lama_regular_inpaint.engine";

            if (File.Exists(model_path))
            {
            }
            else
            {
                Nvinfer.OnnxToEngine("model/big_lama_regular_inpaint.onnx", 20);
            }

            predictor = new Nvinfer(model_path);

        }

        float[] input_tensor_data, input_mask_data;

        private void button1_Click(object sender, EventArgs e)
        {
            image = new Mat(image_path);
            int w = image.Width;
            int h = image.Height;
            image_mask = new Mat(image_mask_path);

            Common.Preprocess(image, image_mask, out input_tensor_data, out input_mask_data);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            int index = 1;
            for (int i = 0; i < 100; i++)
            {
                stopwatch.Restart();
                predictor.LoadInferenceData("image", input_tensor_data);
                predictor.LoadInferenceData("mask", input_mask_data);

                predictor.infer();

                float[] outputData = predictor.GetInferenceResult("inpainted");
                Console.WriteLine("第" + index + "次,耗时:" + stopwatch.Elapsed.TotalMilliseconds);

                index++;
            }


        }
    }
}

using OpenCvSharp;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using TensorRtSharp.Custom;

namespace LaMa_Image_Inpainting_图像修复_TensorRT_Demo
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        string image_path = "";
        string image_mask_path = "";
        string model_path;
        Mat image;
        Mat image_mask;

        StringBuilder sb = new StringBuilder();

        private Nvinfer predictor;

        private void button2_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
            {
                return;
            }

            pictureBox2.Image = null;
            textBox1.Text = "";
            sb.Clear();

            Application.DoEvents();

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

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            float[] input_tensor_data, input_mask_data;

            Common.Preprocess(image, image_mask, out input_tensor_data, out input_mask_data);

            predictor.LoadInferenceData("image", input_tensor_data);
            predictor.LoadInferenceData("mask", input_mask_data);

            double preprocessTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Restart();

            predictor.infer();

            double inferTime = stopwatch.Elapsed.TotalMilliseconds;

            stopwatch.Restart();

            float[] outputData = predictor.GetInferenceResult("inpainted");

            Mat result = Common.Postprocess(outputData, w, h);

            double postprocessTime = stopwatch.Elapsed.TotalMilliseconds;
            stopwatch.Stop();

            double totalTime = preprocessTime + inferTime + postprocessTime;

            pictureBox2.Image = new Bitmap(result.ToMemoryStream());
            sb.AppendLine($"Preprocess: {preprocessTime:F2}ms");
            sb.AppendLine($"Infer: {inferTime:F2}ms");
            sb.AppendLine($"Postprocess: {postprocessTime:F2}ms");
            sb.AppendLine($"Total: {totalTime:F2}ms");
            textBox1.Text = sb.ToString();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            image_path = "test_img/test.jpg";
            pictureBox1.Image = new Bitmap(image_path);

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

            model_path = "model/big_lama_regular_inpaint.engine";

            if (File.Exists(model_path))
            {
            }
            else
            {
                Nvinfer.OnnxToEngine("model/big_lama_regular_inpaint.onnx", 20);
            }

            predictor = new Nvinfer(model_path);

        }

        float[] input_tensor_data, input_mask_data;

        private void button1_Click(object sender, EventArgs e)
        {
            image = new Mat(image_path);
            int w = image.Width;
            int h = image.Height;
            image_mask = new Mat(image_mask_path);

            Common.Preprocess(image, image_mask, out input_tensor_data, out input_mask_data);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            int index = 1;
            for (int i = 0; i < 100; i++)
            {
                stopwatch.Restart();
                predictor.LoadInferenceData("image", input_tensor_data);
                predictor.LoadInferenceData("mask", input_mask_data);

                predictor.infer();

                float[] outputData = predictor.GetInferenceResult("inpainted");
                Console.WriteLine("第" + index + "次,耗时:" + stopwatch.Elapsed.TotalMilliseconds);

                index++;
            }


        }
    }
}

下载

源码下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乱蜂朝王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值