Skip to content

Commit 1234ddf

Browse files
have a separate capture element, but need a separate thread/ properly async so that can run at same time as style transfer
1 parent d02f16e commit 1234ddf

File tree

4 files changed

+54
-12
lines changed

4 files changed

+54
-12
lines changed

Samples/StyleTransfer/AppModel.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Text;
99
using System.Threading.Tasks;
1010
using Windows.Media;
11+
using Windows.Media.Capture;
1112
using Windows.Media.Core;
1213
using Windows.Media.Playback;
1314

@@ -86,6 +87,13 @@ public int SelectedCameraIndex
8687
set { _selectedCameraIndex = value; OnPropertyChanged(); }
8788
}
8889

90+
private MediaCapture _inputMediaCapture;
91+
public MediaCapture InputMediaCapture
92+
{
93+
get { return _inputMediaCapture; }
94+
set { _inputMediaCapture = value; OnPropertyChanged(); }
95+
}
96+
8997
public event PropertyChangedEventHandler PropertyChanged;
9098
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
9199
{

Samples/StyleTransfer/AppViewModel.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ public AppViewModel()
3838
private IDictionary<string, object> modelSetup;
3939
private LearningModel m_model = null;
4040
private LearningModelDeviceKind m_inferenceDeviceSelected = LearningModelDeviceKind.Default;
41-
private LearningModelDevice m_device;
4241
private LearningModelSession m_session;
43-
private LearningModelBinding m_binding;
44-
string m_outName, m_inName;
4542
string _inputImageDescription;
4643
string _outputImageDescription;
4744

@@ -234,7 +231,6 @@ private void StartPreview()
234231
_appModel.OutputMediaSource = MediaSource.CreateFromMediaFrameSource(_selectedMediaFrameSource);
235232
_appModel.InputMediaSource = MediaSource.CreateFromMediaFrameSource(_selectedMediaFrameSource);
236233

237-
238234
//UIInputMediaPlayerElement.SetMediaPlayer(_mediaPlayer);
239235
//UIOutputMediaPlayerElement.SetMediaPlayer(_mediaPlayer);
240236

Samples/StyleTransfer/MainPage.xaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131

132132
<Button
133133
Name="UIButtonLiveStream"
134+
Click="UIButtonLiveStream_Click"
134135
Command="{Binding LiveStreamCommand}"
135136
CommandParameter="LiveStream"
136137
ToolTipService.ToolTip="Camera preview">
@@ -215,7 +216,7 @@
215216
</StackPanel>
216217
</Viewbox>
217218

218-
<!-- Camera preview -->
219+
<!-- Camera preview
219220
<MediaPlayerElement
220221
Name="UIInputMediaPlayerElement"
221222
Grid.Column="0"
@@ -226,7 +227,12 @@
226227
Canvas.ZIndex="-1"
227228
Source="{Binding CurrentApp.InputMediaSource}"
228229
Stretch="Uniform"
229-
Visibility="Visible" />
230+
Visibility="Visible" />-->
231+
232+
<CaptureElement
233+
Name="PreviewControl"
234+
Source="{Binding CurrentApp.InputMediaCapture}"
235+
Stretch="Uniform" />
230236

231237
<!-- Input image preview -->
232238
<Image

Samples/StyleTransfer/MainPage.xaml.cs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
using Windows.Storage;
2121
using Windows.Media.Capture.Frames;
2222
using System.Diagnostics;
23+
using Windows.System.Display;
24+
using Windows.Graphics.Display;
2325

2426
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
2527

@@ -39,12 +41,6 @@ public sealed partial class MainPage : Page
3941
};
4042
private AppViewModel _viewModel;
4143

42-
private MediaPlayer _mediaPlayer;
43-
Windows.Media.Capture.MediaCapture _mediaCapture;
44-
private List<MediaFrameSourceGroup> _mediaFrameSourceGroupList;
45-
private MediaFrameSourceGroup _selectedMediaFrameSourceGroup;
46-
private MediaFrameSource _selectedMediaFrameSource;
47-
4844
public MainPage()
4945
{
5046
this.InitializeComponent();
@@ -53,6 +49,42 @@ public MainPage()
5349

5450

5551
}
52+
Windows.Media.Capture.MediaCapture _inputMediaCapture;
53+
bool isPreviewing;
54+
DisplayRequest displayRequest = new DisplayRequest();
55+
private async void UIButtonLiveStream_Click(object sender, RoutedEventArgs e)
56+
{
57+
// Need to have separate thread ? for style transfer section
58+
try
59+
{
60+
61+
_inputMediaCapture = new MediaCapture();
62+
await _inputMediaCapture.InitializeAsync();
63+
64+
displayRequest.RequestActive();
65+
DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
66+
}
67+
catch (UnauthorizedAccessException)
68+
{
69+
// This will be thrown if the user denied access to the camera in privacy settings
70+
Debug.WriteLine("The app was denied access to the camera");
71+
return;
72+
}
73+
74+
try
75+
{
76+
PreviewControl.Source = _inputMediaCapture;
77+
await _inputMediaCapture.StartPreviewAsync();
78+
isPreviewing = true;
79+
}
80+
catch (System.IO.FileLoadException fle)
81+
{
82+
Debug.WriteLine("Failed to load input media capture", fle);
83+
//_inputMediaCapture.CaptureDeviceExclusiveControlStatusChanged += _mediaCapture_CaptureDeviceExclusiveControlStatusChanged;
84+
}
85+
86+
return;
87+
}
5688

5789

5890
}

0 commit comments

Comments
 (0)