3030using System . Threading ;
3131using StyleTransferEffectCpp ;
3232using System . Runtime . InteropServices . WindowsRuntime ;
33+ using Windows . Devices . Enumeration ;
3334
3435namespace StyleTransfer
3536{
@@ -50,7 +51,8 @@ public AppViewModel()
5051
5152 m_notifier = new StyleTransferEffectNotifier ( ) ;
5253 m_notifier . FrameRateUpdated += async ( _ , e ) => await DispatcherHelper . RunAsync ( ( ) => RenderFPS = e ) ;
53-
54+ _useGpu = true ;
55+ isPreviewing = false ;
5456 }
5557
5658 // Media capture properties
@@ -74,6 +76,8 @@ public AppViewModel()
7476 System . Threading . Mutex Processing = new Mutex ( ) ;
7577 StyleTransferEffectCpp . StyleTransferEffectNotifier m_notifier ;
7678 private float _renderFPS ;
79+ DeviceInformationCollection devices ;
80+
7781
7882 // Image style transfer properties
7983 uint m_inWidth , m_inHeight , m_outWidth , m_outHeight ;
@@ -108,6 +112,20 @@ public float CaptureFPS
108112 _captureFPS = value ; OnPropertyChanged ( ) ;
109113 }
110114 }
115+ private bool _useGpu ;
116+ public bool UseGpu
117+ {
118+ get { return _useGpu ; }
119+ set
120+ {
121+ _useGpu = value ;
122+ if ( _appModel . InputMedia == "LiveStream" )
123+ {
124+ SetMediaSourceCommand . Execute ( "LiveStream" ) ;
125+ }
126+ OnPropertyChanged ( ) ;
127+ }
128+ }
111129 private SoftwareBitmapSource _inputSoftwareBitmapSource ;
112130 public SoftwareBitmapSource InputSoftwareBitmapSource
113131 {
@@ -155,7 +173,6 @@ public async Task SetMediaSource(string src)
155173 {
156174 _appModel . InputMedia = src ;
157175
158- CleanupInputImage ( ) ;
159176 NotifyUser ( true ) ;
160177 SaveEnabled = true ;
161178
@@ -166,10 +183,11 @@ public async Task SetMediaSource(string src)
166183 await StartLiveStream ( ) ;
167184 break ;
168185 case "AcquireImage" :
186+ CleanupInputImage ( ) ;
169187 await StartAcquireImage ( ) ;
170188 break ;
171189 case "FilePick" :
172-
190+ CleanupInputImage ( ) ;
173191 await StartFilePick ( ) ;
174192 break ;
175193 case "Inking" :
@@ -297,29 +315,36 @@ public async Task StartLiveStream()
297315 Debug . WriteLine ( "StartLiveStream" ) ;
298316 await CleanupCameraAsync ( ) ;
299317
300- try
301- {
302- // Find the sources
303- var allGroups = await MediaFrameSourceGroup . FindAllAsync ( ) ;
304- _mediaFrameSourceGroupList = allGroups . Where ( group => group . SourceInfos . Any ( sourceInfo => sourceInfo . SourceKind == MediaFrameSourceKind . Color
305- && ( sourceInfo . MediaStreamType == MediaStreamType . VideoPreview
306- || sourceInfo . MediaStreamType == MediaStreamType . VideoRecord ) ) ) . ToList ( ) ;
307- }
308- catch ( Exception ex )
318+ if ( devices == null )
309319 {
310- Debug . WriteLine ( ex . Message ) ;
311- _mediaFrameSourceGroupList = null ;
320+ try
321+ {
322+ devices = await DeviceInformation . FindAllAsync ( DeviceClass . VideoCapture ) ;
323+ }
324+ catch ( Exception ex )
325+ {
326+ Debug . WriteLine ( ex . Message ) ;
327+ devices = null ;
328+ }
329+ if ( ( devices == null ) || ( devices . Count == 0 ) )
330+ {
331+ // No camera sources found
332+ Debug . WriteLine ( "No cameras found" ) ;
333+ NotifyUser ( false , "No cameras found." ) ;
334+ return ;
335+ }
336+ _appModel . CameraNamesList = devices . Select ( device => device . Name ) ;
337+ return ;
312338 }
313-
314- if ( ( _mediaFrameSourceGroupList == null ) || ( _mediaFrameSourceGroupList . Count == 0 ) )
339+ // If just above 0 you fool
340+ if ( _appModel . SelectedCameraIndex >= 0 )
315341 {
316- // No camera sources found
317- Debug . WriteLine ( "No Camera found" ) ;
318- NotifyUser ( false , "No Camera found." ) ;
342+ Debug . WriteLine ( "StartLive: already 0" ) ;
343+ await ChangeLiveStream ( ) ;
319344 return ;
320345 }
321-
322- _appModel . CameraNamesList = _mediaFrameSourceGroupList . Select ( group => group . DisplayName ) ;
346+ // If already have the list, set to the default
347+ _appModel . SelectedCameraIndex = 0 ;
323348 }
324349
325350 public async Task ChangeLiveStream ( )
@@ -330,25 +355,28 @@ public async Task ChangeLiveStream()
330355 SaveEnabled = false ;
331356
332357 // If webcam hasn't been initialized, bail.
333- if ( _mediaFrameSourceGroupList == null ) return ;
358+ if ( ( devices == null ) || ( devices . Count == 0 ) )
359+ return ;
334360
335361 try
336362 {
337363 // Check that SCI hasn't < 0
364+ // Probably -1 when doesn't find camera, or when list gone?
338365 if ( _appModel . SelectedCameraIndex < 0 )
339366 {
367+ Debug . WriteLine ( "selectedCamera < 0" ) ;
368+ NotifyUser ( false , "Invalid Camera selected, using default" ) ;
340369 _appModel . SelectedCameraIndex = 0 ;
341370 return ;
342371 }
343- _selectedMediaFrameSourceGroup = _mediaFrameSourceGroupList [ _appModel . SelectedCameraIndex ] ;
344-
345- MediaCapture . FindAllVideoProfiles ( _selectedMediaFrameSourceGroup . Id ) ;
372+ var device = devices . ToList ( ) . ElementAt ( _appModel . SelectedCameraIndex ) ;
346373 // Create MediaCapture and its settings
347374 var settings = new MediaCaptureInitializationSettings
348375 {
376+ VideoDeviceId = device . Id ,
349377 SourceGroup = _selectedMediaFrameSourceGroup ,
350378 PhotoCaptureSource = PhotoCaptureSource . Auto ,
351- MemoryPreference = _appModel . UseGPU ? MediaCaptureMemoryPreference . Auto : MediaCaptureMemoryPreference . Cpu ,
379+ MemoryPreference = UseGpu ? MediaCaptureMemoryPreference . Auto : MediaCaptureMemoryPreference . Cpu ,
352380 StreamingCaptureMode = StreamingCaptureMode . Video ,
353381 MediaCategory = MediaCategory . Communications ,
354382 } ;
@@ -361,12 +389,11 @@ public async Task ChangeLiveStream()
361389 _appModel . OutputCaptureElement = capture ;
362390
363391 var modelPath = Path . GetFullPath ( $ "./Assets/{ _appModel . ModelSource } .onnx") ;
364- videoEffectDefinition = new VideoEffectDefinition ( _videoEffectID ) ;
365- videoEffect = await _mediaCapture . AddVideoEffectAsync ( videoEffectDefinition , MediaStreamType . VideoPreview ) ;
366- videoEffect . SetProperties ( new PropertySet ( ) {
392+ videoEffectDefinition = new VideoEffectDefinition ( _videoEffectID , new PropertySet ( ) {
367393 { "ModelName" , modelPath } ,
368- { "UseGPU" , _appModel . UseGPU } ,
394+ { "UseGPU" , UseGpu } ,
369395 { "Notifier" , m_notifier } } ) ;
396+ videoEffect = await _mediaCapture . AddVideoEffectAsync ( videoEffectDefinition , MediaStreamType . VideoPreview ) ;
370397
371398 var props = _mediaCapture . VideoDeviceController . GetMediaStreamProperties ( MediaStreamType . VideoPreview ) as VideoEncodingProperties ;
372399 CaptureFPS = props . FrameRate . Numerator / props . FrameRate . Denominator ;
@@ -398,7 +425,7 @@ private async Task LoadModelAsync()
398425 StorageFile modelFile = await StorageFile . GetFileFromApplicationUriAsync ( new Uri ( $ "ms-appx:///Assets/{ _appModel . ModelSource } .onnx") ) ;
399426 m_model = await LearningModel . LoadFromStorageFileAsync ( modelFile ) ;
400427
401- m_inferenceDeviceSelected = _appModel . UseGPU ? LearningModelDeviceKind . DirectX : LearningModelDeviceKind . Cpu ;
428+ m_inferenceDeviceSelected = UseGpu ? LearningModelDeviceKind . DirectX : LearningModelDeviceKind . Cpu ;
402429 m_session = new LearningModelSession ( m_model , new LearningModelDevice ( m_inferenceDeviceSelected ) ) ;
403430 m_binding = new LearningModelBinding ( m_session ) ;
404431
@@ -461,22 +488,21 @@ private async Task CleanupCameraAsync()
461488 await _mediaCapture . StopPreviewAsync ( ) ;
462489 }
463490 catch ( Exception e ) { Debug . WriteLine ( "CleanupCamera: " + e . Message ) ; }
464-
465- isPreviewing = false ;
466491 }
467492 await DispatcherHelper . RunAsync ( ( ) =>
468493 {
469494 CaptureElement cap = new CaptureElement ( ) ;
470495 cap . Source = null ;
471496 _appModel . OutputCaptureElement = cap ;
472- if ( displayRequest != null )
497+ if ( isPreviewing )
473498 {
474499 displayRequest . RequestRelease ( ) ;
475500 }
476501
477502 MediaCapture m = _mediaCapture ;
478503 _mediaCapture = null ;
479504 m . Dispose ( ) ;
505+ isPreviewing = false ;
480506 } ) ;
481507 }
482508
0 commit comments