Skip to content

Commit 62bfed2

Browse files
evalasync but super slow?
1 parent 6f843d8 commit 62bfed2

File tree

5 files changed

+52
-38
lines changed

5 files changed

+52
-38
lines changed

Samples/StyleTransfer/AppViewModel.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ public AppViewModel()
5555
await DispatcherHelper.RunAsync(() =>
5656
{
5757
// Running average just to see where it levels out at
58-
float temp = RenderFPS;
59-
RenderFPS = ((temp * numFrames) + e) / ++numFrames;
58+
//float temp = RenderFPS;
59+
//RenderFPS = ((temp * numFrames) + e) / ++numFrames;
60+
RenderFPS = e;
6061
});
6162

6263
};

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.9.0" />
12+
Version="1.0.10.0" />
1313

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

Samples/StyleTransfer/StyleTransfer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<WindowsXamlEnableOverview>true</WindowsXamlEnableOverview>
2020
<AppxPackageSigningEnabled>True</AppxPackageSigningEnabled>
2121
<GenerateAppInstallerFile>True</GenerateAppInstallerFile>
22-
<AppxAutoIncrementPackageRevision>False</AppxAutoIncrementPackageRevision>
22+
<AppxAutoIncrementPackageRevision>True</AppxAutoIncrementPackageRevision>
2323
<GenerateTestArtifacts>True</GenerateTestArtifacts>
2424
<AppxBundle>Always</AppxBundle>
2525
<AppxBundlePlatforms>x64</AppxBundlePlatforms>

Samples/StyleTransfer/VideoEffect/StyleTransferEffectCpp/StyleTransferEffect.cpp

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "StyleTransferEffect.h"
33
#include "StyleTransferEffect.g.cpp"
44
#include <ppltasks.h>
5+
#include <sstream>
56

67
using namespace std;
78
using namespace winrt::Windows::Storage;
@@ -11,8 +12,9 @@ using namespace concurrency;
1112
namespace winrt::StyleTransferEffectCpp::implementation
1213
{
1314
StyleTransferEffect::StyleTransferEffect() :
15+
cachedOutput(nullptr),
16+
cachedOutputCopy(VideoFrame(Windows::Graphics::Imaging::BitmapPixelFormat::Bgra8, 640, 360)),
1417
outputTransformed(VideoFrame(Windows::Graphics::Imaging::BitmapPixelFormat::Bgra8, 720, 720)),
15-
cachedOutput(VideoFrame(Windows::Graphics::Imaging::BitmapPixelFormat::Bgra8, 720, 720)),
1618
Session(nullptr),
1719
Binding(nullptr)
1820
{
@@ -36,51 +38,62 @@ namespace winrt::StyleTransferEffectCpp::implementation
3638
OutputDebugString(L"Close\n");
3739
if (Binding != nullptr) Binding.Clear();
3840
if (Session != nullptr) Session.Close();
39-
outputTransformed.Close();
4041
}
4142

43+
std::wstring index(const std::thread::id id)
44+
{
45+
static std::size_t nextindex = 0;
46+
static std::mutex my_mutex;
47+
static std::map<std::thread::id, std::string> ids;
48+
std::lock_guard<std::mutex> lock(my_mutex);
49+
if (ids.find(id) == ids.end()) {
50+
ids[id] = std::to_string((int)nextindex);
51+
nextindex++;
52+
}
53+
return std::wstring(ids[id].begin(), ids[id].end());
54+
}
4255

4356
void StyleTransferEffect::ProcessFrame(ProcessVideoFrameContext context) {
4457

45-
4658
OutputDebugString(L"PF Start | ");
47-
if (evalStatus != nullptr && evalStatus.Status() == Windows::Foundation::AsyncStatus::Started) {
48-
context.OutputFrame() = cachedOutput;
49-
OutputDebugString(L"PF Cache | ");
50-
return;
51-
}
59+
//OutputDebugString(index(thread().get_id()).c_str());
60+
auto now = std::chrono::high_resolution_clock::now();
5261
VideoFrame inputFrame = context.InputFrame();
5362
VideoFrame outputFrame = context.OutputFrame();
63+
VideoFrame temp = cachedOutput;
5464

55-
std::lock_guard<mutex> guard{ Processing };
56-
auto now = std::chrono::high_resolution_clock::now();
57-
std::chrono::milliseconds timePassed;
58-
// If the first time calling ProcessFrame, just start the timer
59-
if (firstProcessFrameCall) {
60-
m_StartTime = now;
61-
firstProcessFrameCall = false;
65+
OutputDebugString(L"PF Eval | ");
66+
if (evalStatus == nullptr || evalStatus.Status() != Windows::Foundation::AsyncStatus::Started)
67+
{
68+
Binding.Bind(InputImageDescription, inputFrame);
69+
Binding.Bind(OutputImageDescription, outputTransformed);
70+
auto nowEval = std::chrono::high_resolution_clock::now();
71+
evalStatus = Session.EvaluateAsync(Binding, L"test");
72+
evalStatus.Completed([&, nowEval](auto&& asyncInfo, winrt::Windows::Foundation::AsyncStatus const args) {
73+
VideoFrame output = asyncInfo.GetResults().Outputs().Lookup(OutputImageDescription).try_as<VideoFrame>();
74+
OutputDebugString(L"PF Copy | ");
75+
output.CopyToAsync(cachedOutputCopy);
76+
{
77+
cachedOutput = cachedOutputCopy;
78+
}
79+
OutputDebugString(L"PF End\n ");
80+
auto timePassedEval = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - nowEval);
81+
std::wostringstream ss;
82+
ss << (timePassedEval.count() / 1000.f);
83+
Notifier.SetFrameRate(timePassedEval.count() / 1000.f);
84+
OutputDebugString(L"\nEval Time : ");
85+
OutputDebugString(ss.str().c_str());
86+
});
6287

6388
}
64-
// On the second and any proceding process,
65-
else {
66-
timePassed = std::chrono::duration_cast<std::chrono::milliseconds>(now - m_StartTime);
67-
m_StartTime = now;
68-
Notifier.SetFrameRate(1000.f / timePassed.count()); // Convert to FPS: milli to seconds, invert
89+
if (temp != nullptr) {
90+
OutputDebugString(L"\nStart CopyAsync | ");
91+
temp.CopyToAsync(context.OutputFrame()).get();
92+
OutputDebugString(L"Stop CopyAsync\n");
6993
}
7094

71-
OutputDebugString(L"PF Locked | ");
72-
Binding.Bind(InputImageDescription, inputFrame);
73-
Binding.Bind(OutputImageDescription, outputTransformed);
74-
75-
OutputDebugString(L"PF Eval | ");
76-
evalStatus = Session.EvaluateAsync(Binding, L"test");
77-
auto evalTask = create_task(evalStatus);
78-
evalTask.then([&](LearningModelEvaluationResult result) {
79-
OutputDebugString(L"PF Copy | ");
80-
outputTransformed.CopyToAsync(context.OutputFrame()).get();
81-
cachedOutput = context.OutputFrame();
82-
OutputDebugString(L"PF End\n ");
83-
});
95+
auto timePassed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - now);
96+
//Notifier.SetFrameRate(1000.f / timePassed.count()); // Convert to FPS: milli to seconds, invert
8497

8598
}
8699

Samples/StyleTransfer/VideoEffect/StyleTransferEffectCpp/StyleTransferEffect.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ namespace winrt::StyleTransferEffectCpp::implementation
1515
{
1616
StyleTransferEffect();
1717
VideoFrame outputTransformed;
18-
1918
IVectorView<VideoEncodingProperties> SupportedEncodingProperties();
2019
bool TimeIndependent();
2120
MediaMemoryTypes SupportedMemoryTypes();
@@ -41,6 +40,7 @@ namespace winrt::StyleTransferEffectCpp::implementation
4140
Windows::Graphics::Imaging::BitmapBounds copyBounds;
4241
Windows::Foundation::IAsyncOperation<LearningModelEvaluationResult> evalStatus;
4342
VideoFrame cachedOutput;
43+
VideoFrame cachedOutputCopy;
4444
};
4545
}
4646

0 commit comments

Comments
 (0)