Skip to content

Commit 3bb165a

Browse files
small fixes to styletansfercpp
1 parent 2de4464 commit 3bb165a

File tree

5 files changed

+74
-56
lines changed

5 files changed

+74
-56
lines changed

Samples/StyleTransfer/AppModel.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class AppModel : INotifyPropertyChanged
2020
{
2121
public AppModel()
2222
{
23-
this._useGPU = false;
2423
this._modelSource = "candy";
2524
this._selectedCameraIndex = 0;
2625
this._outputCaptureElement = new CaptureElement();
@@ -51,16 +50,7 @@ public string InputMedia
5150
}
5251
}
5352

54-
private bool _useGPU;
55-
public bool UseGPU
56-
{
57-
get { return _useGPU; }
58-
set
59-
{
60-
_useGPU = value;
61-
OnPropertyChanged();
62-
}
63-
}
53+
6454

6555
// Input VideoFrame when processing images
6656
private VideoFrame _inputFrame;

Samples/StyleTransfer/AppViewModel.cs

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
using System.Threading;
3131
using StyleTransferEffectCpp;
3232
using System.Runtime.InteropServices.WindowsRuntime;
33+
using Windows.Devices.Enumeration;
3334

3435
namespace 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

Samples/StyleTransfer/MainPage.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@
185185
HorizontalAlignment="Center"
186186
VerticalAlignment="Top"
187187
HorizontalContentAlignment="Center"
188-
IsOn="{Binding CurrentApp.UseGPU, Mode=TwoWay}"
188+
IsOn="{Binding UseGpu, Mode=TwoWay}"
189189
OffContent="CPU"
190190
OnContent="GPU" />
191191
</Grid>

Samples/StyleTransfer/Package.appxmanifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<Identity
1010
Name="7fafd263-abdf-4f88-87a1-ffb0922489d1"
1111
Publisher="CN=t-limay"
12-
Version="1.0.8.0" />
12+
Version="1.0.9.0" />
1313

1414
<mp:PhoneIdentity PhoneProductId="7fafd263-abdf-4f88-87a1-ffb0922489d1" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>
1515

Samples/StyleTransfer/VideoEffect/StyleTransferEffectCpp/StyleTransferEffect.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace winrt::StyleTransferEffectCpp::implementation
1616
IVectorView<VideoEncodingProperties> StyleTransferEffect::SupportedEncodingProperties() {
1717
VideoEncodingProperties encodingProperties = VideoEncodingProperties();
1818
encodingProperties.Subtype(L"ARGB32");
19-
return single_threaded_vector(std::move(std::vector<VideoEncodingProperties>{encodingProperties})).GetView();
19+
return single_threaded_vector(std::vector<VideoEncodingProperties>{encodingProperties}).GetView();
2020
}
2121

2222
bool StyleTransferEffect::TimeIndependent() { return true; }
@@ -35,6 +35,7 @@ namespace winrt::StyleTransferEffectCpp::implementation
3535

3636

3737
void StyleTransferEffect::ProcessFrame(ProcessVideoFrameContext context) {
38+
std::lock_guard<mutex> guard{ Processing };
3839
auto now = std::chrono::high_resolution_clock::now();
3940
std::chrono::milliseconds timePassed;
4041
// If the first time calling ProcessFrame, just start the timer
@@ -54,7 +55,6 @@ namespace winrt::StyleTransferEffectCpp::implementation
5455
VideoFrame inputFrame = context.InputFrame();
5556
VideoFrame outputFrame = context.OutputFrame();
5657

57-
std::lock_guard<mutex> guard{ Processing };
5858
OutputDebugString(L"PF Locked | ");
5959
Binding.Bind(InputImageDescription, inputFrame);
6060
Binding.Bind(OutputImageDescription, outputTransformed);
@@ -73,15 +73,17 @@ namespace winrt::StyleTransferEffectCpp::implementation
7373
void StyleTransferEffect::SetProperties(IPropertySet config) {
7474
this->configuration = config;
7575
hstring modelName;
76+
bool useGpu;
77+
7678
IInspectable val = config.TryLookup(L"ModelName");
77-
if (!val) {
78-
return;
79-
}
80-
modelName = unbox_value<hstring>(val);
81-
val = configuration.TryLookup(L"UseGPU");
82-
bool useGpu = unbox_value<bool>(val);
79+
if (val) modelName = unbox_value<hstring>(val);
80+
else winrt::throw_hresult(E_FAIL);
81+
val = configuration.TryLookup(L"UseGpu");
82+
if (val) useGpu = unbox_value<bool>(val);
83+
else winrt::throw_hresult(E_FAIL);
8384
val = configuration.TryLookup(L"Notifier");
84-
Notifier = val.try_as<StyleTransferEffectNotifier>();
85+
if (val) Notifier = val.try_as<StyleTransferEffectNotifier>();
86+
else winrt::throw_hresult(E_FAIL);
8587

8688
LearningModel m_model = LearningModel::LoadFromFilePath(modelName);
8789
LearningModelDeviceKind m_device = useGpu ? LearningModelDeviceKind::DirectX : LearningModelDeviceKind::Cpu;

0 commit comments

Comments
 (0)