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

Commit d5ef111

Browse files
committed
Add some debugging to GifToGif
1 parent 4b8e41c commit d5ef111

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

OnnxStack.Console/Examples/StableDiffusionGif.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using OnnxStack.StableDiffusion.Common;
33
using OnnxStack.StableDiffusion.Config;
44
using OnnxStack.StableDiffusion.Enums;
5+
using OnnxStack.StableDiffusion.Helpers;
56
using OnnxStack.StableDiffusion.Models;
67
using SixLabors.ImageSharp;
78
using SixLabors.ImageSharp.Formats.Gif;
@@ -19,6 +20,7 @@ public StableDiffusionGif(IStableDiffusionService stableDiffusionService)
1920
{
2021
_stableDiffusionService = stableDiffusionService;
2122
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(StableDiffusionGif));
23+
Directory.CreateDirectory(_outputDirectory);
2224
}
2325

2426
public string Name => "Stable Diffusion Gif";
@@ -30,9 +32,7 @@ public StableDiffusionGif(IStableDiffusionService stableDiffusionService)
3032
/// </summary>
3133
public async Task RunAsync()
3234
{
33-
Directory.CreateDirectory(_outputDirectory);
34-
35-
var prompt = "elon musk wearing a red hat and sunglasses";
35+
var prompt = "Elon Musk";
3636
var negativePrompt = "";
3737

3838
var promptOptions = new PromptOptions
@@ -47,9 +47,8 @@ public async Task RunAsync()
4747
SchedulerType = SchedulerType.LCM,
4848
Seed = 624461087,
4949
GuidanceScale = 1f,
50-
InferenceSteps = 20,
51-
Strength = 0.3f,
52-
50+
InferenceSteps = 12,
51+
Strength = 0.5f,
5352
};
5453

5554
// Choose Model
@@ -67,22 +66,30 @@ public async Task RunAsync()
6766
var gifMetaData = gifDestination.Metadata.GetGifMetadata();
6867
gifMetaData.RepeatCount = 0; // Loop
6968

70-
using (var gifSource = await Image.LoadAsync(Path.Combine(_outputDirectory, "source.gif")))
69+
using (var gifSource = await Image.LoadAsync(Path.Combine(_outputDirectory, "Source.gif")))
7170
{
7271
for (int i = 0; i < gifSource.Frames.Count; i++)
7372
{
74-
promptOptions.InputImage = new InputImage(gifSource.Frames.CloneFrame(i).CloneAs<Rgba32>());
75-
var result = await _stableDiffusionService.GenerateAsImageAsync(model, promptOptions, schedulerOptions);
73+
// Get Frame as Image
74+
var frame = gifSource.Frames.CloneFrame(i).CloneAs<Rgba32>();
7675

76+
// Save Debug Output
77+
await frame.SaveAsPngAsync(Path.Combine(_outputDirectory, $"Debug-Frame.png"));
7778

79+
// Set prompt Image, Run Diffusion
80+
promptOptions.InputImage = new InputImage(frame);
81+
var result = await _stableDiffusionService.GenerateAsImageAsync(model, promptOptions, schedulerOptions);
7882

79-
gifDestination.Frames.AddFrame(result.Frames.RootFrame);
83+
// Save Debug Output
84+
await result.SaveAsPngAsync(Path.Combine(_outputDirectory, $"Debug-Output.png"));
8085

86+
// Add Result to Gif
87+
gifDestination.Frames.InsertFrame(i, result.Frames.RootFrame);
8188
OutputHelpers.WriteConsole($"Frame: {i + 1}/{gifSource.Frames.Count}", ConsoleColor.Cyan);
8289
}
8390

84-
var outputFilename = Path.Combine(_outputDirectory, $"result.gif");
85-
await gifDestination.SaveAsGifAsync(outputFilename);
91+
// Save Result
92+
await gifDestination.SaveAsGifAsync(Path.Combine(_outputDirectory, $"Result.gif"));
8693
}
8794
}
8895
}

0 commit comments

Comments
 (0)