22using OnnxStack . StableDiffusion . Common ;
33using OnnxStack . StableDiffusion . Config ;
44using OnnxStack . StableDiffusion . Enums ;
5+ using OnnxStack . StableDiffusion . Helpers ;
56using OnnxStack . StableDiffusion . Models ;
67using SixLabors . ImageSharp ;
78using 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