|
7 | 7 | using SixLabors.ImageSharp;
|
8 | 8 | using SixLabors.ImageSharp.Formats.Gif;
|
9 | 9 | using SixLabors.ImageSharp.PixelFormats;
|
| 10 | +using SixLabors.ImageSharp.Processing; |
10 | 11 | using System.Diagnostics;
|
11 | 12 |
|
12 | 13 | namespace OnnxStack.Console.Runner
|
@@ -60,37 +61,45 @@ public async Task RunAsync()
|
60 | 61 | schedulerOptions.Width = model.SampleSize;
|
61 | 62 | schedulerOptions.Height = model.SampleSize;
|
62 | 63 |
|
| 64 | + var repeatCount = 0; |
| 65 | + var frameDelay = 0; |
63 | 66 |
|
64 | 67 | using Image<Rgba32> gifDestination = new(schedulerOptions.Width, schedulerOptions.Height);
|
65 | 68 | {
|
66 | 69 | var gifMetaData = gifDestination.Metadata.GetGifMetadata();
|
67 |
| - gifMetaData.RepeatCount = 0; // Loop |
| 70 | + gifMetaData.RepeatCount = (ushort)repeatCount; // Loop |
68 | 71 |
|
69 | 72 | using (var gifSource = await Image.LoadAsync(Path.Combine(_outputDirectory, "Source.gif")))
|
| 73 | + using (var frame = gifSource.Frames.CloneFrame(0)) |
70 | 74 | {
|
71 | 75 | for (int i = 0; i < gifSource.Frames.Count; i++)
|
72 | 76 | {
|
73 |
| - // Get Frame as Image |
74 |
| - var frame = gifSource.Frames.CloneFrame(i).CloneAs<Rgba32>(); |
| 77 | + // Draw each frame on top of the last to fix issues with compressed gifs |
| 78 | + frame.Mutate(x => x.DrawImage(gifSource.Frames.CloneFrame(i), 1f)); |
75 | 79 |
|
76 | 80 | // Save Debug Output
|
77 | 81 | await frame.SaveAsPngAsync(Path.Combine(_outputDirectory, $"Debug-Frame.png"));
|
78 | 82 |
|
79 | 83 | // Set prompt Image, Run Diffusion
|
80 |
| - promptOptions.InputImage = new InputImage(frame); |
| 84 | + promptOptions.InputImage = new InputImage(frame.CloneAs<Rgba32>()); |
81 | 85 | var result = await _stableDiffusionService.GenerateAsImageAsync(model, promptOptions, schedulerOptions);
|
82 | 86 |
|
83 | 87 | // Save Debug Output
|
84 | 88 | await result.SaveAsPngAsync(Path.Combine(_outputDirectory, $"Debug-Output.png"));
|
85 | 89 |
|
| 90 | + // Set Fraem metadata |
| 91 | + var metadata = result.Frames.RootFrame.Metadata.GetGifMetadata(); |
| 92 | + metadata.FrameDelay = frameDelay; |
| 93 | + |
86 | 94 | // Add Result to Gif
|
87 | 95 | gifDestination.Frames.InsertFrame(i, result.Frames.RootFrame);
|
88 | 96 | OutputHelpers.WriteConsole($"Frame: {i + 1}/{gifSource.Frames.Count}", ConsoleColor.Cyan);
|
89 | 97 | }
|
90 |
| - |
91 |
| - // Save Result |
92 |
| - await gifDestination.SaveAsGifAsync(Path.Combine(_outputDirectory, $"Result.gif")); |
93 | 98 | }
|
| 99 | + |
| 100 | + // Save Result |
| 101 | + await gifDestination.SaveAsGifAsync(Path.Combine(_outputDirectory, $"Result.gif")); |
| 102 | + |
94 | 103 | }
|
95 | 104 | }
|
96 | 105 |
|
|
0 commit comments