C# StableDiffusion StableDiffusionSharp 脱离python臃肿的环境

目录

说明

效果

项目

代码

下载


C# StableDiffusion StableDiffusionSharp 脱离python臃肿的环境

说明

Stable Diffusion in pure C/C++

github地址:https://github.com/leejet/stable-diffusion.cpp

C# Wrapper for StableDiffusion.cpp

github地址:https://github.com/DarthAffe/StableDiffusion.NET

效果

项目

电脑配置

AMD Ryzen 7 7735H with Radeon Graphics 3.19GHz

NVIDIA GeForce RTX 4060 Laptop GPU

cuda12.1+cudnn 8.8.1

代码

using NLog;

using OpenCvSharp;

using StableDiffusionSharp.Enums;

using System.ComponentModel;

using System.Drawing;

using System.Drawing.Imaging;

using System.IO;

using System.Windows.Forms;

namespace StableDiffusionSharp

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

NLog.Windows.Forms.RichTextBoxTarget.ReInitializeAllTextboxes(this);

}

StableDiffusionModel sd;

ModelParameter mp;

StableDiffusionParameter sdp;

string modelpath = "C:\\MyStudy\\v1-5-pruned-emaonly.safetensors";

string prompt = "";

int progress = 0;

private static Logger _log = NLog.LogManager.GetCurrentClassLogger();

private void button1_Click(object sender, System.EventArgs e)

{

prompt = txtPrompt.Text;

if (string.IsNullOrEmpty(prompt))

{

MessageBox.Show("请输入提示词!");

txtPrompt.Focus();

return;

}

if (pictureBox1.Image != null)

{

pictureBox1.Image.Dispose();

}

pictureBox1.Image = null;

button1.Enabled = false;

progress = 0;

//各种参数设置 TODO:从UI界面取值

sdp.SampleSteps = 25;

sdp.Width = 512;

sdp.Height = 512;

sdp.CfgScale = 7.5f;

sdp.SampleMethod = Sampler.Euler_A;

sdp.NegativePrompt = string.Empty;

sdp.Seed = -1;

sdp.Strength = 0.7f;

sdp.ClipSkip = -1;

sdp.ControlNet.Image = null;

sdp.ControlNet.Strength = 0.9f;

sdp.ControlNet.CannyPreprocess = false;

sdp.ControlNet.CannyHighThreshold = 0.08f;

sdp.ControlNet.CannyLowThreshold = 0.08f;

sdp.ControlNet.CannyWeak = 0.8f;

sdp.ControlNet.CannyStrong = 1.0f;

sdp.ControlNet.CannyInverse = false;

sdp.PhotoMaker.InputIdImageDirectory = string.Empty;

sdp.PhotoMaker.StyleRatio = 20f;

sdp.PhotoMaker.NormalizeInput = false;

backgroundWorker1.RunWorkerAsync();

}

private void Form1_Load(object sender, System.EventArgs e)

{

//检查文件是否存在

if (!File.Exists(modelpath))

{

MessageBox.Show("文件不存在,请检查!");

return;

}

timer1.Enabled = true;

timer1.Interval = 100;

StableDiffusionModel.Log += StableDiffusionModel_Log;

StableDiffusionModel.Progress += StableDiffusionModel_Progress;

}

private void timer1_Tick(object sender, System.EventArgs e)

{

mp = new ModelParameter();

sdp = new StableDiffusionParameter();

sd = new StableDiffusionModel(modelpath, mp);

button1.Enabled = true;

timer1.Enabled = false;

}

private void StableDiffusionModel_Progress(object sender, EventArgs.StableDiffusionProgressEventArgs e)

{

_log.Info($"{e.Step}|{e.Steps} taking {e.Time}s");

progress = (int)(e.Progress * 100);

backgroundWorker1.ReportProgress(progress);

}

private void StableDiffusionModel_Log(object sender, EventArgs.StableDiffusionLogEventArgs e)

{

_log.Info($"{e.Text.Replace("\n", "")}");

}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)

{

StableDiffusionImage sdimage = sd.TextToImage(prompt, sdp);

Mat image = new Mat(sdimage.Height, sdimage.Width, MatType.CV_8UC3, sdimage.Data);

Cv2.CvtColor(image, image, ColorConversionCodes.BGR2RGB);

//Cv2.ImShow("output", image);

pictureBox1.Image = new Bitmap(image.ToMemoryStream());

}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)

{

progressBar1.Value = e.ProgressPercentage;

}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

{

button1.Enabled = true;

//progressBar1.Value = 0;

}

private void button2_Click(object sender, System.EventArgs e)

{

if (pictureBox1.Image == null)

{

return;

}

Bitmap output = new Bitmap(pictureBox1.Image);

SaveFileDialog sdf = new SaveFileDialog();

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

}

}

}

}

using NLog;
using OpenCvSharp;
using StableDiffusionSharp.Enums;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

namespace StableDiffusionSharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            NLog.Windows.Forms.RichTextBoxTarget.ReInitializeAllTextboxes(this);
        }

        StableDiffusionModel sd;
        ModelParameter mp;
        StableDiffusionParameter sdp;
        string modelpath = "C:\\MyStudy\\v1-5-pruned-emaonly.safetensors";
        string prompt = "";
        int progress = 0;

        private static Logger _log = NLog.LogManager.GetCurrentClassLogger();

        private void button1_Click(object sender, System.EventArgs e)
        {
            prompt = txtPrompt.Text;
            if (string.IsNullOrEmpty(prompt))
            {
                MessageBox.Show("请输入提示词!");
                txtPrompt.Focus();
                return;
            }
            if (pictureBox1.Image != null)
            {
                pictureBox1.Image.Dispose();
            }
            pictureBox1.Image = null;
            button1.Enabled = false;
            progress = 0;

            //各种参数设置 TODO:从UI界面取值
            sdp.SampleSteps = 25;
            sdp.Width = 512;
            sdp.Height = 512;
            sdp.CfgScale = 7.5f;
            sdp.SampleMethod = Sampler.Euler_A;
            sdp.NegativePrompt = string.Empty;
            sdp.Seed = -1;
            sdp.Strength = 0.7f;
            sdp.ClipSkip = -1;

            sdp.ControlNet.Image = null;
            sdp.ControlNet.Strength = 0.9f;
            sdp.ControlNet.CannyPreprocess = false;
            sdp.ControlNet.CannyHighThreshold = 0.08f;
            sdp.ControlNet.CannyLowThreshold = 0.08f;
            sdp.ControlNet.CannyWeak = 0.8f;
            sdp.ControlNet.CannyStrong = 1.0f;
            sdp.ControlNet.CannyInverse = false;

            sdp.PhotoMaker.InputIdImageDirectory = string.Empty;
            sdp.PhotoMaker.StyleRatio = 20f;
            sdp.PhotoMaker.NormalizeInput = false;

            backgroundWorker1.RunWorkerAsync();
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            //检查文件是否存在
            if (!File.Exists(modelpath))
            {
                MessageBox.Show("文件不存在,请检查!");
                return;
            }

            timer1.Enabled = true;
            timer1.Interval = 100;

            StableDiffusionModel.Log += StableDiffusionModel_Log;
            StableDiffusionModel.Progress += StableDiffusionModel_Progress;
        }

        private void timer1_Tick(object sender, System.EventArgs e)
        {
            mp = new ModelParameter();
            sdp = new StableDiffusionParameter();
            sd = new StableDiffusionModel(modelpath, mp);
            button1.Enabled = true;
            timer1.Enabled = false;
        }

        private void StableDiffusionModel_Progress(object sender, EventArgs.StableDiffusionProgressEventArgs e)
        {
            _log.Info($"{e.Step}|{e.Steps} taking  {e.Time}s");
            progress = (int)(e.Progress * 100);
            backgroundWorker1.ReportProgress(progress);
        }

        private void StableDiffusionModel_Log(object sender, EventArgs.StableDiffusionLogEventArgs e)
        {
            _log.Info($"{e.Text.Replace("\n", "")}");
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            StableDiffusionImage sdimage = sd.TextToImage(prompt, sdp);
            Mat image = new Mat(sdimage.Height, sdimage.Width, MatType.CV_8UC3, sdimage.Data);
            Cv2.CvtColor(image, image, ColorConversionCodes.BGR2RGB);
            //Cv2.ImShow("output", image);
            pictureBox1.Image = new Bitmap(image.ToMemoryStream());
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            button1.Enabled = true;
            //progressBar1.Value = 0;
        }

        private void button2_Click(object sender, System.EventArgs e)
        {
            if (pictureBox1.Image == null)
            {
                return;
            }
            Bitmap output = new Bitmap(pictureBox1.Image);
            SaveFileDialog sdf = new SaveFileDialog();
            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);
            }

        }
    }
}

下载

源码下载(模型太大,需要单独下载)

模型下载

Stable Diffusion v1.4 https://huggingface.co/CompVis/stable-diffusion-v-1-4-original

Stable Diffusion v1.5 https://huggingface.co/runwayml/stable-diffusion-v1-5

Stable Diffuison v2.1 https://huggingface.co/stabilityai/stable-diffusion-2-1

相关推荐
桃花键神6 分钟前
AI可信论坛亮点:合合信息分享视觉内容安全技术前沿
人工智能
野蛮的大西瓜28 分钟前
开源呼叫中心中,如何将ASR与IVR菜单结合,实现动态的IVR交互
人工智能·机器人·自动化·音视频·信息与通信
CountingStars6191 小时前
目标检测常用评估指标(metrics)
人工智能·目标检测·目标跟踪
tangjunjun-owen1 小时前
第四节:GLM-4v-9b模型的tokenizer源码解读
人工智能·glm-4v-9b·多模态大模型教程
冰蓝蓝1 小时前
深度学习中的注意力机制:解锁智能模型的新视角
人工智能·深度学习
橙子小哥的代码世界1 小时前
【计算机视觉基础CV-图像分类】01- 从历史源头到深度时代:一文读懂计算机视觉的进化脉络、核心任务与产业蓝图
人工智能·计算机视觉
fkdw2 小时前
C# Newtonsoft.Json 反序列化派生类数据丢失问题
c#·json
新加坡内哥谈技术2 小时前
苏黎世联邦理工学院与加州大学伯克利分校推出MaxInfoRL:平衡内在与外在探索的全新强化学习框架
大数据·人工智能·语言模型
fanstuck2 小时前
Prompt提示工程上手指南(七)Prompt编写实战-基于智能客服问答系统下的Prompt编写
人工智能·数据挖掘·openai
lovelin+v175030409662 小时前
安全性升级:API接口在零信任架构下的安全防护策略
大数据·数据库·人工智能·爬虫·数据分析