22using System . Collections . Generic ;
33using System . ComponentModel ;
44using System . Linq ;
5- using System . Text ;
65using System . Threading . Tasks ;
76using System . Windows . Input ;
87using Windows . Media . Core ;
@@ -49,18 +48,27 @@ public AppViewModel()
4948 _saveEnabled = true ;
5049 NotifyUser ( true ) ;
5150
52- m_notifier = new StyleTransferEffectNotifier ( ) ;
53- m_notifier . FrameRateUpdated += async ( _ , e ) => await DispatcherHelper . RunAsync ( ( ) => RenderFPS = e ) ;
54- _useGpu = true ;
51+ m_notifier = new StyleTransferEffectCpp . StyleTransferEffectNotifier ( ) ;
52+ m_notifier . FrameRateUpdated += async ( _ , e ) =>
53+ {
54+ await DispatcherHelper . RunAsync ( ( ) =>
55+ {
56+ RenderFPS = e ;
57+ } ) ;
58+
59+ } ;
60+ _useGpu = false ;
5561 isPreviewing = false ;
5662 }
5763
5864 // Media capture properties
5965 public Windows . Media . Capture . MediaCapture _mediaCapture ;
60- private List < MediaFrameSourceGroup > _mediaFrameSourceGroupList ;
61- private MediaFrameSourceGroup _selectedMediaFrameSourceGroup ;
6266 private bool isPreviewing ;
6367 private DisplayRequest displayRequest = new DisplayRequest ( ) ;
68+ DeviceInformationCollection devices ;
69+ System . Threading . Mutex Processing = new Mutex ( ) ;
70+ private float _renderFPS ;
71+ StyleTransferEffectCpp . StyleTransferEffectNotifier m_notifier ;
6472
6573 // Style transfer effect properties
6674 private LearningModel m_model = null ;
@@ -69,15 +77,8 @@ public AppViewModel()
6977 private LearningModelBinding m_binding ;
7078 private string m_inputImageDescription ;
7179 private string m_outputImageDescription ;
72- private IMediaExtension videoEffect ;
73- private VideoEffectDefinition videoEffectDefinition ;
7480 // Activatable Class ID of the video effect.
7581 private String _videoEffectID = "StyleTransferEffectCpp.StyleTransferEffect" ;
76- System . Threading . Mutex Processing = new Mutex ( ) ;
77- StyleTransferEffectCpp . StyleTransferEffectNotifier m_notifier ;
78- private float _renderFPS ;
79- DeviceInformationCollection devices ;
80-
8182
8283 // Image style transfer properties
8384 uint m_inWidth , m_inHeight , m_outWidth , m_outHeight ;
@@ -93,7 +94,6 @@ public AppModel CurrentApp
9394 {
9495 get { return _appModel ; }
9596 }
96-
9797 public float RenderFPS
9898 {
9999 get { return ( float ) Math . Round ( _renderFPS , 2 ) ; }
@@ -104,13 +104,9 @@ public float RenderFPS
104104 public float CaptureFPS
105105 {
106106 get
107- {
108- return _captureFPS ;
109- }
107+ { return _captureFPS ; }
110108 set
111- {
112- _captureFPS = value ; OnPropertyChanged ( ) ;
113- }
109+ { _captureFPS = value ; OnPropertyChanged ( ) ; }
114110 }
115111 private bool _useGpu ;
116112 public bool UseGpu
@@ -172,28 +168,26 @@ public async void SaveOutput()
172168 public async Task SetMediaSource ( string src )
173169 {
174170 _appModel . InputMedia = src ;
171+ await CleanupCameraAsync ( ) ;
172+ CleanupInputImage ( ) ;
175173
176174 NotifyUser ( true ) ;
177175 SaveEnabled = true ;
178176
179- // Changes media source while keeping all other controls
180177 switch ( _appModel . InputMedia )
181178 {
182179 case "LiveStream" :
183180 await StartLiveStream ( ) ;
184181 break ;
185182 case "AcquireImage" :
186- CleanupInputImage ( ) ;
187183 await StartAcquireImage ( ) ;
188184 break ;
189185 case "FilePick" :
190- CleanupInputImage ( ) ;
191186 await StartFilePick ( ) ;
192187 break ;
193- case "Inking" :
188+ default :
194189 break ;
195190 }
196-
197191 return ;
198192 }
199193
@@ -213,7 +207,7 @@ public async Task SetModelSource()
213207 case "FilePick" :
214208 await ChangeImage ( ) ;
215209 break ;
216- case "Inking" :
210+ default :
217211 break ;
218212 }
219213 }
@@ -255,9 +249,6 @@ public async Task StartFilePick()
255249 var file = await StorageFile . GetFileFromApplicationUriAsync ( new Uri ( $ "ms-appx:///Assets/{ _DefaultImageFileName } ") ) ;
256250 _appModel . InputFrame = await ImageHelper . LoadVideoFrameFromStorageFileAsync ( file ) ;
257251 }
258- else
259- {
260- }
261252 await ChangeImage ( ) ;
262253 }
263254 catch ( Exception ex )
@@ -276,7 +267,6 @@ public async Task ChangeImage()
276267 var file = await StorageFile . GetFileFromApplicationUriAsync ( new Uri ( $ "ms-appx:///Assets/{ _DefaultImageFileName } ") ) ;
277268 _appModel . InputFrame = await ImageHelper . LoadVideoFrameFromStorageFileAsync ( file ) ;
278269 }
279-
280270 await EvaluateVideoFrameAsync ( ) ;
281271 }
282272
@@ -374,7 +364,6 @@ public async Task ChangeLiveStream()
374364 var settings = new MediaCaptureInitializationSettings
375365 {
376366 VideoDeviceId = device . Id ,
377- SourceGroup = _selectedMediaFrameSourceGroup ,
378367 PhotoCaptureSource = PhotoCaptureSource . Auto ,
379368 MemoryPreference = UseGpu ? MediaCaptureMemoryPreference . Auto : MediaCaptureMemoryPreference . Cpu ,
380369 StreamingCaptureMode = StreamingCaptureMode . Video ,
@@ -389,11 +378,11 @@ public async Task ChangeLiveStream()
389378 _appModel . OutputCaptureElement = capture ;
390379
391380 var modelPath = Path . GetFullPath ( $ "./Assets/{ _appModel . ModelSource } .onnx") ;
392- videoEffectDefinition = new VideoEffectDefinition ( _videoEffectID , new PropertySet ( ) {
381+ VideoEffectDefinition videoEffectDefinition = new VideoEffectDefinition ( _videoEffectID , new PropertySet ( ) {
393382 { "ModelName" , modelPath } ,
394383 { "UseGpu" , UseGpu } ,
395384 { "Notifier" , m_notifier } } ) ;
396- videoEffect = await _mediaCapture . AddVideoEffectAsync ( videoEffectDefinition , MediaStreamType . VideoPreview ) ;
385+ IMediaExtension videoEffect = await _mediaCapture . AddVideoEffectAsync ( videoEffectDefinition , MediaStreamType . VideoPreview ) ;
397386
398387 var props = _mediaCapture . VideoDeviceController . GetMediaStreamProperties ( MediaStreamType . VideoPreview ) as VideoEncodingProperties ;
399388 CaptureFPS = props . FrameRate . Numerator / props . FrameRate . Denominator ;
@@ -519,14 +508,22 @@ private void CleanupInputImage()
519508 {
520509 try
521510 {
522- InputSoftwareBitmapSource ? . Dispose ( ) ;
523- OutputSoftwareBitmapSource ? . Dispose ( ) ;
524-
525- InputSoftwareBitmapSource = new SoftwareBitmapSource ( ) ;
526- OutputSoftwareBitmapSource = new SoftwareBitmapSource ( ) ;
527-
528- _appModel . OutputFrame ? . Dispose ( ) ;
511+ if ( InputSoftwareBitmapSource != null )
512+ {
513+ InputSoftwareBitmapSource . Dispose ( ) ;
514+ InputSoftwareBitmapSource = new SoftwareBitmapSource ( ) ;
515+ }
516+ if ( OutputSoftwareBitmapSource != null )
517+ {
518+ OutputSoftwareBitmapSource . Dispose ( ) ;
519+ OutputSoftwareBitmapSource = new SoftwareBitmapSource ( ) ;
520+ }
521+ if ( _appModel . OutputFrame != null )
522+ {
523+ _appModel . OutputFrame . Dispose ( ) ;
524+ }
529525 _appModel . OutputFrame = new VideoFrame ( BitmapPixelFormat . Bgra8 , ( int ) m_outWidth , ( int ) m_outHeight ) ;
526+
530527 }
531528 catch ( Exception e )
532529 {
0 commit comments