Skip to content

Commit a836167

Browse files
Image Upload style transfer, need to crop input image
1 parent f0115ed commit a836167

File tree

6 files changed

+347
-4
lines changed

6 files changed

+347
-4
lines changed

Samples/StyleTransfer/AppModel.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,29 @@ public bool UseGPU
5959
}
6060
}
6161

62+
// Input VideoFrame when processing images
63+
private VideoFrame _inputFrame;
64+
public VideoFrame InputFrame
65+
{
66+
get { return _inputFrame; }
67+
set
68+
{
69+
_inputFrame = value;
70+
OnPropertyChanged();
71+
}
72+
}
73+
74+
// o
75+
private VideoFrame _outputFrame;
76+
public VideoFrame OutputFrame
77+
{
78+
get { return _outputFrame; }
79+
set
80+
{
81+
_outputFrame = value;
82+
OnPropertyChanged();
83+
}
84+
}
6285
// MediaSource for transformed media
6386
private MediaSource _outputMediaSource;
6487
public MediaSource OutputMediaSource

Samples/StyleTransfer/AppViewModel.cs

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
using Windows.AI.MachineLearning;
1818
using Windows.Storage;
1919
using System.Runtime.CompilerServices;
20+
using Windows.Graphics.Imaging;
21+
using Windows.UI.Xaml.Media.Imaging;
2022

2123
namespace StyleTransfer
2224
{
23-
class AppViewModel
25+
class AppViewModel : INotifyPropertyChanged
2426
{
2527
public AppViewModel()
2628
{
@@ -46,6 +48,11 @@ public AppViewModel()
4648
string m_outputImageDescription;
4749
IMediaExtension videoEffect;
4850

51+
// Image style transfer properties
52+
private bool _showInitialImage;
53+
uint m_inWidth, m_inHeight, m_outWidth, m_outHeight;
54+
private string _DefaultImageFileName = "Input.jpg";
55+
4956

5057
private AppModel _appModel;
5158
public AppModel CurrentApp
@@ -59,6 +66,19 @@ public AppModel CurrentApp
5966
public ICommand ChangeLiveStreamCommand { get; set; }
6067
public ICommand SetModelSourceCommand { get; set; }
6168

69+
private SoftwareBitmapSource _inputSoftwareBitmapSource;
70+
public SoftwareBitmapSource InputSoftwareBitmapSource
71+
{
72+
get { return _inputSoftwareBitmapSource; }
73+
set { _inputSoftwareBitmapSource = value; OnPropertyChanged(); }
74+
}
75+
private SoftwareBitmapSource _outputSoftwareBitmapSource;
76+
public SoftwareBitmapSource OutputSoftwareBitmapSource
77+
{
78+
get { return _outputSoftwareBitmapSource; }
79+
set { _outputSoftwareBitmapSource = value; OnPropertyChanged(); }
80+
}
81+
6282
public void SaveOutput()
6383
{
6484
// TODO: Take from UIButtonSaveImage_Click
@@ -71,7 +91,7 @@ public async Task SetMediaSource(string obj)
7191

7292
// TODO: Reset media source stuff: set Camera input controls visibility to 0, etc.
7393
await CleanupCameraAsync();
74-
// TODO: CleanupInputImage()
94+
CleanupInputImage();
7595

7696
// Changes media source while keeping all other controls
7797
switch (_appModel.InputMedia)
@@ -84,6 +104,7 @@ public async Task SetMediaSource(string obj)
84104
break;
85105
case "FilePick":
86106
// HelperMethods::LoadVideoFrameFromFilePickedAsync
107+
await StartFilePick();
87108
break;
88109
case "Inking":
89110
break;
@@ -106,12 +127,76 @@ public async Task SetModelSource()
106127
case "AcquireImage":
107128
break;
108129
case "FilePick":
130+
await ChangeFilePick();
109131
break;
110132
case "Inking":
111133
break;
112134
}
113135
}
114136

137+
public async Task StartFilePick()
138+
{
139+
Debug.WriteLine("StartFilePick");
140+
try
141+
{
142+
// Load image to VideoFrame
143+
_appModel.InputFrame = await ImageHelper.LoadVideoFrameFromFilePickedAsync();
144+
if (_appModel.InputFrame == null)
145+
{
146+
Debug.WriteLine("no valid image file selected");
147+
}
148+
else
149+
{
150+
await ChangeFilePick();
151+
}
152+
153+
}
154+
catch (Exception ex)
155+
{
156+
Debug.WriteLine($"error: {ex.Message}");
157+
//NotifyUser(ex.Message, NotifyType.ErrorMessage);
158+
}
159+
}
160+
161+
public async Task ChangeFilePick()
162+
{
163+
// Make sure have an input image, use default otherwise
164+
if (_appModel.InputFrame == null)
165+
{
166+
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{_DefaultImageFileName}"));
167+
_appModel.InputFrame = await ImageHelper.LoadVideoFrameFromStorageFileAsync(file);
168+
}
169+
170+
await EvaluateVideoFrameAsync();
171+
}
172+
173+
private async Task EvaluateVideoFrameAsync()
174+
{
175+
InputSoftwareBitmapSource = new SoftwareBitmapSource();
176+
OutputSoftwareBitmapSource = new SoftwareBitmapSource();
177+
178+
if ((_appModel.InputFrame != null) &&
179+
(_appModel.InputFrame.SoftwareBitmap != null || _appModel.InputFrame.Direct3DSurface != null))
180+
{
181+
// _appModel.InputFrame = await ImageHelper.CenterCropImageAsync(inputVideoFrame, m_inWidth, m_inHeight);
182+
183+
m_binding.Bind(m_inputImageDescription, ImageFeatureValue.CreateFromVideoFrame(_appModel.InputFrame));
184+
m_binding.Bind(m_outputImageDescription, ImageFeatureValue.CreateFromVideoFrame(_appModel.OutputFrame));
185+
186+
var results = m_session.Evaluate(m_binding, "test");
187+
188+
// Parse Results
189+
IReadOnlyDictionary<string, object> outputs = results.Outputs;
190+
foreach (var output in outputs)
191+
{
192+
Debug.WriteLine($"{output.Key} : {output.Value} -> {output.Value.GetType()}");
193+
}
194+
195+
await InputSoftwareBitmapSource.SetBitmapAsync(_appModel.InputFrame.SoftwareBitmap);
196+
await OutputSoftwareBitmapSource.SetBitmapAsync(_appModel.OutputFrame.SoftwareBitmap);
197+
198+
}
199+
}
115200
public async Task StartLiveStream()
116201
{
117202
Debug.WriteLine("StartLiveStream");
@@ -221,11 +306,13 @@ private async Task LoadModelAsync()
221306
m_inputImageDescription = m_model.InputFeatures.ToList().First().Name;
222307

223308
m_outputImageDescription = m_model.OutputFeatures.ToList().First().Name;
309+
310+
_appModel.OutputFrame?.Dispose();
311+
_appModel.OutputFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)m_outWidth, (int)m_outHeight);
224312
}
225313

226314
public void debugModelIO()
227315
{
228-
uint m_inWidth, m_inHeight, m_outWidth, m_outHeight;
229316
string m_inName, m_outName;
230317
foreach (var inputF in m_model.InputFeatures)
231318
{
@@ -286,5 +373,18 @@ private async Task CleanupCameraAsync()
286373
Debug.WriteLine($"CleanupCameraAsync: {ex.Message}");
287374
}
288375
}
376+
377+
private void CleanupInputImage()
378+
{
379+
InputSoftwareBitmapSource?.Dispose();
380+
OutputSoftwareBitmapSource?.Dispose();
381+
382+
}
383+
384+
public event PropertyChangedEventHandler PropertyChanged;
385+
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
386+
{
387+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
388+
}
289389
}
290390
}

0 commit comments

Comments
 (0)