Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit cd8a245

Browse files
committed
Basic Upscale UI
1 parent a68d7a9 commit cd8a245

17 files changed

+1139
-37
lines changed

OnnxStack.ImageUpscaler/Services/IUpscaleService.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,21 @@ public interface IUpscaleService
4646
/// <returns></returns>
4747
Task<bool> LoadModelAsync(UpscaleModelSet model);
4848

49-
5049
/// <summary>
5150
/// Unloads the model.
5251
/// </summary>
5352
/// <param name="model">The model.</param>
5453
/// <returns></returns>
5554
Task<bool> UnloadModelAsync(UpscaleModelSet model);
5655

56+
/// <summary>
57+
/// Determines whether [is model loaded] [the specified model options].
58+
/// </summary>
59+
/// <param name="modelOptions">The model options.</param>
60+
/// <returns>
61+
/// <c>true</c> if [is model loaded] [the specified model options]; otherwise, <c>false</c>.
62+
/// </returns>
63+
bool IsModelLoaded(UpscaleModelSet modelOptions);
5764

5865
/// <summary>
5966
/// Generates the upscaled image.

OnnxStack.ImageUpscaler/Services/UpscaleService.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,20 @@ public async Task<bool> UnloadModelAsync(UpscaleModelSet model)
124124
}
125125

126126

127+
/// <summary>
128+
/// Determines whether [is model loaded] [the specified model options].
129+
/// </summary>
130+
/// <param name="modelOptions">The model options.</param>
131+
/// <returns>
132+
/// <c>true</c> if [is model loaded] [the specified model options]; otherwise, <c>false</c>.
133+
/// </returns>
134+
/// <exception cref="System.NotImplementedException"></exception>
135+
public bool IsModelLoaded(UpscaleModelSet modelOptions)
136+
{
137+
return _modelService.IsModelLoaded(modelOptions);
138+
}
139+
140+
127141
/// <summary>
128142
/// Generates the upscaled image.
129143
/// </summary>
@@ -231,5 +245,6 @@ private UpscaleInput CreateInputParams(Image<Rgba32> imageSource, int maxTileSiz
231245
var height = imageSource.Height * scaleFactor;
232246
return new UpscaleInput(tiles, width, height);
233247
}
248+
234249
}
235250
}

OnnxStack.UI/App.xaml

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106

107107

108108

109-
109+
110110

111111

112112

@@ -233,8 +233,8 @@
233233
<TextBlock Text="{Binding SchedulerOptions.Seed}" Margin="2,0,0,0" />
234234
</StackPanel>
235235
</UniformGrid>
236-
<UniformGrid Columns="6" Margin="0,2,0,2">
237-
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=SaveImageCommand}" CommandParameter="{Binding}">
236+
<UniformGrid Columns="7" Margin="0,2,0,2">
237+
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=SaveImageResultCommand}" CommandParameter="{Binding}">
238238
<userControls:FontAwesome Icon="&#xf0c7;" IconStyle="Light" Size="13" ToolTip="Save Image File"/>
239239
</Button>
240240
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=SaveBlueprintCommand}" CommandParameter="{Binding}" BorderThickness="0, 1">
@@ -252,6 +252,9 @@
252252
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=NavigateImagePaintToImageCommand}" CommandParameter="{Binding}" Padding="2,2" BorderThickness="0,1,1,1">
253253
<userControls:FontAwesome Icon="&#xf1fc;" IconStyle="Light" Size="13" ToolTip="Send To Upscaler"/>
254254
</Button>
255+
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=NavigateUpscalerCommand}" CommandParameter="{Binding}" Padding="2,2" BorderThickness="0,1,1,1">
256+
<userControls:FontAwesome Icon="&#xf424;" IconStyle="Light" Size="13" ToolTip="Send To Upscaler"/>
257+
</Button>
255258
</UniformGrid>
256259
</StackPanel>
257260
<Border Margin="0,2" BorderBrush="{StaticResource ContainerBorder}" BorderThickness="1" Background="{StaticResource ContainerBackground}" >
@@ -260,7 +263,55 @@
260263
</DockPanel>
261264
</DataTemplate>
262265

266+
<DataTemplate DataType="{x:Type models:UpscaleResult}">
267+
<DockPanel Width="220" Height="285" HorizontalAlignment="Center">
268+
<StackPanel DockPanel.Dock="Bottom" >
269+
270+
<UniformGrid Columns="2" Margin="0,3">
271+
272+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
273+
<TextBlock Text="Scale Factor:" Margin="0,0,5,0" FontStyle="Italic" FontSize="10" Opacity=".6" VerticalAlignment="Center"/>
274+
<TextBlock Text="{Binding Info.ScaleFactor}" />
275+
</StackPanel>
276+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
277+
<TextBlock Text="Elapsed:" Margin="5,0,0,0" FontSize="10" Opacity=".6" VerticalAlignment="Center" FontStyle="Italic"/>
278+
<TextBlock Text="{Binding Elapsed, StringFormat={}{0:F1}s}" Margin="5,0,0,0"/>
279+
</StackPanel>
280+
281+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
282+
<TextBlock Text="Input:" Margin="0,0,5,0" FontStyle="Italic" FontSize="10" Opacity=".6" VerticalAlignment="Center"/>
283+
<TextBlock Text="{Binding Info.InputWidth}" />
284+
<TextBlock Text="x" Margin="2,0,2,0"/>
285+
<TextBlock Text="{Binding Info.InputHeight}" />
286+
</StackPanel>
287+
288+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
289+
<TextBlock Text="Output:" Margin="0,0,5,0" FontStyle="Italic" FontSize="10" Opacity=".6" VerticalAlignment="Center"/>
290+
<TextBlock Text="{Binding Info.OutputWidth}" />
291+
<TextBlock Text="x" Margin="2,0,2,0"/>
292+
<TextBlock Text="{Binding Info.OutputHeight}" />
293+
</StackPanel>
294+
295+
</UniformGrid>
296+
297+
298+
299+
<UniformGrid Columns="1" Height="30" IsEnabled="{Binding HasResult}">
300+
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}, Path=SaveImageCommand}" CommandParameter="{Binding}" ToolTip="Save Image File">
301+
<StackPanel Orientation="Horizontal">
302+
<userControls:FontAwesome Icon="&#xf0c7;" IconStyle="Light" Size="13" />
303+
<TextBlock Text="Save Image" Margin="5,0,0,0"/>
304+
</StackPanel>
305+
</Button>
306+
</UniformGrid>
307+
263308

309+
</StackPanel>
310+
<Border Margin="0,2" BorderBrush="{StaticResource ContainerBorder}" BorderThickness="1" Background="{StaticResource ContainerBackground}" >
311+
<Image Source="{Binding Image}" DockPanel.Dock="Left" Stretch="Uniform" VerticalAlignment="Center" />
312+
</Border>
313+
</DockPanel>
314+
</DataTemplate>
264315

265316
<!--RadioButton-->
266317
<SolidColorBrush x:Key="RadioButtonBackground" Color="#FF2D2D2D"/>

OnnxStack.UI/App.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.Extensions.Hosting;
33
using Microsoft.Extensions.Logging;
44
using OnnxStack.Core;
5+
using OnnxStack.ImageUpscaler;
56
using OnnxStack.UI.Dialogs;
67
using OnnxStack.UI.Models;
78
using OnnxStack.UI.Services;
@@ -26,6 +27,7 @@ public App()
2627

2728
// Add OnnxStackStableDiffusion
2829
builder.Services.AddOnnxStackStableDiffusion();
30+
builder.Services.AddOnnxStackImageUpscaler();
2931
builder.Services.AddOnnxStackConfig<OnnxStackUIConfig>();
3032

3133
// Add Windows

OnnxStack.UI/MainWindow.xaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,19 @@
8383
<views:PaintToImageView UISettings="{Binding UISettings}" Margin="0,6,0,0"/>
8484
</TabItem>
8585

86+
<!--Upscale-->
87+
<TabItem>
88+
<TabItem.Header>
89+
<StackPanel Orientation="Horizontal" Margin="5">
90+
<StackPanel Orientation="Horizontal">
91+
<userControls:FontAwesome Icon="&#xf424;" IconStyle="Light"/>
92+
</StackPanel>
93+
<TextBlock Text="Upscale Image" Margin="5,0,0,0"/>
94+
</StackPanel>
95+
</TabItem.Header>
96+
<views:UpscaleView UISettings="{Binding UISettings}" Margin="0,6,0,0"/>
97+
</TabItem>
98+
8699
<!--Log Window-->
87100
<TabItem DockPanel.Dock="Right" HorizontalAlignment="Right">
88101
<TabItem.Header>

OnnxStack.UI/MainWindow.xaml.cs

Lines changed: 86 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using Microsoft.Extensions.Logging;
22
using Microsoft.Win32;
33
using Models;
4+
using OnnxStack.ImageUpscaler.Config;
45
using OnnxStack.StableDiffusion.Config;
5-
using OnnxStack.StableDiffusion.Enums;
66
using OnnxStack.UI.Commands;
77
using OnnxStack.UI.Models;
88
using OnnxStack.UI.Views;
@@ -27,36 +27,43 @@ public partial class MainWindow : Window, INotifyPropertyChanged
2727
private INavigatable _selectedTabItem;
2828
private readonly ILogger<MainWindow> _logger;
2929
private ObservableCollection<ModelOptionsModel> _models;
30+
private ObservableCollection<UpscaleModelSetModel> _upscaleModels;
3031

31-
public MainWindow(StableDiffusionConfig configuration, OnnxStackUIConfig uiSettings, ILogger<MainWindow> logger)
32+
public MainWindow(OnnxStackUIConfig uiSettings, StableDiffusionConfig configuration, ImageUpscalerConfig upscaleConfiguration, ILogger<MainWindow> logger)
3233
{
3334
_logger = logger;
3435
UISettings = uiSettings;
35-
SaveImageCommand = new AsyncRelayCommand<ImageResult>(SaveImageFile);
36+
SaveImageCommand = new AsyncRelayCommand<UpscaleResult>(SaveUpscaleImageFile);
37+
SaveImageResultCommand = new AsyncRelayCommand<ImageResult>(SaveImageResultFile);
3638
SaveBlueprintCommand = new AsyncRelayCommand<ImageResult>(SaveBlueprintFile);
3739
NavigateTextToImageCommand = new AsyncRelayCommand<ImageResult>(NavigateTextToImage);
3840
NavigateImageToImageCommand = new AsyncRelayCommand<ImageResult>(NavigateImageToImage);
3941
NavigateImageInpaintCommand = new AsyncRelayCommand<ImageResult>(NavigateImageInpaint);
4042
NavigateImagePaintToImageCommand = new AsyncRelayCommand<ImageResult>(NavigateImagePaintToImage);
41-
43+
NavigateUpscalerCommand = new AsyncRelayCommand<ImageResult>(NavigateUpscaler);
4244
WindowCloseCommand = new AsyncRelayCommand(WindowClose);
4345
WindowRestoreCommand = new AsyncRelayCommand(WindowRestore);
4446
WindowMinimizeCommand = new AsyncRelayCommand(WindowMinimize);
4547
WindowMaximizeCommand = new AsyncRelayCommand(WindowMaximize);
4648
Models = CreateModelOptions(configuration.ModelSets);
49+
UpscaleModels = CreateUpscaleModelOptions(upscaleConfiguration.ModelSets);
4750
InitializeComponent();
4851
}
4952

53+
54+
5055
public AsyncRelayCommand WindowMinimizeCommand { get; }
5156
public AsyncRelayCommand WindowRestoreCommand { get; }
5257
public AsyncRelayCommand WindowMaximizeCommand { get; }
5358
public AsyncRelayCommand WindowCloseCommand { get; }
54-
public AsyncRelayCommand<ImageResult> SaveImageCommand { get; }
59+
public AsyncRelayCommand<UpscaleResult> SaveImageCommand { get; }
60+
public AsyncRelayCommand<ImageResult> SaveImageResultCommand { get; }
5561
public AsyncRelayCommand<ImageResult> SaveBlueprintCommand { get; }
5662
public AsyncRelayCommand<ImageResult> NavigateTextToImageCommand { get; }
5763
public AsyncRelayCommand<ImageResult> NavigateImageToImageCommand { get; }
5864
public AsyncRelayCommand<ImageResult> NavigateImageInpaintCommand { get; }
5965
public AsyncRelayCommand<ImageResult> NavigateImagePaintToImageCommand { get; }
66+
public AsyncRelayCommand<ImageResult> NavigateUpscalerCommand { get; }
6067

6168
public OnnxStackUIConfig UISettings
6269
{
@@ -72,6 +79,13 @@ public ObservableCollection<ModelOptionsModel> Models
7279
set { _models = value; NotifyPropertyChanged(); }
7380
}
7481

82+
public ObservableCollection<UpscaleModelSetModel> UpscaleModels
83+
{
84+
get { return _upscaleModels; }
85+
set { _upscaleModels = value; NotifyPropertyChanged(); }
86+
}
87+
88+
7589
public int SelectedTabIndex
7690
{
7791
get { return _selectedTabIndex; }
@@ -104,6 +118,10 @@ private async Task NavigateImagePaintToImage(ImageResult result)
104118
await NavigateToTab(TabId.PaintToImage, result);
105119
}
106120

121+
private async Task NavigateUpscaler(ImageResult result)
122+
{
123+
await NavigateToTab(TabId.Upscaler, result);
124+
}
107125

108126
private async Task NavigateToTab(TabId tab, ImageResult imageResult)
109127
{
@@ -116,7 +134,8 @@ private enum TabId
116134
TextToImage = 0,
117135
ImageToImage = 1,
118136
ImageInpaint = 2,
119-
PaintToImage = 3
137+
PaintToImage = 3,
138+
Upscaler = 4
120139
}
121140

122141
private ObservableCollection<ModelOptionsModel> CreateModelOptions(List<StableDiffusionModelSet> onnxModelSets)
@@ -131,29 +150,47 @@ private ObservableCollection<ModelOptionsModel> CreateModelOptions(List<StableDi
131150
return new ObservableCollection<ModelOptionsModel>(models);
132151
}
133152

134-
private async Task SaveImageFile(ImageResult imageResult)
153+
154+
private ObservableCollection<UpscaleModelSetModel> CreateUpscaleModelOptions(List<UpscaleModelSet> modelSets)
155+
{
156+
var models = modelSets.Select(model => new UpscaleModelSetModel
157+
{
158+
Name = model.Name,
159+
ModelOptions = model,
160+
IsEnabled = model.IsEnabled
161+
});
162+
return new ObservableCollection<UpscaleModelSetModel>(models);
163+
}
164+
165+
private async Task SaveImageResultFile(ImageResult imageResult)
135166
{
136167
try
137168
{
138-
var saveFileDialog = new SaveFileDialog
139-
{
140-
Title = Title,
141-
Filter = "png files (*.png)|*.png",
142-
DefaultExt = "png",
143-
AddExtension = true,
144-
RestoreDirectory = true,
145-
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),
146-
FileName = $"image-{imageResult.SchedulerOptions.Seed}.png"
147-
};
169+
var filename = GetSaveFilename($"image-{imageResult.SchedulerOptions.Seed}");
170+
if (string.IsNullOrEmpty(filename))
171+
return;
148172

149-
var dialogResult = saveFileDialog.ShowDialog();
150-
if (dialogResult == false)
151-
{
152-
_logger.LogInformation("Saving image canceled");
173+
var result = await imageResult.Image.SaveImageFileAsync(filename);
174+
if (!result)
175+
_logger.LogError("Error saving image");
176+
177+
}
178+
catch (Exception ex)
179+
{
180+
_logger.LogError(ex, "Error saving image");
181+
}
182+
}
183+
184+
185+
private async Task SaveUpscaleImageFile(UpscaleResult imageResult)
186+
{
187+
try
188+
{
189+
var filename = GetSaveFilename($"image-{imageResult.Info.OutputWidth}x{imageResult.Info.OutputHeight}");
190+
if (string.IsNullOrEmpty(filename))
153191
return;
154-
}
155192

156-
var result = await imageResult.SaveImageFileAsync(saveFileDialog.FileName);
193+
var result = await imageResult.Image.SaveImageFileAsync(filename);
157194
if (!result)
158195
_logger.LogError("Error saving image");
159196

@@ -187,6 +224,8 @@ private async Task SaveBlueprintFile(ImageResult imageResult)
187224
return;
188225
}
189226

227+
228+
190229
var result = await imageResult.SaveBlueprintFileAsync(saveFileDialog.FileName);
191230
if (!result)
192231
_logger.LogError("Error saving image blueprint");
@@ -199,6 +238,30 @@ private async Task SaveBlueprintFile(ImageResult imageResult)
199238
}
200239

201240

241+
private string GetSaveFilename(string initialFilename)
242+
{
243+
var saveFileDialog = new SaveFileDialog
244+
{
245+
Title = Title,
246+
Filter = "png files (*.png)|*.png",
247+
DefaultExt = "png",
248+
AddExtension = true,
249+
RestoreDirectory = true,
250+
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),
251+
FileName = $"{initialFilename}.png"
252+
};
253+
254+
var dialogResult = saveFileDialog.ShowDialog();
255+
if (dialogResult == false)
256+
{
257+
_logger.LogInformation("Saving image canceled");
258+
return null;
259+
}
260+
261+
return saveFileDialog.FileName;
262+
}
263+
264+
202265
/// <summary>
203266
/// Gets or sets the output log.
204267
/// </summary>

0 commit comments

Comments
 (0)