1414using Windows . Media . Effects ;
1515using Windows . Media ;
1616using Windows . Foundation . Collections ;
17- using Microsoft . AI . MachineLearning ;
17+ using Windows . AI . MachineLearning ;
1818using Windows . Storage ;
1919
2020namespace StyleTransfer
@@ -35,14 +35,15 @@ public AppViewModel()
3535 private MediaFrameSourceGroup _selectedMediaFrameSourceGroup ;
3636 private MediaFrameSource _selectedMediaFrameSource ;
3737
38+ private IDictionary < string , object > modelSetup ;
3839 private LearningModel m_model = null ;
3940 private LearningModelDeviceKind m_inferenceDeviceSelected = LearningModelDeviceKind . Default ;
4041 private LearningModelDevice m_device ;
4142 private LearningModelSession m_session ;
4243 private LearningModelBinding m_binding ;
4344 string m_outName , m_inName ;
44- ImageFeatureDescriptor _inputImageDescription ;
45- ImageFeatureDescriptor _outputImageDescription ;
45+ string _inputImageDescription ;
46+ string _outputImageDescription ;
4647
4748 private AppModel _appModel ;
4849 public AppModel CurrentApp
@@ -113,13 +114,17 @@ public async Task StartWebcamStream()
113114
114115 // Initialize MediaCapture
115116 await _mediaCapture . InitializeAsync ( settings ) ;
116-
117+ await LoadModelAsync ( ) ;
117118
118119 // Initialize VideoEffect
119120 var videoEffectDefinition = new VideoEffectDefinition ( "StyleTransferEffectComponent.StyleTransferVideoEffect" ) ;
120121 IMediaExtension videoEffect = await _mediaCapture . AddVideoEffectAsync ( videoEffectDefinition , MediaStreamType . VideoPreview ) ;
121122 // Try loading the model here and passing as a property instead
122- videoEffect . SetProperties ( new PropertySet ( ) { { "ModelName" , "candy" } } ) ; // need to await this first
123+ videoEffect . SetProperties ( new PropertySet ( ) {
124+ { "Model" , m_model } ,
125+ { "Session" , m_session } ,
126+ { "InputImageDescription" , _inputImageDescription } ,
127+ { "OutputImageDescription" , _outputImageDescription } } ) ;
123128
124129 StartPreview ( ) ;
125130 }
@@ -129,25 +134,68 @@ public async Task StartWebcamStream()
129134 }
130135 }
131136
132- private async Task LoadModelAsync ( String modelFileName )
137+ private async Task LoadModelAsync ( )
133138 {
134- StorageFile modelFile = await StorageFile . GetFileFromApplicationUriAsync ( new Uri ( $ "ms-appx:///Assets/{ modelFileName } .onnx") ) ;
139+ modelSetup = new Dictionary < string , object > ( ) ;
140+
141+ StorageFile modelFile = await StorageFile . GetFileFromApplicationUriAsync ( new Uri ( "ms-appx:///Assets/candy.onnx" ) ) ;
135142 m_model = await LearningModel . LoadFromStorageFileAsync ( modelFile ) ;
143+ modelSetup . Add ( "Model" , m_model ) ;
136144
137145 // TODO: Pass in useGPU as well. OR decide which side of binary these go on.
138146 //m_inferenceDeviceSelected = _useGPU ? LearningModelDeviceKind.DirectXHighPerformance : LearningModelDeviceKind.Cpu;
139147 m_inferenceDeviceSelected = LearningModelDeviceKind . Cpu ;
140148 m_session = new LearningModelSession ( m_model , new LearningModelDevice ( m_inferenceDeviceSelected ) ) ;
149+ modelSetup . Add ( "Session" , m_session ) ;
150+
151+ debugIO ( ) ;
141152
142- _inputImageDescription =
143- m_model . InputFeatures . FirstOrDefault ( feature => feature . Kind == LearningModelFeatureKind . Image )
144- as ImageFeatureDescriptor ;
153+ _inputImageDescription = m_model . InputFeatures . ToList ( ) . First ( ) . Name ;
154+ //m_model.InputFeatures.FirstOrDefault(feature => feature.Kind == LearningModelFeatureKind.Tensor)
155+ //as ImageFeatureDescriptor;
156+ modelSetup . Add ( "InputImageDescription" , _inputImageDescription ) ;
145157
146- _outputImageDescription =
147- m_model . OutputFeatures . FirstOrDefault ( feature => feature . Kind == LearningModelFeatureKind . Image )
148- as ImageFeatureDescriptor ;
158+ _outputImageDescription = m_model . OutputFeatures . ToList ( ) . First ( ) . Name ;
159+ //m_model.OutputFeatures.FirstOrDefault(feature => feature.Kind == LearningModelFeatureKind.Tensor)
160+ //as ImageFeatureDescriptor;
161+ modelSetup . Add ( "OutputImageDescription" , _outputImageDescription ) ;
149162 }
150163
164+ public void debugIO ( )
165+ {
166+ uint m_inWidth , m_inHeight , m_outWidth , m_outHeight ;
167+ string m_inName , m_outName ;
168+ foreach ( var inputF in m_model . InputFeatures )
169+ {
170+ Debug . WriteLine ( $ "input | kind:{ inputF . Kind } , name:{ inputF . Name } , type:{ inputF . GetType ( ) } ") ;
171+ int i = 0 ;
172+ ImageFeatureDescriptor imgDesc = inputF as ImageFeatureDescriptor ;
173+ TensorFeatureDescriptor tfDesc = inputF as TensorFeatureDescriptor ;
174+ m_inWidth = ( uint ) ( imgDesc == null ? tfDesc . Shape [ 3 ] : imgDesc . Width ) ;
175+ m_inHeight = ( uint ) ( imgDesc == null ? tfDesc . Shape [ 2 ] : imgDesc . Height ) ;
176+ m_inName = inputF . Name ;
177+
178+ Debug . WriteLine ( $ "N: { ( imgDesc == null ? tfDesc . Shape [ 0 ] : 1 ) } , " +
179+ $ "Channel: { ( imgDesc == null ? tfDesc . Shape [ 1 ] . ToString ( ) : imgDesc . BitmapPixelFormat . ToString ( ) ) } , " +
180+ $ "Height:{ ( imgDesc == null ? tfDesc . Shape [ 2 ] : imgDesc . Height ) } , " +
181+ $ "Width: { ( imgDesc == null ? tfDesc . Shape [ 3 ] : imgDesc . Width ) } ") ;
182+ }
183+ foreach ( var outputF in m_model . OutputFeatures )
184+ {
185+ Debug . WriteLine ( $ "output | kind:{ outputF . Kind } , name:{ outputF . Name } , type:{ outputF . GetType ( ) } ") ;
186+ int i = 0 ;
187+ ImageFeatureDescriptor imgDesc = outputF as ImageFeatureDescriptor ;
188+ TensorFeatureDescriptor tfDesc = outputF as TensorFeatureDescriptor ;
189+ m_outWidth = ( uint ) ( imgDesc == null ? tfDesc . Shape [ 3 ] : imgDesc . Width ) ;
190+ m_outHeight = ( uint ) ( imgDesc == null ? tfDesc . Shape [ 2 ] : imgDesc . Height ) ;
191+ m_outName = outputF . Name ;
192+
193+ Debug . WriteLine ( $ "N: { ( imgDesc == null ? tfDesc . Shape [ 0 ] : 1 ) } , " +
194+ $ "Channel: { ( imgDesc == null ? tfDesc . Shape [ 1 ] . ToString ( ) : imgDesc . BitmapPixelFormat . ToString ( ) ) } , " +
195+ $ "Height:{ ( imgDesc == null ? tfDesc . Shape [ 2 ] : imgDesc . Height ) } , " +
196+ $ "Width: { ( imgDesc == null ? tfDesc . Shape [ 3 ] : imgDesc . Width ) } ") ;
197+ }
198+ }
151199 public void SetMediaSource ( object obj )
152200 {
153201 // TODO: Convert to a better value for the appModel object here.
0 commit comments