@@ -26,6 +26,7 @@ public partial class ImageToImageView : UserControl, INavigatable, INotifyProper
2626 {
2727 private readonly ILogger < ImageToImageView > _logger ;
2828 private readonly IStableDiffusionService _stableDiffusionService ;
29+ private readonly IImageService _imageService ;
2930
3031 private bool _hasResult ;
3132 private int _progressMax ;
@@ -37,6 +38,7 @@ public partial class ImageToImageView : UserControl, INavigatable, INotifyProper
3738 private ImageInput _inputImage ;
3839 private ImageResult _resultImage ;
3940 private StableDiffusionModelSetViewModel _selectedModel ;
41+ private ControlNetModelSetViewModel _selectedControlNetModel ;
4042 private PromptOptionsModel _promptOptionsModel ;
4143 private SchedulerOptionsModel _schedulerOptions ;
4244 private CancellationTokenSource _cancelationTokenSource ;
@@ -51,9 +53,10 @@ public ImageToImageView()
5153 {
5254 _logger = App . GetService < ILogger < ImageToImageView > > ( ) ;
5355 _stableDiffusionService = App . GetService < IStableDiffusionService > ( ) ;
56+ _imageService = App . GetService < IImageService > ( ) ;
5457 }
5558
56- SupportedDiffusers = new ( ) { DiffuserType . ImageToImage } ;
59+ SupportedDiffusers = new ( ) { DiffuserType . ImageToImage , DiffuserType . ControlNet } ;
5760 CancelCommand = new AsyncRelayCommand ( Cancel , CanExecuteCancel ) ;
5861 GenerateCommand = new AsyncRelayCommand ( Generate , CanExecuteGenerate ) ;
5962 ClearHistoryCommand = new AsyncRelayCommand ( ClearHistory , CanExecuteClearHistory ) ;
@@ -84,6 +87,12 @@ public StableDiffusionModelSetViewModel SelectedModel
8487 get { return _selectedModel ; }
8588 set { _selectedModel = value ; NotifyPropertyChanged ( ) ; }
8689 }
90+
91+ public ControlNetModelSetViewModel SelectedControlNetModel
92+ {
93+ get { return _selectedControlNetModel ; }
94+ set { _selectedControlNetModel = value ; NotifyPropertyChanged ( ) ; }
95+ }
8796
8897 public PromptOptionsModel PromptOptions
8998 {
@@ -195,13 +204,13 @@ private async Task Generate()
195204 IsControlsEnabled = false ;
196205 ResultImage = null ;
197206 _cancelationTokenSource = new CancellationTokenSource ( ) ;
198- var promptOptions = GetPromptOptions ( PromptOptions , InputImage ) ;
207+ var promptOptions = await GetPromptOptions ( PromptOptions , InputImage ) ;
199208 var schedulerOptions = SchedulerOptions . ToSchedulerOptions ( ) ;
200209
201210 try
202211 {
203212 var timestamp = Stopwatch . GetTimestamp ( ) ;
204- var result = await _stableDiffusionService . GenerateAsBytesAsync ( new ModelOptions ( _selectedModel . ModelSet ) , promptOptions , schedulerOptions , ProgressCallback ( ) , _cancelationTokenSource . Token ) ;
213+ var result = await _stableDiffusionService . GenerateAsBytesAsync ( new ModelOptions ( _selectedModel . ModelSet , _selectedControlNetModel ? . ModelSet ) , promptOptions , schedulerOptions , ProgressCallback ( ) , _cancelationTokenSource . Token ) ;
205214 var resultImage = await GenerateResultAsync ( result , promptOptions , schedulerOptions , timestamp ) ;
206215 if ( resultImage != null )
207216 {
@@ -232,8 +241,8 @@ private async Task Generate()
232241 private bool CanExecuteGenerate ( )
233242 {
234243 return ! IsGenerating
235- // && !string.IsNullOrEmpty(PromptOptions.Prompt)
236- && HasInputResult ;
244+ && HasInputResult
245+ && ( ! SelectedModel . IsControlNet || ( SelectedModel . IsControlNet && SelectedControlNetModel ? . IsLoaded == true ) ) ;
237246 }
238247
239248
@@ -294,17 +303,38 @@ private void Reset()
294303 }
295304
296305
297- private PromptOptions GetPromptOptions ( PromptOptionsModel promptOptionsModel , ImageInput imageInput )
306+ private async Task < PromptOptions > GetPromptOptions ( PromptOptionsModel promptOptionsModel , ImageInput imageInput )
298307 {
308+ if ( _selectedModel . IsControlNet )
309+ {
310+ var controlNetDiffuserType = _schedulerOptions . Strength >= 1
311+ ? DiffuserType . ControlNet
312+ : DiffuserType . ControlNetImage ;
313+
314+ var inputImage = default ( InputImage ) ;
315+ if ( controlNetDiffuserType == DiffuserType . ControlNetImage )
316+ inputImage = new InputImage ( imageInput . Image . GetImageBytes ( ) ) ;
317+
318+ var controlImage = new InputImage ( imageInput . Image . GetImageBytes ( ) ) ;
319+ if ( _schedulerOptions . ProcessInputImage )
320+ controlImage = await _imageService . PrepareInputImage ( _selectedControlNetModel . ModelSet , controlImage , _schedulerOptions . Height , _schedulerOptions . Width ) ;
321+
322+ return new PromptOptions
323+ {
324+ Prompt = promptOptionsModel . Prompt ,
325+ NegativePrompt = promptOptionsModel . NegativePrompt ,
326+ DiffuserType = controlNetDiffuserType ,
327+ InputImage = inputImage ,
328+ InputContolImage = controlImage
329+ } ;
330+ }
331+
299332 return new PromptOptions
300333 {
301334 Prompt = promptOptionsModel . Prompt ,
302335 NegativePrompt = promptOptionsModel . NegativePrompt ,
303336 DiffuserType = DiffuserType . ImageToImage ,
304- InputImage = new InputImage
305- {
306- ImageBytes = imageInput . Image . GetImageBytes ( )
307- }
337+ InputImage = new InputImage ( imageInput . Image . GetImageBytes ( ) )
308338 } ;
309339 }
310340
0 commit comments