@@ -66,13 +66,13 @@ public AppModel CurrentApp
6666 public ICommand ChangeLiveStreamCommand { get ; set ; }
6767 public ICommand SetModelSourceCommand { get ; set ; }
6868
69- private SoftwareBitmapSource _inputSoftwareBitmapSource ;
69+ private SoftwareBitmapSource _inputSoftwareBitmapSource = new SoftwareBitmapSource ( ) ;
7070 public SoftwareBitmapSource InputSoftwareBitmapSource
7171 {
7272 get { return _inputSoftwareBitmapSource ; }
7373 set { _inputSoftwareBitmapSource = value ; OnPropertyChanged ( ) ; }
7474 }
75- private SoftwareBitmapSource _outputSoftwareBitmapSource ;
75+ private SoftwareBitmapSource _outputSoftwareBitmapSource = new SoftwareBitmapSource ( ) ;
7676 public SoftwareBitmapSource OutputSoftwareBitmapSource
7777 {
7878 get { return _outputSoftwareBitmapSource ; }
@@ -92,20 +92,24 @@ public async Task SetMediaSource(string obj)
9292 _appModel . InputMedia = obj ;
9393
9494 // TODO: Reset media source stuff: set Camera input controls visibility to 0, etc.
95- await CleanupCameraAsync ( ) ;
95+ CleanupCameraAsync ( ) ;
9696 CleanupInputImage ( ) ;
9797
98+
9899 // Changes media source while keeping all other controls
99100 switch ( _appModel . InputMedia )
100101 {
101102 case "LiveStream" :
103+
102104 await StartLiveStream ( ) ;
103105 // TODO: Also spin up a Capture for preview on left side
104106 break ;
105107 case "AcquireImage" :
108+
109+ await StartAcquireImage ( ) ;
106110 break ;
107111 case "FilePick" :
108- // HelperMethods::LoadVideoFrameFromFilePickedAsync
112+
109113 await StartFilePick ( ) ;
110114 break ;
111115 case "Inking" :
@@ -120,13 +124,14 @@ public async Task SetModelSource()
120124 // Clean up model, etc. by setting to null?
121125 // Based on InputMedia, call ChangeLiveStream/ChangeCamera/ChangeImage
122126 await LoadModelAsync ( ) ;
127+
123128 switch ( _appModel . InputMedia )
124129 {
125130 case "LiveStream" :
126131 await ChangeLiveStream ( ) ;
127- // TODO: Also spin up a Capture for preview on left side
128132 break ;
129133 case "AcquireImage" :
134+ await ChangeFilePick ( ) ;
130135 break ;
131136 case "FilePick" :
132137 await ChangeFilePick ( ) ;
@@ -136,10 +141,32 @@ public async Task SetModelSource()
136141 }
137142 }
138143
144+
145+ private async Task StartAcquireImage ( )
146+ {
147+
148+ CameraCaptureUI dialog = new CameraCaptureUI ( ) ;
149+ dialog . PhotoSettings . AllowCropping = false ;
150+ dialog . PhotoSettings . Format = CameraCaptureUIPhotoFormat . Png ;
151+
152+ StorageFile file = await dialog . CaptureFileAsync ( CameraCaptureUIMode . Photo ) ;
153+
154+ if ( file != null )
155+ {
156+ _appModel . InputFrame = await ImageHelper . LoadVideoFrameFromStorageFileAsync ( file ) ;
157+
158+ }
159+ else
160+ {
161+ Debug . WriteLine ( "Failed to capture image" ) ;
162+ }
163+
164+ await ChangeFilePick ( ) ;
165+ }
166+
139167 public async Task StartFilePick ( )
140168 {
141169 Debug . WriteLine ( "StartFilePick" ) ;
142- InputSoftwareBitmapSource = new SoftwareBitmapSource ( ) ;
143170
144171 try
145172 {
@@ -176,7 +203,6 @@ public async Task ChangeFilePick()
176203
177204 private async Task EvaluateVideoFrameAsync ( )
178205 {
179- OutputSoftwareBitmapSource = new SoftwareBitmapSource ( ) ;
180206
181207 if ( ( _appModel . InputFrame != null ) &&
182208 ( _appModel . InputFrame . SoftwareBitmap != null || _appModel . InputFrame . Direct3DSurface != null ) )
@@ -295,6 +321,8 @@ private void StartPreview()
295321
296322 private async Task LoadModelAsync ( )
297323 {
324+ m_model ? . Dispose ( ) ;
325+ m_session ? . Dispose ( ) ;
298326
299327 StorageFile modelFile = await StorageFile . GetFileFromApplicationUriAsync ( new Uri ( $ "ms-appx:///Assets/{ _appModel . ModelSource } .onnx") ) ;
300328 m_model = await LearningModel . LoadFromStorageFileAsync ( modelFile ) ;
@@ -346,17 +374,13 @@ public void debugModelIO()
346374 }
347375 }
348376
349- private async Task CleanupCameraAsync ( )
377+ private void CleanupCameraAsync ( )
350378 {
351379 Debug . WriteLine ( "CleanupCameraAsync" ) ;
352380 try
353381 {
354- if ( _mediaCapture != null )
355- {
356- if ( videoEffect != null ) await _mediaCapture . RemoveEffectAsync ( videoEffect ) ;
357- await _mediaCapture . StopRecordAsync ( ) ;
358- _mediaCapture = null ;
359- }
382+ _mediaCapture ? . Dispose ( ) ;
383+ _appModel . OutputMediaSource ? . Dispose ( ) ;
360384 if ( _appModel . OutputMediaSource != null )
361385 {
362386 _appModel . OutputMediaSource = null ;
@@ -379,6 +403,9 @@ private void CleanupInputImage()
379403 InputSoftwareBitmapSource ? . Dispose ( ) ;
380404 OutputSoftwareBitmapSource ? . Dispose ( ) ;
381405
406+ InputSoftwareBitmapSource = new SoftwareBitmapSource ( ) ;
407+ OutputSoftwareBitmapSource = new SoftwareBitmapSource ( ) ;
408+
382409 _appModel . OutputFrame ? . Dispose ( ) ;
383410 _appModel . OutputFrame = new VideoFrame ( BitmapPixelFormat . Bgra8 , ( int ) m_outWidth , ( int ) m_outHeight ) ;
384411 }
0 commit comments