22#include " StyleTransferEffect.h"
33#include " StyleTransferEffect.g.cpp"
44#include < ppltasks.h>
5+ #include < sstream>
56
67using namespace std ;
78using namespace winrt ::Windows::Storage;
@@ -11,8 +12,9 @@ using namespace concurrency;
1112namespace 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" \n Eval 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" \n Start 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
0 commit comments