Skip to content

Commit ae57c95

Browse files
send framerate from processframe
1 parent 07d1b74 commit ae57c95

File tree

7 files changed

+94
-16
lines changed

7 files changed

+94
-16
lines changed

Samples/StyleTransfer/AppViewModel.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,8 @@ public AppViewModel()
4949
NotifyUser(true);
5050

5151
m_notifier = new StyleTransferEffectNotifier();
52-
m_notifier.AccountIsInDebit += M_notifier_AccountIsInDebit;
53-
m_notifier.AdjustBalance(-4.0f);
54-
}
52+
m_notifier.FrameRateUpdated += async (_, e) => await DispatcherHelper.RunAsync(() => RenderFPS = e);
5553

56-
private void M_notifier_AccountIsInDebit(object sender, float e)
57-
{
58-
Debug.WriteLine("Account in Debit: " + e);
5954
}
6055

6156
// Media capture properties
@@ -77,8 +72,8 @@ private void M_notifier_AccountIsInDebit(object sender, float e)
7772
// Activatable Class ID of the video effect.
7873
private String _videoEffectID = "StyleTransferEffectCpp.StyleTransferEffect";
7974
System.Threading.Mutex Processing = new Mutex();
80-
EventRegistrationToken m_eventToken;
8175
StyleTransferEffectCpp.StyleTransferEffectNotifier m_notifier;
76+
private float _renderFPS;
8277

8378
// Image style transfer properties
8479
uint m_inWidth, m_inHeight, m_outWidth, m_outHeight;
@@ -95,6 +90,24 @@ public AppModel CurrentApp
9590
get { return _appModel; }
9691
}
9792

93+
public float RenderFPS
94+
{
95+
get { return (float)Math.Round(_renderFPS, 2); }
96+
set { _renderFPS = value; OnPropertyChanged(); }
97+
}
98+
99+
private float _captureFPS;
100+
public float CaptureFPS
101+
{
102+
get
103+
{
104+
return _captureFPS;
105+
}
106+
set
107+
{
108+
_captureFPS = value; OnPropertyChanged();
109+
}
110+
}
98111
private SoftwareBitmapSource _inputSoftwareBitmapSource;
99112
public SoftwareBitmapSource InputSoftwareBitmapSource
100113
{
@@ -350,7 +363,11 @@ public async Task ChangeLiveStream()
350363
videoEffect = await _mediaCapture.AddVideoEffectAsync(videoEffectDefinition, MediaStreamType.VideoPreview);
351364
videoEffect.SetProperties(new PropertySet() {
352365
{"ModelName", modelPath },
353-
{"UseGPU", _appModel.UseGPU }});
366+
{"UseGPU", _appModel.UseGPU },
367+
{ "Notifier", m_notifier} });
368+
369+
var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;
370+
CaptureFPS = props.FrameRate.Numerator / props.FrameRate.Denominator;
354371

355372
await _mediaCapture.StartPreviewAsync();
356373
isPreviewing = true;
@@ -425,6 +442,7 @@ private async Task CleanupCameraAsync()
425442
Debug.WriteLine("CleanupCameraAsync");
426443
try
427444
{
445+
RenderFPS = 0;
428446
// Clean up the media capture
429447
if (_mediaCapture != null)
430448
{

Samples/StyleTransfer/MainPage.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,9 @@
320320
RelativePanel.AlignVerticalCenterWithPanel="True"
321321
Visibility="{Binding CurrentApp.StreamingControlsVisible, Converter={StaticResource BoolToVis}, Mode=OneWay}">
322322
<Run x:Uid="TxtCapture" FontWeight="Bold" />
323-
<Run Text="{x:Bind dummyFPS}" />
323+
<Run Text="{Binding CaptureFPS}" />
324324
<Run x:Uid="TxtRender" FontWeight="Bold" />
325-
<Run FontWeight="Normal" Text="{x:Bind dummyFPS}" />
325+
<Run FontWeight="Normal" Text="{Binding RenderFPS}" />
326326
</TextBlock>
327327
</RelativePanel>
328328
</Border>

Samples/StyleTransfer/VideoEffect/StyleTransferEffectCpp/StyleTransferEffect.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,25 @@ namespace winrt::StyleTransferEffectCpp::implementation
3535
}
3636

3737
void StyleTransferEffect::ProcessFrame(ProcessVideoFrameContext context) {
38-
Processing.lock();
3938
OutputDebugString(L"Start ProcessFrame | ");
39+
auto startSync = std::chrono::high_resolution_clock::now();
40+
4041
VideoFrame inputFrame = context.InputFrame();
4142
VideoFrame outputFrame = context.OutputFrame();
4243

43-
OutputDebugString(L"PF Binding | ");
44+
Processing.lock();
45+
OutputDebugString(L"PF Locked | ");
4446
Binding.Bind(InputImageDescription, inputFrame);
4547
Binding.Bind(OutputImageDescription, outputTransformed);
4648

4749
OutputDebugString(L"PF Eval | ");
4850
Session.Evaluate(Binding, L"test");
4951
outputTransformed.CopyToAsync(context.OutputFrame());
50-
OutputDebugString(L"Stop ProcessFrame | ");
5152
Processing.unlock();
52-
OutputDebugString(L"End Lock ProcessFrame\n");
53+
OutputDebugString(L"PF Unlocked");
54+
55+
auto syncTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - startSync);
56+
Notifier.SetFrameRate(1000.f / syncTime.count()); // Convert to FPS: milli to seconds, invert
5357
}
5458

5559
void StyleTransferEffect::SetEncodingProperties(VideoEncodingProperties props, IDirect3DDevice device) {
@@ -66,9 +70,10 @@ namespace winrt::StyleTransferEffectCpp::implementation
6670
modelName = unbox_value<hstring>(val);
6771
val = configuration.TryLookup(L"UseGPU");
6872
bool useGpu = unbox_value<bool>(val);
69-
OutputDebugString(modelName.c_str());
70-
LearningModel m_model = LearningModel::LoadFromFilePath(modelName);
73+
val = configuration.TryLookup(L"Notifier");
74+
Notifier = val.try_as<StyleTransferEffectNotifier>();
7175

76+
LearningModel m_model = LearningModel::LoadFromFilePath(modelName);
7277
LearningModelDeviceKind m_device = useGpu ? LearningModelDeviceKind::DirectX : LearningModelDeviceKind::Cpu;
7378
Session = LearningModelSession{ m_model, LearningModelDevice(m_device) };
7479
Binding = LearningModelBinding{ Session };

Samples/StyleTransfer/VideoEffect/StyleTransferEffectCpp/StyleTransferEffect.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ namespace winrt::StyleTransferEffectCpp::implementation
3535
hstring OutputImageDescription;
3636
VideoEncodingProperties encodingProperties;
3737
std::mutex Processing;
38+
StyleTransferEffectNotifier Notifier;
3839
};
3940
}
4041

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "pch.h"
2+
#include "StyleTransferEffectNotifier.h"
3+
#include "StyleTransferEffectNotifier.g.cpp"
4+
5+
6+
namespace winrt::StyleTransferEffectCpp::implementation
7+
{
8+
void StyleTransferEffectNotifier::SetFrameRate(float value)
9+
{
10+
m_balance = value;
11+
m_frameRateUpdatedEvent(*this, m_balance);
12+
}
13+
winrt::event_token StyleTransferEffectNotifier::FrameRateUpdated(Windows::Foundation::EventHandler<float> const& handler)
14+
{
15+
return m_frameRateUpdatedEvent.add(handler);
16+
}
17+
void StyleTransferEffectNotifier::FrameRateUpdated(winrt::event_token const& token) noexcept
18+
{
19+
m_frameRateUpdatedEvent.remove(token);
20+
}
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
#include "StyleTransferEffectNotifier.g.h"
3+
4+
5+
namespace winrt::StyleTransferEffectCpp::implementation
6+
{
7+
struct StyleTransferEffectNotifier : StyleTransferEffectNotifierT<StyleTransferEffectNotifier>
8+
{
9+
StyleTransferEffectNotifier() = default;
10+
11+
void SetFrameRate(float value);
12+
winrt::event_token FrameRateUpdated(Windows::Foundation::EventHandler<float> const& handler);
13+
void FrameRateUpdated(winrt::event_token const& token) noexcept;
14+
private:
15+
winrt::event<Windows::Foundation::EventHandler<float>> m_frameRateUpdatedEvent;
16+
float m_balance{ 0.f };
17+
};
18+
}
19+
namespace winrt::StyleTransferEffectCpp::factory_implementation
20+
{
21+
struct StyleTransferEffectNotifier : StyleTransferEffectNotifierT<StyleTransferEffectNotifier, implementation::StyleTransferEffectNotifier>
22+
{
23+
};
24+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace StyleTransferEffectCpp
2+
{
3+
runtimeclass StyleTransferEffectNotifier
4+
{
5+
StyleTransferEffectNotifier();
6+
void SetFrameRate(Single value);
7+
event Windows.Foundation.EventHandler<Single> FrameRateUpdated;
8+
}
9+
}

0 commit comments

Comments
 (0)