Skip to content

Commit 3adf348

Browse files
trying to optimize with copying to rectangle of model inputframe
1 parent c4202b5 commit 3adf348

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

Samples/StyleTransfer/AppViewModel.cs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,17 @@ public AppViewModel()
5050
NotifyUser(true);
5151

5252
m_notifier = new StyleTransferEffectNotifier();
53-
m_notifier.FrameRateUpdated += async (_, e) => await DispatcherHelper.RunAsync(() => RenderFPS = e);
54-
_useGpu = true;
53+
m_notifier.FrameRateUpdated += async (_, e) =>
54+
{
55+
await DispatcherHelper.RunAsync(() =>
56+
{
57+
// Running average just to see where it levels out at
58+
float temp = RenderFPS;
59+
RenderFPS = ((temp * numFrames) + e) / ++numFrames;
60+
});
61+
62+
};
63+
_useGpu = false;
5564
isPreviewing = false;
5665
}
5766

@@ -61,6 +70,7 @@ public AppViewModel()
6170
private MediaFrameSourceGroup _selectedMediaFrameSourceGroup;
6271
private bool isPreviewing;
6372
private DisplayRequest displayRequest = new DisplayRequest();
73+
private int numFrames = 0;
6474

6575
// Style transfer effect properties
6676
private LearningModel m_model = null;
@@ -172,6 +182,8 @@ public async void SaveOutput()
172182
public async Task SetMediaSource(string src)
173183
{
174184
_appModel.InputMedia = src;
185+
await CleanupCameraAsync();
186+
CleanupInputImage();
175187

176188
NotifyUser(true);
177189
SaveEnabled = true;
@@ -183,11 +195,9 @@ public async Task SetMediaSource(string src)
183195
await StartLiveStream();
184196
break;
185197
case "AcquireImage":
186-
CleanupInputImage();
187198
await StartAcquireImage();
188199
break;
189200
case "FilePick":
190-
CleanupInputImage();
191201
await StartFilePick();
192202
break;
193203
case "Inking":
@@ -519,14 +529,22 @@ private void CleanupInputImage()
519529
{
520530
try
521531
{
522-
InputSoftwareBitmapSource?.Dispose();
523-
OutputSoftwareBitmapSource?.Dispose();
524-
525-
InputSoftwareBitmapSource = new SoftwareBitmapSource();
526-
OutputSoftwareBitmapSource = new SoftwareBitmapSource();
527-
528-
_appModel.OutputFrame?.Dispose();
532+
if (InputSoftwareBitmapSource != null)
533+
{
534+
InputSoftwareBitmapSource.Dispose();
535+
InputSoftwareBitmapSource = new SoftwareBitmapSource();
536+
}
537+
if (OutputSoftwareBitmapSource != null)
538+
{
539+
OutputSoftwareBitmapSource.Dispose();
540+
OutputSoftwareBitmapSource = new SoftwareBitmapSource();
541+
}
542+
if (_appModel.OutputFrame != null)
543+
{
544+
_appModel.OutputFrame.Dispose();
545+
}
529546
_appModel.OutputFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)m_outWidth, (int)m_outHeight);
547+
530548
}
531549
catch (Exception e)
532550
{

Samples/StyleTransfer/VideoEffect/StyleTransferEffectCpp/StyleTransferEffect.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ using namespace winrt::Windows::Storage::Streams;
77

88
namespace winrt::StyleTransferEffectCpp::implementation
99
{
10-
StyleTransferEffect::StyleTransferEffect() : outputTransformed(VideoFrame(Windows::Graphics::Imaging::BitmapPixelFormat::Bgra8, 720, 720)),
10+
StyleTransferEffect::StyleTransferEffect() :
11+
outputTransformed(VideoFrame(Windows::Graphics::Imaging::BitmapPixelFormat::Bgra8, 720, 720)),
12+
inputTransformed(VideoFrame(Windows::Graphics::Imaging::BitmapPixelFormat::Bgra8, 720, 720)),
13+
copyBounds(Windows::Graphics::Imaging::BitmapBounds{ 0, 0, 640, 360 }),
1114
Session(nullptr),
1215
Binding(nullptr)
1316
{
@@ -38,29 +41,32 @@ namespace winrt::StyleTransferEffectCpp::implementation
3841
std::lock_guard<mutex> guard{ Processing };
3942
auto now = std::chrono::high_resolution_clock::now();
4043
std::chrono::milliseconds timePassed;
41-
// If the first time calling ProcessFrame, just start the timer
44+
// If the first time calling ProcessFrame, just start the timer
4245
if (firstProcessFrameCall) {
4346
m_StartTime = now;
4447
firstProcessFrameCall = false;
4548
}
46-
// On the second and any proceding process,
49+
// On the second and any proceding process,
4750
else {
4851
timePassed = std::chrono::duration_cast<std::chrono::milliseconds>(now - m_StartTime);
4952
m_StartTime = now;
50-
Notifier.SetFrameRate(1000.f / timePassed.count()); // Convert to FPS: milli to seconds, invert
53+
Notifier.SetFrameRate(1000.f / timePassed.count()); // Convert to FPS: milli to seconds, invert
5154
}
5255

5356
OutputDebugString(L"PF Start | ");
5457

5558
VideoFrame inputFrame = context.InputFrame();
5659
VideoFrame outputFrame = context.OutputFrame();
5760

61+
// X, Y, Width, Height
62+
//inputFrame.CopyToAsync(inputTransformed, copyBounds, copyBounds).get();
5863
OutputDebugString(L"PF Locked | ");
5964
Binding.Bind(InputImageDescription, inputFrame);
6065
Binding.Bind(OutputImageDescription, outputTransformed);
6166

6267
OutputDebugString(L"PF Eval | ");
6368
Session.Evaluate(Binding, L"test");
69+
OutputDebugString(L"PF Copy | ");
6470
outputTransformed.CopyToAsync(outputFrame).get();
6571

6672
OutputDebugString(L"PF End\n ");

Samples/StyleTransfer/VideoEffect/StyleTransferEffectCpp/StyleTransferEffect.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace winrt::StyleTransferEffectCpp::implementation
1515
{
1616
StyleTransferEffect();
1717
VideoFrame outputTransformed;
18+
VideoFrame inputTransformed;
1819

1920
IVectorView<VideoEncodingProperties> SupportedEncodingProperties();
2021
bool TimeIndependent();
@@ -38,6 +39,7 @@ namespace winrt::StyleTransferEffectCpp::implementation
3839
StyleTransferEffectNotifier Notifier;
3940
std::chrono::time_point<std::chrono::steady_clock> m_StartTime;
4041
bool firstProcessFrameCall = true;
42+
Windows::Graphics::Imaging::BitmapBounds copyBounds;
4143
};
4244
}
4345

0 commit comments

Comments
 (0)