Skip to content

Commit 93510f2

Browse files
rewrite effect to handle all destruction itself
1 parent 7ca9951 commit 93510f2

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

Samples/StyleTransfer/AppViewModel.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using Windows.Media.MediaProperties;
2424
using Windows.Storage.Pickers;
2525
using Windows.UI.Xaml.Controls;
26+
using System.IO;
2627

2728
namespace StyleTransfer
2829
{
@@ -316,13 +317,13 @@ public async Task ChangeLiveStream()
316317
capture.Source = _mediaCapture;
317318
_appModel.OutputCaptureElement = capture;
318319

320+
// var modelPath = new Uri($"./Assets/{_appModel.ModelSource}.onnx");
321+
var modelPath = Path.GetFullPath($"./Assets/{_appModel.ModelSource}.onnx");
319322
videoEffectDefinition = new VideoEffectDefinition(_videoEffectID);
320323
videoEffect = await _mediaCapture.AddVideoEffectAsync(videoEffectDefinition, MediaStreamType.VideoPreview);
321324
videoEffect.SetProperties(new PropertySet() {
322-
{ "Session", m_session},
323-
{ "Binding", m_binding },
324-
{ "InputImageDescription", m_inputImageDescription },
325-
{ "OutputImageDescription", m_outputImageDescription } });
325+
{"ModelName", modelPath },
326+
{"UseGPU", _appModel.UseGPU }});
326327

327328
await _mediaCapture.StartPreviewAsync();
328329
}
@@ -416,7 +417,6 @@ private void CleanupCameraAsync()
416417
if (_appModel.OutputCaptureElement != null)
417418
{
418419
_appModel.OutputCaptureElement = null;
419-
420420
}
421421
if (videoEffect != null)
422422
{

Samples/StyleTransfer/VideoEffect/StyleTransferEffectCpp/StyleTransferEffect.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include "pch.h"
22
#include "StyleTransferEffect.h"
33
#include "StyleTransferEffect.g.cpp"
4-
4+
using namespace std;
5+
using namespace winrt::Windows::Storage;
6+
using namespace winrt::Windows::Storage::Streams;
57

68
namespace winrt::StyleTransferEffectCpp::implementation
79
{
@@ -56,22 +58,25 @@ namespace winrt::StyleTransferEffectCpp::implementation
5658

5759
void StyleTransferEffect::SetProperties(IPropertySet config) {
5860
this->configuration = config;
59-
60-
IInspectable val = configuration.TryLookup(L"Session");
61-
if (val) {
62-
Session = val.try_as<LearningModelSession>();
63-
}
64-
val = configuration.TryLookup(L"Binding");
65-
if (val) {
66-
Binding = val.try_as<LearningModelBinding>();
67-
}
68-
val = configuration.TryLookup(L"InputImageDescription");
69-
if (val) {
70-
InputImageDescription = unbox_value<hstring>(val);
71-
}
72-
val = configuration.TryLookup(L"OutputImageDescription");
73-
if (val) {
74-
OutputImageDescription = unbox_value<hstring>(val);
61+
hstring modelName;
62+
IInspectable val = config.TryLookup(L"ModelName");
63+
if (!val) {
64+
return;
7565
}
66+
modelName = unbox_value<hstring>(val);
67+
val = configuration.TryLookup(L"UseGPU");
68+
bool useGpu = unbox_value<bool>(val);
69+
OutputDebugString(modelName.c_str());
70+
//std::wstring fullModelName(L"ms-appx:///Assets/");
71+
//fullModelName += modelName + L".onnx";
72+
//OutputDebugString(fullModelName.c_str());
73+
LearningModel m_model = LearningModel::LoadFromFilePath(modelName);
74+
75+
LearningModelDeviceKind m_device = useGpu ? LearningModelDeviceKind::DirectX : LearningModelDeviceKind::Cpu;
76+
Session = LearningModelSession{ m_model, LearningModelDevice(m_device) };
77+
Binding = LearningModelBinding{ Session };
78+
79+
InputImageDescription = L"inputImage";
80+
OutputImageDescription = L"outputImage";
7681
}
7782
}

0 commit comments

Comments
 (0)