Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit 4b8e41c

Browse files
committed
Animated GifToGif ExampleRunner
1 parent 2eef655 commit 4b8e41c

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using OnnxStack.StableDiffusion;
2+
using OnnxStack.StableDiffusion.Common;
3+
using OnnxStack.StableDiffusion.Config;
4+
using OnnxStack.StableDiffusion.Enums;
5+
using OnnxStack.StableDiffusion.Models;
6+
using SixLabors.ImageSharp;
7+
using SixLabors.ImageSharp.Formats.Gif;
8+
using SixLabors.ImageSharp.PixelFormats;
9+
using System.Diagnostics;
10+
11+
namespace OnnxStack.Console.Runner
12+
{
13+
public sealed class StableDiffusionGif : IExampleRunner
14+
{
15+
private readonly string _outputDirectory;
16+
private readonly IStableDiffusionService _stableDiffusionService;
17+
18+
public StableDiffusionGif(IStableDiffusionService stableDiffusionService)
19+
{
20+
_stableDiffusionService = stableDiffusionService;
21+
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(StableDiffusionGif));
22+
}
23+
24+
public string Name => "Stable Diffusion Gif";
25+
26+
public string Description => "Stable Diffusion Gif";
27+
28+
/// <summary>
29+
/// Stable Diffusion Demo
30+
/// </summary>
31+
public async Task RunAsync()
32+
{
33+
Directory.CreateDirectory(_outputDirectory);
34+
35+
var prompt = "elon musk wearing a red hat and sunglasses";
36+
var negativePrompt = "";
37+
38+
var promptOptions = new PromptOptions
39+
{
40+
Prompt = prompt,
41+
NegativePrompt = negativePrompt,
42+
DiffuserType = DiffuserType.ImageToImage
43+
};
44+
45+
var schedulerOptions = new SchedulerOptions
46+
{
47+
SchedulerType = SchedulerType.LCM,
48+
Seed = 624461087,
49+
GuidanceScale = 1f,
50+
InferenceSteps = 20,
51+
Strength = 0.3f,
52+
53+
};
54+
55+
// Choose Model
56+
var model = _stableDiffusionService.Models.FirstOrDefault(x => x.Name == "LCM-Dreamshaper-V7");
57+
OutputHelpers.WriteConsole($"Loading Model `{model.Name}`...", ConsoleColor.Green);
58+
await _stableDiffusionService.LoadModelAsync(model);
59+
60+
// Set Size
61+
schedulerOptions.Width = model.SampleSize;
62+
schedulerOptions.Height = model.SampleSize;
63+
64+
65+
using Image<Rgba32> gifDestination = new(schedulerOptions.Width, schedulerOptions.Height);
66+
{
67+
var gifMetaData = gifDestination.Metadata.GetGifMetadata();
68+
gifMetaData.RepeatCount = 0; // Loop
69+
70+
using (var gifSource = await Image.LoadAsync(Path.Combine(_outputDirectory, "source.gif")))
71+
{
72+
for (int i = 0; i < gifSource.Frames.Count; i++)
73+
{
74+
promptOptions.InputImage = new InputImage(gifSource.Frames.CloneFrame(i).CloneAs<Rgba32>());
75+
var result = await _stableDiffusionService.GenerateAsImageAsync(model, promptOptions, schedulerOptions);
76+
77+
78+
79+
gifDestination.Frames.AddFrame(result.Frames.RootFrame);
80+
81+
OutputHelpers.WriteConsole($"Frame: {i + 1}/{gifSource.Frames.Count}", ConsoleColor.Cyan);
82+
}
83+
84+
var outputFilename = Path.Combine(_outputDirectory, $"result.gif");
85+
await gifDestination.SaveAsGifAsync(outputFilename);
86+
}
87+
}
88+
}
89+
90+
}
91+
}

OnnxStack.StableDiffusion/Helpers/ImageHelpers.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static DenseTensor<float> ToDenseTensor(this InputImage imageData, ReadOn
106106
if (imageData.ImageTensor != null)
107107
return imageData.ImageTensor.ToDenseTensor(); // Note: Tensor Copy // TODO: Reshape to dimensions
108108

109-
return null;
109+
return TensorFromImage(imageData.Image, dimensions);
110110
}
111111

112112

@@ -181,6 +181,22 @@ public static void TensorToImageDebug2(DenseTensor<float> imageTensor, string fi
181181
}
182182

183183

184+
/// <summary>
185+
/// Tensors from image.
186+
/// </summary>
187+
/// <param name="image">The image.</param>
188+
/// <param name="dimensions">The dimensions.</param>
189+
/// <returns></returns>
190+
public static DenseTensor<float> TensorFromImage(Image<Rgba32> image, ReadOnlySpan<int> dimensions)
191+
{
192+
using (image)
193+
{
194+
Resize(image, dimensions);
195+
return ProcessPixels(image, dimensions);
196+
}
197+
}
198+
199+
184200
/// <summary>
185201
/// Loads and image from file and loads it iton a DenseTesor with the shape 1, 3, W, H.
186202
/// </summary>

0 commit comments

Comments
 (0)