2
2
using OnnxStack . StableDiffusion . Common ;
3
3
using OnnxStack . StableDiffusion . Config ;
4
4
using OnnxStack . StableDiffusion . Enums ;
5
+ using OnnxStack . StableDiffusion . Helpers ;
5
6
using OnnxStack . StableDiffusion . Models ;
6
7
using SixLabors . ImageSharp ;
7
8
using SixLabors . ImageSharp . Formats . Gif ;
@@ -19,6 +20,7 @@ public StableDiffusionGif(IStableDiffusionService stableDiffusionService)
19
20
{
20
21
_stableDiffusionService = stableDiffusionService ;
21
22
_outputDirectory = Path . Combine ( Directory . GetCurrentDirectory ( ) , "Examples" , nameof ( StableDiffusionGif ) ) ;
23
+ Directory . CreateDirectory ( _outputDirectory ) ;
22
24
}
23
25
24
26
public string Name => "Stable Diffusion Gif" ;
@@ -30,9 +32,7 @@ public StableDiffusionGif(IStableDiffusionService stableDiffusionService)
30
32
/// </summary>
31
33
public async Task RunAsync ( )
32
34
{
33
- Directory . CreateDirectory ( _outputDirectory ) ;
34
-
35
- var prompt = "elon musk wearing a red hat and sunglasses" ;
35
+ var prompt = "Elon Musk" ;
36
36
var negativePrompt = "" ;
37
37
38
38
var promptOptions = new PromptOptions
@@ -47,9 +47,8 @@ public async Task RunAsync()
47
47
SchedulerType = SchedulerType . LCM ,
48
48
Seed = 624461087 ,
49
49
GuidanceScale = 1f ,
50
- InferenceSteps = 20 ,
51
- Strength = 0.3f ,
52
-
50
+ InferenceSteps = 12 ,
51
+ Strength = 0.5f ,
53
52
} ;
54
53
55
54
// Choose Model
@@ -67,22 +66,30 @@ public async Task RunAsync()
67
66
var gifMetaData = gifDestination . Metadata . GetGifMetadata ( ) ;
68
67
gifMetaData . RepeatCount = 0 ; // Loop
69
68
70
- using ( var gifSource = await Image . LoadAsync ( Path . Combine ( _outputDirectory , "source .gif" ) ) )
69
+ using ( var gifSource = await Image . LoadAsync ( Path . Combine ( _outputDirectory , "Source .gif" ) ) )
71
70
{
72
71
for ( int i = 0 ; i < gifSource . Frames . Count ; i ++ )
73
72
{
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 > ( ) ;
76
75
76
+ // Save Debug Output
77
+ await frame . SaveAsPngAsync ( Path . Combine ( _outputDirectory , $ "Debug-Frame.png") ) ;
77
78
79
+ // Set prompt Image, Run Diffusion
80
+ promptOptions . InputImage = new InputImage ( frame ) ;
81
+ var result = await _stableDiffusionService . GenerateAsImageAsync ( model , promptOptions , schedulerOptions ) ;
78
82
79
- gifDestination . Frames . AddFrame ( result . Frames . RootFrame ) ;
83
+ // Save Debug Output
84
+ await result . SaveAsPngAsync ( Path . Combine ( _outputDirectory , $ "Debug-Output.png") ) ;
80
85
86
+ // Add Result to Gif
87
+ gifDestination . Frames . InsertFrame ( i , result . Frames . RootFrame ) ;
81
88
OutputHelpers . WriteConsole ( $ "Frame: { i + 1 } /{ gifSource . Frames . Count } ", ConsoleColor . Cyan ) ;
82
89
}
83
90
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" ) ) ;
86
93
}
87
94
}
88
95
}
0 commit comments