|
| 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 | +} |
0 commit comments