2727using Windows . System . Display ;
2828using Windows . UI . Core ;
2929using GalaSoft . MvvmLight . Threading ;
30+ using System . Threading ;
3031
3132namespace StyleTransfer
3233{
@@ -65,6 +66,7 @@ public AppViewModel()
6566 private VideoEffectDefinition videoEffectDefinition ;
6667 // Activatable Class ID of the video effect.
6768 private String _videoEffectID = "StyleTransferEffectCpp.StyleTransferEffect" ;
69+ System . Threading . Mutex Processing = new Mutex ( ) ;
6870
6971 // Image style transfer properties
7072 uint m_inWidth , m_inHeight , m_outWidth , m_outHeight ;
@@ -154,7 +156,7 @@ public async Task SetMediaSource(string src)
154156
155157 public async Task SetModelSource ( )
156158 {
157- //CleanupCameraAsync( );
159+ Debug . WriteLine ( "SetModelSource" ) ;
158160 await LoadModelAsync ( ) ;
159161
160162 switch ( _appModel . InputMedia )
@@ -206,7 +208,9 @@ public async Task StartFilePick()
206208 _appModel . InputFrame = await ImageHelper . LoadVideoFrameFromFilePickedAsync ( ) ;
207209 if ( _appModel . InputFrame == null )
208210 {
209- Debug . WriteLine ( "no valid image file selected" ) ;
211+ NotifyUser ( false , "No valid image file selected, using default image instead." ) ;
212+ var file = await StorageFile . GetFileFromApplicationUriAsync ( new Uri ( $ "ms-appx:///Assets/{ _DefaultImageFileName } ") ) ;
213+ _appModel . InputFrame = await ImageHelper . LoadVideoFrameFromStorageFileAsync ( file ) ;
210214 }
211215 else
212216 {
@@ -241,11 +245,17 @@ private async Task EvaluateVideoFrameAsync()
241245 ( _appModel . InputFrame . SoftwareBitmap != null || _appModel . InputFrame . Direct3DSurface != null ) )
242246 {
243247 _appModel . InputFrame = await ImageHelper . CenterCropImageAsync ( _appModel . InputFrame , m_inWidth , m_inHeight ) ;
248+ await InputSoftwareBitmapSource . SetBitmapAsync ( _appModel . InputFrame . SoftwareBitmap ) ;
244249
250+ // Lock so eval + binding not destroyed mid-evaluation
251+ Debug . Write ( "Eval Begin | " ) ;
252+ Processing . WaitOne ( ) ;
253+ Debug . Write ( "Eval Lock | " ) ;
245254 m_binding . Bind ( m_inputImageDescription , ImageFeatureValue . CreateFromVideoFrame ( _appModel . InputFrame ) ) ;
246255 m_binding . Bind ( m_outputImageDescription , ImageFeatureValue . CreateFromVideoFrame ( _appModel . OutputFrame ) ) ;
247-
248256 var results = m_session . Evaluate ( m_binding , "test" ) ;
257+ Processing . ReleaseMutex ( ) ;
258+ Debug . Write ( "Eval Unlock\n " ) ;
249259
250260 // Parse Results
251261 IReadOnlyDictionary < string , object > outputs = results . Outputs ;
@@ -254,7 +264,6 @@ private async Task EvaluateVideoFrameAsync()
254264 Debug . WriteLine ( $ "{ output . Key } : { output . Value } -> { output . Value . GetType ( ) } ") ;
255265 }
256266
257- await InputSoftwareBitmapSource . SetBitmapAsync ( _appModel . InputFrame . SoftwareBitmap ) ;
258267 await OutputSoftwareBitmapSource . SetBitmapAsync ( _appModel . OutputFrame . SoftwareBitmap ) ;
259268 }
260269 }
@@ -342,7 +351,11 @@ public async Task ChangeLiveStream()
342351
343352 private async Task LoadModelAsync ( )
344353 {
354+ Debug . Write ( "LoadModelBegin | " ) ;
355+ Processing . WaitOne ( ) ;
356+ Debug . Write ( "LoadModel Lock | " ) ;
345357
358+ m_binding ? . Clear ( ) ;
346359 m_session ? . Dispose ( ) ;
347360
348361 StorageFile modelFile = await StorageFile . GetFileFromApplicationUriAsync ( new Uri ( $ "ms-appx:///Assets/{ _appModel . ModelSource } .onnx") ) ;
@@ -356,6 +369,8 @@ private async Task LoadModelAsync()
356369
357370 m_inputImageDescription = m_model . InputFeatures . ToList ( ) . First ( ) . Name ;
358371 m_outputImageDescription = m_model . OutputFeatures . ToList ( ) . First ( ) . Name ;
372+ Processing . ReleaseMutex ( ) ;
373+ Debug . Write ( "LoadModel Unlock\n " ) ;
359374 }
360375
361376 public void debugModelIO ( )
0 commit comments