Skip to content

Commit 0e2ced6

Browse files
clean up redundant code
1 parent 848105e commit 0e2ced6

File tree

3 files changed

+23
-59
lines changed

3 files changed

+23
-59
lines changed

Samples/StyleTransfer/AppViewModel.cs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.ComponentModel;
44
using System.Linq;
5-
using System.Text;
65
using System.Threading.Tasks;
76
using System.Windows.Input;
87
using Windows.Media.Core;
@@ -54,9 +53,6 @@ public AppViewModel()
5453
{
5554
await DispatcherHelper.RunAsync(() =>
5655
{
57-
// Running average just to see where it levels out at
58-
//float temp = RenderFPS;
59-
//RenderFPS = ((temp * numFrames) + e) / ++numFrames;
6056
RenderFPS = e;
6157
});
6258

@@ -69,6 +65,10 @@ await DispatcherHelper.RunAsync(() =>
6965
public Windows.Media.Capture.MediaCapture _mediaCapture;
7066
private bool isPreviewing;
7167
private DisplayRequest displayRequest = new DisplayRequest();
68+
DeviceInformationCollection devices;
69+
System.Threading.Mutex Processing = new Mutex();
70+
private float _renderFPS;
71+
StyleTransferEffectCpp.StyleTransferEffectNotifier m_notifier;
7272

7373
// Style transfer effect properties
7474
private LearningModel m_model = null;
@@ -77,15 +77,8 @@ await DispatcherHelper.RunAsync(() =>
7777
private LearningModelBinding m_binding;
7878
private string m_inputImageDescription;
7979
private string m_outputImageDescription;
80-
private IMediaExtension videoEffect;
81-
private VideoEffectDefinition videoEffectDefinition;
8280
// Activatable Class ID of the video effect.
8381
private String _videoEffectID = "StyleTransferEffectCpp.StyleTransferEffect";
84-
System.Threading.Mutex Processing = new Mutex();
85-
StyleTransferEffectCpp.StyleTransferEffectNotifier m_notifier;
86-
private float _renderFPS;
87-
DeviceInformationCollection devices;
88-
8982

9083
// Image style transfer properties
9184
uint m_inWidth, m_inHeight, m_outWidth, m_outHeight;
@@ -111,13 +104,9 @@ public float RenderFPS
111104
public float CaptureFPS
112105
{
113106
get
114-
{
115-
return _captureFPS;
116-
}
107+
{ return _captureFPS; }
117108
set
118-
{
119-
_captureFPS = value; OnPropertyChanged();
120-
}
109+
{ _captureFPS = value; OnPropertyChanged(); }
121110
}
122111
private bool _useGpu;
123112
public bool UseGpu
@@ -185,7 +174,6 @@ public async Task SetMediaSource(string src)
185174
NotifyUser(true);
186175
SaveEnabled = true;
187176

188-
// Changes media source while keeping all other controls
189177
switch (_appModel.InputMedia)
190178
{
191179
case "LiveStream":
@@ -197,10 +185,9 @@ public async Task SetMediaSource(string src)
197185
case "FilePick":
198186
await StartFilePick();
199187
break;
200-
case "Inking":
188+
default:
201189
break;
202190
}
203-
204191
return;
205192
}
206193

@@ -220,7 +207,7 @@ public async Task SetModelSource()
220207
case "FilePick":
221208
await ChangeImage();
222209
break;
223-
case "Inking":
210+
default:
224211
break;
225212
}
226213
}
@@ -262,9 +249,6 @@ public async Task StartFilePick()
262249
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{_DefaultImageFileName}"));
263250
_appModel.InputFrame = await ImageHelper.LoadVideoFrameFromStorageFileAsync(file);
264251
}
265-
else
266-
{
267-
}
268252
await ChangeImage();
269253
}
270254
catch (Exception ex)
@@ -283,7 +267,6 @@ public async Task ChangeImage()
283267
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{_DefaultImageFileName}"));
284268
_appModel.InputFrame = await ImageHelper.LoadVideoFrameFromStorageFileAsync(file);
285269
}
286-
287270
await EvaluateVideoFrameAsync();
288271
}
289272

@@ -395,11 +378,11 @@ public async Task ChangeLiveStream()
395378
_appModel.OutputCaptureElement = capture;
396379

397380
var modelPath = Path.GetFullPath($"./Assets/{_appModel.ModelSource}.onnx");
398-
videoEffectDefinition = new VideoEffectDefinition(_videoEffectID, new PropertySet() {
381+
VideoEffectDefinition videoEffectDefinition = new VideoEffectDefinition(_videoEffectID, new PropertySet() {
399382
{"ModelName", modelPath },
400383
{"UseGpu", UseGpu },
401384
{ "Notifier", m_notifier} });
402-
videoEffect = await _mediaCapture.AddVideoEffectAsync(videoEffectDefinition, MediaStreamType.VideoPreview);
385+
IMediaExtension videoEffect = await _mediaCapture.AddVideoEffectAsync(videoEffectDefinition, MediaStreamType.VideoPreview);
403386

404387
var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;
405388
CaptureFPS = props.FrameRate.Numerator / props.FrameRate.Denominator;

Samples/StyleTransfer/VideoEffect/StyleTransferEffectCpp/StyleTransferEffect.cpp

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ using namespace concurrency;
1212
namespace winrt::StyleTransferEffectCpp::implementation
1313
{
1414
StyleTransferEffect::StyleTransferEffect() :
15-
cachedOutput(nullptr),
16-
cachedOutputCopy(VideoFrame(Windows::Graphics::Imaging::BitmapPixelFormat::Bgra8, 640, 360)),
17-
outputTransformed(VideoFrame(Windows::Graphics::Imaging::BitmapPixelFormat::Bgra8, 720, 720)),
18-
Session(nullptr),
19-
Binding(nullptr)
15+
Session(nullptr)
2016
{
2117
for (int i = 0; i < swapChainEntryCount; i++)
2218
{
@@ -25,38 +21,37 @@ namespace winrt::StyleTransferEffectCpp::implementation
2521
}
2622

2723
IVectorView<VideoEncodingProperties> StyleTransferEffect::SupportedEncodingProperties() {
28-
VideoEncodingProperties encodingProperties = VideoEncodingProperties();
29-
encodingProperties.Subtype(L"ARGB32");
30-
return single_threaded_vector(std::vector<VideoEncodingProperties>{encodingProperties}).GetView();
24+
VideoEncodingProperties props = VideoEncodingProperties();
25+
props.Subtype(L"ARGB32");
26+
return single_threaded_vector(std::vector<VideoEncodingProperties>{props}).GetView();
3127
}
3228

3329
bool StyleTransferEffect::TimeIndependent() { return true; }
3430
MediaMemoryTypes StyleTransferEffect::SupportedMemoryTypes() { return MediaMemoryTypes::GpuAndCpu; }
3531
bool StyleTransferEffect::IsReadOnly() { return false; }
3632
void StyleTransferEffect::DiscardQueuedFrames() {}
3733

38-
void StyleTransferEffect::Close(MediaEffectClosedReason m) {
34+
void StyleTransferEffect::Close(MediaEffectClosedReason) {
3935
OutputDebugString(L"Close Begin | ");
4036
std::lock_guard<mutex> guard{ Processing };
41-
OutputDebugString(L"Close\n");
42-
if (Binding != nullptr) Binding.Clear();
43-
if (Session != nullptr) Session.Close();
4437
// Make sure evalAsyncs are done before clearing resources
4538
for (int i = 0; i < swapChainEntryCount; i++) {
4639
if (bindings[i]->activetask != nullptr &&
4740
bindings[i]->binding != nullptr)
4841
{
42+
std::wostringstream ss;
43+
ss << i;
44+
OutputDebugString(ss.str().c_str());
4945
bindings[i]->activetask.get();
5046
bindings[i]->binding.Clear();
5147
}
5248
}
49+
if (Session != nullptr) Session.Close();
50+
OutputDebugString(L"Close\n");
5351
}
5452

5553
void StyleTransferEffect::SubmitEval(VideoFrame input, VideoFrame output) {
5654
auto currentBinding = bindings[0].get();
57-
//VideoFrame outputTransformed = cachedOutput;
58-
// Different way of waiting for a swapchain index to finish?
59-
// Or would it be just setting the output to be a cached frame?
6055
if (currentBinding->activetask == nullptr
6156
|| currentBinding->activetask.Status() != Windows::Foundation::AsyncStatus::Started)
6257
{
@@ -67,16 +62,15 @@ namespace winrt::StyleTransferEffectCpp::implementation
6762
OutputDebugString(ss.str().c_str());
6863
OutputDebugString(L" | ");
6964

70-
// bind the input and the output buffers by name
7165
currentBinding->binding.Bind(InputImageDescription, input);
7266
// submit an eval and wait for it to finish submitting work
7367
{
7468
std::lock_guard<mutex> guard{ Processing };
7569
std::rotate(bindings.begin(), bindings.begin() + 1, bindings.end());
7670
finishedIdx = (finishedIdx - 1 + swapChainEntryCount) % swapChainEntryCount;
71+
currentBinding->activetask = Session.EvaluateAsync(currentBinding->binding, ss.str().c_str());
7772
}
78-
currentBinding->activetask = Session.EvaluateAsync(currentBinding->binding, ss.str().c_str());
79-
currentBinding->activetask.Completed([&, currentBinding, now](auto&& asyncInfo, winrt::Windows::Foundation::AsyncStatus const args) {
73+
currentBinding->activetask.Completed([&, currentBinding, now](auto&& asyncInfo, winrt::Windows::Foundation::AsyncStatus const) {
8074
OutputDebugString(L"PF Eval completed |");
8175
VideoFrame evalOutput = asyncInfo.GetResults().Outputs().Lookup(OutputImageDescription).try_as<VideoFrame>();
8276
int bindingIdx;
@@ -103,7 +97,6 @@ namespace winrt::StyleTransferEffectCpp::implementation
10397
if (bindings[finishedIdx]->outputCache != nullptr) {
10498
std::wostringstream ss;
10599
ss << finishedIdx;
106-
//std::lock_guard<mutex> guard{ Copy };
107100
OutputDebugString(L"\nStart CopyAsync ");
108101
OutputDebugString(ss.str().c_str());
109102
bindings[finishedIdx]->outputCache.CopyToAsync(output).get();
@@ -114,7 +107,6 @@ namespace winrt::StyleTransferEffectCpp::implementation
114107

115108
void StyleTransferEffect::ProcessFrame(ProcessVideoFrameContext context) {
116109
OutputDebugString(L"PF Start | ");
117-
auto now = std::chrono::high_resolution_clock::now();
118110
VideoFrame inputFrame = context.InputFrame();
119111
VideoFrame outputFrame = context.OutputFrame();
120112

@@ -145,7 +137,6 @@ namespace winrt::StyleTransferEffectCpp::implementation
145137
LearningModel m_model = LearningModel::LoadFromFilePath(modelName);
146138
LearningModelDeviceKind m_device = useGpu ? LearningModelDeviceKind::DirectX : LearningModelDeviceKind::Cpu;
147139
Session = LearningModelSession{ m_model, LearningModelDevice(m_device) };
148-
Binding = LearningModelBinding{ Session };
149140

150141
InputImageDescription = L"inputImage";
151142
OutputImageDescription = L"outputImage";

Samples/StyleTransfer/VideoEffect/StyleTransferEffectCpp/StyleTransferEffect.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ namespace winrt::StyleTransferEffectCpp::implementation
2424
struct StyleTransferEffect : StyleTransferEffectT<StyleTransferEffect>
2525
{
2626
StyleTransferEffect();
27-
VideoFrame outputTransformed;
2827
IVectorView<VideoEncodingProperties> SupportedEncodingProperties();
2928
bool TimeIndependent();
3029
MediaMemoryTypes SupportedMemoryTypes();
@@ -39,20 +38,11 @@ namespace winrt::StyleTransferEffectCpp::implementation
3938
void SubmitEval(VideoFrame, VideoFrame);
4039

4140
private:
42-
LearningModelSession Session;
43-
LearningModelBinding Binding;
44-
4541
VideoEncodingProperties encodingProperties;
4642
std::mutex Processing;
47-
std::mutex Copy;
48-
4943
StyleTransferEffectNotifier Notifier;
50-
std::chrono::time_point<std::chrono::steady_clock> m_StartTime;
51-
bool firstProcessFrameCall = true;
52-
Windows::Graphics::Imaging::BitmapBounds copyBounds;
44+
LearningModelSession Session;
5345
Windows::Foundation::IAsyncOperation<LearningModelEvaluationResult> evalStatus;
54-
VideoFrame cachedOutput;
55-
VideoFrame cachedOutputCopy;
5646
hstring InputImageDescription;
5747
hstring OutputImageDescription;
5848
int swapChainIndex = 0;

0 commit comments

Comments
 (0)