1414using Windows . Media . Effects ;
1515using Windows . Media ;
1616using Windows . Foundation . Collections ;
17+ using Microsoft . AI . MachineLearning ;
18+ using Windows . Storage ;
1719
1820namespace StyleTransfer
1921{
@@ -33,6 +35,15 @@ public AppViewModel()
3335 private MediaFrameSourceGroup _selectedMediaFrameSourceGroup ;
3436 private MediaFrameSource _selectedMediaFrameSource ;
3537
38+ private LearningModel m_model = null ;
39+ private LearningModelDeviceKind m_inferenceDeviceSelected = LearningModelDeviceKind . Default ;
40+ private LearningModelDevice m_device ;
41+ private LearningModelSession m_session ;
42+ private LearningModelBinding m_binding ;
43+ string m_outName , m_inName ;
44+ ImageFeatureDescriptor _inputImageDescription ;
45+ ImageFeatureDescriptor _outputImageDescription ;
46+
3647 private AppModel _appModel ;
3748 public AppModel CurrentApp
3849 {
@@ -103,10 +114,12 @@ public async Task StartWebcamStream()
103114 // Initialize MediaCapture
104115 await _mediaCapture . InitializeAsync ( settings ) ;
105116
117+
106118 // Initialize VideoEffect
107- var videoEffectDefinition = new VideoEffectDefinition ( "VideoEffectComponent .StyleTransferVideoEffect" ) ;
119+ var videoEffectDefinition = new VideoEffectDefinition ( "StyleTransferEffectComponent .StyleTransferVideoEffect" ) ;
108120 IMediaExtension videoEffect = await _mediaCapture . AddVideoEffectAsync ( videoEffectDefinition , MediaStreamType . VideoPreview ) ;
109- videoEffect . SetProperties ( new PropertySet ( ) { { "FadeValue" , .15 } } ) ;
121+ // Try loading the model here and passing as a property instead
122+ videoEffect . SetProperties ( new PropertySet ( ) { { "ModelName" , "candy" } } ) ; // need to await this first
110123
111124 StartPreview ( ) ;
112125 }
@@ -116,6 +129,24 @@ public async Task StartWebcamStream()
116129 }
117130 }
118131
132+ private async Task LoadModelAsync ( String modelFileName )
133+ {
134+ StorageFile modelFile = await StorageFile . GetFileFromApplicationUriAsync ( new Uri ( $ "ms-appx:///Assets/{ modelFileName } .onnx") ) ;
135+ m_model = await LearningModel . LoadFromStorageFileAsync ( modelFile ) ;
136+
137+ // TODO: Pass in useGPU as well. OR decide which side of binary these go on.
138+ //m_inferenceDeviceSelected = _useGPU ? LearningModelDeviceKind.DirectXHighPerformance : LearningModelDeviceKind.Cpu;
139+ m_inferenceDeviceSelected = LearningModelDeviceKind . Cpu ;
140+ m_session = new LearningModelSession ( m_model , new LearningModelDevice ( m_inferenceDeviceSelected ) ) ;
141+
142+ _inputImageDescription =
143+ m_model . InputFeatures . FirstOrDefault ( feature => feature . Kind == LearningModelFeatureKind . Image )
144+ as ImageFeatureDescriptor ;
145+
146+ _outputImageDescription =
147+ m_model . OutputFeatures . FirstOrDefault ( feature => feature . Kind == LearningModelFeatureKind . Image )
148+ as ImageFeatureDescriptor ;
149+ }
119150
120151 public void SetMediaSource ( object obj )
121152 {
0 commit comments