1616using Windows . Foundation . Collections ;
1717using Windows . AI . MachineLearning ;
1818using Windows . Storage ;
19+ using System . Runtime . CompilerServices ;
1920
2021namespace StyleTransfer
2122{
@@ -36,12 +37,12 @@ public AppViewModel()
3637 private MediaFrameSourceGroup _selectedMediaFrameSourceGroup ;
3738 private MediaFrameSource _selectedMediaFrameSource ;
3839
39- private IDictionary < string , object > modelSetup ;
4040 private LearningModel m_model = null ;
4141 private LearningModelDeviceKind m_inferenceDeviceSelected = LearningModelDeviceKind . Default ;
4242 private LearningModelSession m_session ;
4343 string _inputImageDescription ;
4444 string _outputImageDescription ;
45+ IMediaExtension videoEffect ;
4546
4647 private AppModel _appModel ;
4748 public AppModel CurrentApp
@@ -63,7 +64,23 @@ public ICommand SaveCommand
6364 public ICommand LiveStreamCommand { get ; set ; }
6465 public ICommand ChangeMediaInputCommand { get ; set ; }
6566
67+ private int _selectedCameraIndex ;
68+ public int SelectedCameraIndex
69+ {
70+ get { return _selectedCameraIndex ; }
71+ set
72+ {
73+ _selectedCameraIndex = value ;
74+ ChangeMediaInput ( ) ;
75+ OnPropertyChanged ( ) ;
76+ }
77+ }
78+
6679 public event PropertyChangedEventHandler PropertyChanged ;
80+ private void OnPropertyChanged ( [ CallerMemberName ] string propertyName = null )
81+ {
82+ PropertyChanged ? . Invoke ( this , new PropertyChangedEventArgs ( propertyName ) ) ;
83+ }
6784
6885 public void SaveOutput ( )
6986 {
@@ -95,34 +112,29 @@ public async Task StartWebcamStream()
95112 }
96113
97114 _appModel . CameraNamesList = _mediaFrameSourceGroupList . Select ( group => group . DisplayName ) ;
98- _appModel . SelectedCameraIndex = 0 ;
115+ SelectedCameraIndex = 0 ;
99116 }
100117
101118 private async Task LoadModelAsync ( )
102119 {
103- modelSetup = new Dictionary < string , object > ( ) ;
104120
105121 StorageFile modelFile = await StorageFile . GetFileFromApplicationUriAsync ( new Uri ( "ms-appx:///Assets/candy.onnx" ) ) ;
106122 m_model = await LearningModel . LoadFromStorageFileAsync ( modelFile ) ;
107- modelSetup . Add ( "Model" , m_model ) ;
108123
109124 // TODO: Pass in useGPU as well. OR decide which side of binary these go on.
110125 //m_inferenceDeviceSelected = _useGPU ? LearningModelDeviceKind.DirectXHighPerformance : LearningModelDeviceKind.Cpu;
111126 m_inferenceDeviceSelected = LearningModelDeviceKind . Cpu ;
112127 m_session = new LearningModelSession ( m_model , new LearningModelDevice ( m_inferenceDeviceSelected ) ) ;
113- modelSetup . Add ( "Session" , m_session ) ;
114128
115129 debugIO ( ) ;
116130
117131 _inputImageDescription = m_model . InputFeatures . ToList ( ) . First ( ) . Name ;
118132 //m_model.InputFeatures.FirstOrDefault(feature => feature.Kind == LearningModelFeatureKind.Tensor)
119133 //as ImageFeatureDescriptor;
120- modelSetup . Add ( "InputImageDescription" , _inputImageDescription ) ;
121134
122135 _outputImageDescription = m_model . OutputFeatures . ToList ( ) . First ( ) . Name ;
123136 //m_model.OutputFeatures.FirstOrDefault(feature => feature.Kind == LearningModelFeatureKind.Tensor)
124137 //as ImageFeatureDescriptor;
125- modelSetup . Add ( "OutputImageDescription" , _outputImageDescription ) ;
126138 }
127139
128140 public void debugIO ( )
@@ -216,9 +228,12 @@ private void StartPreview()
216228 private async Task ChangeMediaInput ( )
217229 {
218230 // UICmbCamera_SelectionChanged
231+
232+ //await CleanupCameraAsync();
233+
219234 try
220235 {
221- _selectedMediaFrameSourceGroup = _mediaFrameSourceGroupList [ _appModel . SelectedCameraIndex ] ;
236+ _selectedMediaFrameSourceGroup = _mediaFrameSourceGroupList [ SelectedCameraIndex ] ;
222237
223238 // Create MediaCapture and its settings
224239 _mediaCapture = new MediaCapture ( ) ;
@@ -235,8 +250,8 @@ private async Task ChangeMediaInput()
235250 await LoadModelAsync ( ) ;
236251
237252 // Initialize VideoEffect
238- var videoEffectDefinition = new VideoEffectDefinition ( "StyleTransferEffectComponent.StyleTransferVideoEffect" ) ;
239- IMediaExtension videoEffect = await _mediaCapture . AddVideoEffectAsync ( videoEffectDefinition , MediaStreamType . VideoPreview ) ;
253+ VideoEffectDefinition videoEffectDefinition = new VideoEffectDefinition ( "StyleTransferEffectComponent.StyleTransferVideoEffect" ) ;
254+ videoEffect = await _mediaCapture . AddVideoEffectAsync ( videoEffectDefinition , MediaStreamType . VideoPreview ) ;
240255 // Try loading the model here and passing as a property instead
241256 videoEffect . SetProperties ( new PropertySet ( ) {
242257 { "Model" , m_model } ,
@@ -251,5 +266,36 @@ private async Task ChangeMediaInput()
251266 Debug . WriteLine ( ex . ToString ( ) ) ;
252267 }
253268 }
269+
270+ private async Task CleanupCameraAsync ( )
271+ {
272+ Debug . WriteLine ( "CleanupCameraAsync" ) ;
273+
274+ try
275+ {
276+ if ( _mediaCapture != null )
277+ {
278+ if ( videoEffect != null ) await _mediaCapture . RemoveEffectAsync ( videoEffect ) ;
279+ await _mediaCapture . StopRecordAsync ( ) ;
280+ _mediaCapture = null ;
281+ }
282+ if ( _appModel . OutputMediaSource != null )
283+ {
284+ _appModel . OutputMediaSource = null ;
285+ }
286+ if ( videoEffect != null )
287+ {
288+ videoEffect = null ;
289+ }
290+
291+ // Add inputmediasource once can show both at the same time
292+ }
293+
294+ catch ( Exception ex )
295+ {
296+ Debug . WriteLine ( $ "CleanupCameraAsync: { ex . Message } ") ;
297+ }
298+
299+ }
254300 }
255301}
0 commit comments