Skip to content

Commit 0735887

Browse files
committed
Demo model download
1 parent 3be1c90 commit 0735887

20 files changed

+535
-46
lines changed

Examples/TensorStack.Example.Extractors/Common/BackgroundModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ public class BackgroundModel : BaseModel
1717
public Normalization OutputNormalization { get; init; }
1818
public int OutputChannels { get; init; } = 1;
1919
public string Path { get; set; }
20+
public string UrlPath { get; set; }
2021
}
2122
}

Examples/TensorStack.Example.Extractors/Common/ExtractorModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ public record ExtractorModel
1515
public int OutputChannels { get; set; }
1616
public bool IsDynamicOutput { get; set; }
1717
public string Path { get; set; }
18+
public string UrlPath { get; set; }
1819
}
1920
}

Examples/TensorStack.Example.Extractors/Models/Download.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.

Examples/TensorStack.Example.Extractors/Settings.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"OutputChannels": 1,
1010
"Normalization": "OneToOne",
1111
"OutputNormalization": "OneToOne",
12-
"Path": "Models\\Canny.onnx"
12+
"Path": "Models\\Canny.onnx",
13+
"UrlPath": "https://huggingface.co/TensorStack/TensorStack/resolve/main/Extractor/Canny.onnx?download=true"
1314
},
1415
{
1516
"Id": 2,
@@ -20,7 +21,8 @@
2021
"OutputChannels": 1,
2122
"Normalization": "OneToOne",
2223
"OutputNormalization": "ZeroToOne",
23-
"Path": "Models\\SoftEdge.onnx"
24+
"Path": "Models\\SoftEdge.onnx",
25+
"UrlPath": "https://huggingface.co/TensorStack/TensorStack/resolve/main/Extractor/SoftEdge.onnx?download=true"
2426
},
2527
{
2628
"Id": 3,
@@ -32,7 +34,8 @@
3234
"Normalization": "ZeroToOne",
3335
"OutputNormalization": "MinMaxOneToOne",
3436
"IsDynamicOutput": true,
35-
"Path": "Models\\Depth.onnx"
37+
"Path": "Models\\Depth.onnx",
38+
"UrlPath": "https://huggingface.co/TensorStack/TensorStack/resolve/main/Extractor/Depth.onnx?download=true"
3639
}
3740
],
3841
"BackgroundModels": [
@@ -44,7 +47,8 @@
4447
"OutputChannels": 1,
4548
"Normalization": "ZeroToOne",
4649
"OutputNormalization": "OneToOne",
47-
"Path": "Models\\BiRefNet.onnx"
50+
"Path": "Models\\BiRefNet.onnx",
51+
"UrlPath": "https://huggingface.co/TensorStack/TensorStack/resolve/main/Extractor/RMBGv1.4.onnx?download=true"
4852
},
4953
{
5054
"Id": 2,
@@ -55,7 +59,8 @@
5559
"OutputChannels": 1,
5660
"Normalization": "ZeroToOne",
5761
"OutputNormalization": "OneToOne",
58-
"Path": "Models\\RMBGv1.4.onnx"
62+
"Path": "Models\\RMBGv1.4.onnx",
63+
"UrlPath": "https://huggingface.co/TensorStack/TensorStack/resolve/main/Extractor/BiRefNet.onnx?download=true"
5964
}
6065
]
6166
}

Examples/TensorStack.Example.Extractors/Views/ImageBackgroundView.xaml.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Diagnostics;
2+
using System.IO;
23
using System.Linq;
34
using System.Threading.Tasks;
45
using TensorStack.Common;
@@ -7,7 +8,6 @@
78
using TensorStack.Extractors.Common;
89
using TensorStack.Image;
910
using TensorStack.WPF;
10-
using TensorStack.WPF.Controls;
1111
using TensorStack.WPF.Services;
1212

1313
namespace TensorStack.Example.Views
@@ -36,7 +36,6 @@ public ImageBackgroundView(Settings settings, NavigationService navigationServic
3636
RemoveForegroundCommand = new AsyncRelayCommand(RemoveForegroundAsync, CanExecute);
3737
SelectedModel = Settings.BackgroundModels.First(x => x.IsDefault);
3838
SelectedDevice = settings.DefaultDevice;
39-
Progress = new ProgressInfo();
4039
InitializeComponent();
4140
}
4241

@@ -49,7 +48,6 @@ public ImageBackgroundView(Settings settings, NavigationService navigationServic
4948
public AsyncRelayCommand MaskForegroundCommand { get; set; }
5049
public AsyncRelayCommand RemoveBackgroundCommand { get; set; }
5150
public AsyncRelayCommand RemoveForegroundCommand { get; set; }
52-
public ProgressInfo Progress { get; set; }
5351

5452
public Device SelectedDevice
5553
{
@@ -85,8 +83,10 @@ public ImageInput CompareImage
8583
private async Task LoadAsync()
8684
{
8785
var timestamp = Stopwatch.GetTimestamp();
88-
Progress.Indeterminate();
86+
if (!await IsModelValidAsync())
87+
return;
8988

89+
Progress.Indeterminate();
9090
var device = _selectedDevice;
9191
if (_selectedDevice is null)
9292
device = Settings.DefaultDevice;
@@ -194,5 +194,12 @@ private bool CanCancel()
194194
return BackgroundService.CanCancel;
195195
}
196196

197+
private async Task<bool> IsModelValidAsync()
198+
{
199+
if (File.Exists(SelectedModel.Path))
200+
return true;
201+
202+
return await DialogService.DownloadAsync($"Download '{SelectedModel.Name}' background model?", SelectedModel.UrlPath, SelectedModel.Path);
203+
}
197204
}
198205
}

Examples/TensorStack.Example.Extractors/Views/ImageExtractorView.xaml.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System.Diagnostics;
2+
using System.IO;
23
using System.Linq;
34
using System.Threading.Tasks;
45
using TensorStack.Common;
56
using TensorStack.Example.Common;
67
using TensorStack.Example.Services;
78
using TensorStack.Image;
89
using TensorStack.WPF;
9-
using TensorStack.WPF.Controls;
1010
using TensorStack.WPF.Services;
1111

1212
namespace TensorStack.Example.Views
@@ -35,7 +35,6 @@ public ImageExtractorView(Settings settings, NavigationService navigationService
3535
CancelCommand = new AsyncRelayCommand(CancelAsync, CanCancel);
3636
SelectedModel = settings.ExtractorModels.First(x => x.IsDefault);
3737
SelectedDevice = settings.DefaultDevice;
38-
Progress = new ProgressInfo();
3938
InitializeComponent();
4039
}
4140

@@ -45,7 +44,6 @@ public ImageExtractorView(Settings settings, NavigationService navigationService
4544
public AsyncRelayCommand UnloadCommand { get; set; }
4645
public AsyncRelayCommand ExecuteCommand { get; set; }
4746
public AsyncRelayCommand CancelCommand { get; set; }
48-
public ProgressInfo Progress { get; set; }
4947

5048
public Device SelectedDevice
5149
{
@@ -99,8 +97,10 @@ public int TileOverlap
9997
private async Task LoadAsync()
10098
{
10199
var timestamp = Stopwatch.GetTimestamp();
102-
Progress.Indeterminate();
100+
if (!await IsModelValidAsync())
101+
return;
103102

103+
Progress.Indeterminate();
104104
var device = _selectedDevice;
105105
if (_selectedDevice is null)
106106
device = Settings.DefaultDevice;
@@ -172,5 +172,12 @@ private bool CanCancel()
172172
return ExtractorService.CanCancel;
173173
}
174174

175+
private async Task<bool> IsModelValidAsync()
176+
{
177+
if (File.Exists(SelectedModel.Path))
178+
return true;
179+
180+
return await DialogService.DownloadAsync($"Download '{SelectedModel.Name}' extractor model?", SelectedModel.UrlPath, SelectedModel.Path);
181+
}
175182
}
176183
}

Examples/TensorStack.Example.Extractors/Views/VideoBackgroundView.xaml.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics;
3+
using System.IO;
34
using System.Linq;
45
using System.Threading.Tasks;
56
using TensorStack.Common;
@@ -9,7 +10,6 @@
910
using TensorStack.Extractors.Common;
1011
using TensorStack.Video;
1112
using TensorStack.WPF;
12-
using TensorStack.WPF.Controls;
1313
using TensorStack.WPF.Services;
1414

1515
namespace TensorStack.Example.Views
@@ -39,7 +39,6 @@ public VideoBackgroundView(Settings settings, NavigationService navigationServic
3939
RemoveForegroundCommand = new AsyncRelayCommand(RemoveForegroundAsync, CanExecute);
4040
SelectedModel = Settings.BackgroundModels.First(x => x.IsDefault);
4141
SelectedDevice = settings.DefaultDevice;
42-
Progress = new ProgressInfo();
4342
_progressCallback = new Progress<RunProgress>(OnProgress);
4443
InitializeComponent();
4544
}
@@ -53,7 +52,6 @@ public VideoBackgroundView(Settings settings, NavigationService navigationServic
5352
public AsyncRelayCommand MaskForegroundCommand { get; set; }
5453
public AsyncRelayCommand RemoveBackgroundCommand { get; set; }
5554
public AsyncRelayCommand RemoveForegroundCommand { get; set; }
56-
public ProgressInfo Progress { get; set; }
5755

5856
public Device SelectedDevice
5957
{
@@ -89,8 +87,10 @@ public VideoInputStream CompareVideo
8987
private async Task LoadAsync()
9088
{
9189
var timestamp = Stopwatch.GetTimestamp();
92-
Progress.Indeterminate();
90+
if (!await IsModelValidAsync())
91+
return;
9392

93+
Progress.Indeterminate();
9494
var device = _selectedDevice;
9595
if (_selectedDevice is null)
9696
device = Settings.DefaultDevice;
@@ -202,5 +202,13 @@ private void OnProgress(RunProgress progress)
202202
Progress.Update(progress.Value + 1, progress.Maximum, progress.Message);
203203
}
204204

205+
206+
private async Task<bool> IsModelValidAsync()
207+
{
208+
if (File.Exists(SelectedModel.Path))
209+
return true;
210+
211+
return await DialogService.DownloadAsync($"Download '{SelectedModel.Name}' background model?", SelectedModel.UrlPath, SelectedModel.Path);
212+
}
205213
}
206214
}

Examples/TensorStack.Example.Extractors/Views/VideoExtractorView.xaml.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics;
3+
using System.IO;
34
using System.Linq;
45
using System.Threading.Tasks;
56
using TensorStack.Common;
@@ -8,7 +9,6 @@
89
using TensorStack.Example.Services;
910
using TensorStack.Video;
1011
using TensorStack.WPF;
11-
using TensorStack.WPF.Controls;
1212
using TensorStack.WPF.Services;
1313

1414
namespace TensorStack.Example.Views
@@ -38,7 +38,6 @@ public VideoExtractorView(Settings settings, NavigationService navigationService
3838
CancelCommand = new AsyncRelayCommand(CancelAsync, CanCancel);
3939
SelectedModel = settings.ExtractorModels.First(x => x.IsDefault);
4040
SelectedDevice = settings.DefaultDevice;
41-
Progress = new ProgressInfo();
4241
_progressCallback = new Progress<RunProgress>(OnProgress);
4342
InitializeComponent();
4443
}
@@ -49,7 +48,6 @@ public VideoExtractorView(Settings settings, NavigationService navigationService
4948
public AsyncRelayCommand UnloadCommand { get; set; }
5049
public AsyncRelayCommand ExecuteCommand { get; set; }
5150
public AsyncRelayCommand CancelCommand { get; set; }
52-
public ProgressInfo Progress { get; set; }
5351

5452
public Device SelectedDevice
5553
{
@@ -103,8 +101,10 @@ public int TileOverlap
103101
private async Task LoadAsync()
104102
{
105103
var timestamp = Stopwatch.GetTimestamp();
106-
Progress.Indeterminate();
104+
if (!await IsModelValidAsync())
105+
return;
107106

107+
Progress.Indeterminate();
108108
var device = _selectedDevice;
109109
if (_selectedDevice is null)
110110
device = Settings.DefaultDevice;
@@ -183,5 +183,13 @@ private void OnProgress(RunProgress progress)
183183
Progress.Update(progress.Value + 1, progress.Maximum, progress.Message);
184184
}
185185

186+
187+
private async Task<bool> IsModelValidAsync()
188+
{
189+
if (File.Exists(SelectedModel.Path))
190+
return true;
191+
192+
return await DialogService.DownloadAsync($"Download '{SelectedModel.Name}' extractor model?", SelectedModel.UrlPath, SelectedModel.Path);
193+
}
186194
}
187195
}

Examples/TensorStack.Example.Extractors/Views/ViewBase.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using TensorStack.WPF.Controls;
1+
using System;
2+
using TensorStack.WPF.Controls;
23
using TensorStack.WPF.Services;
34

45
namespace TensorStack.Example.Views
@@ -9,9 +10,18 @@ public ViewBase(Settings settings, NavigationService navigationService)
910
: base(navigationService)
1011
{
1112
Settings = settings;
13+
Progress = new ProgressInfo();
14+
DownloadCallback = new Progress<double>(OnDownloadProgress);
1215
}
1316

1417
public Settings Settings { get; }
18+
public ProgressInfo Progress { get; }
19+
public Progress<double> DownloadCallback { get; set; }
20+
21+
protected virtual void OnDownloadProgress(double value)
22+
{
23+
Progress.Update((int)value, 100);
24+
}
1525
}
1626

1727
public enum View

Examples/TensorStack.Example.Upscaler/Common/UpscaleModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ public class UpscaleModel : BaseModel
1515
public int ScaleFactor { get; init; } = 1;
1616
public Normalization Normalization { get; init; }
1717
public string Path { get; set; }
18+
public string UrlPath { get; set; }
1819
}
1920
}

0 commit comments

Comments
 (0)