-
Notifications
You must be signed in to change notification settings - Fork 215
Expand file tree
/
Copy pathPoseDetection.xaml.cs
More file actions
195 lines (163 loc) · 6.59 KB
/
PoseDetection.xaml.cs
File metadata and controls
195 lines (163 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using AIDevGallery.Models;
using AIDevGallery.Samples.Attributes;
using AIDevGallery.Samples.SharedCode;
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media.Imaging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using Windows.Storage.Pickers;
namespace AIDevGallery.Samples.OpenSourceModels.HRNetPose;
[GallerySample(
Model1Types = [ModelType.HRNetPose],
Scenario = ScenarioType.ImageDetectPose,
SharedCode = [
SharedCodeEnum.Prediction,
SharedCodeEnum.BitmapFunctions,
SharedCodeEnum.DeviceUtils,
SharedCodeEnum.PoseHelper
],
NugetPackageReferences = [
"System.Drawing.Common",
"Microsoft.ML.OnnxRuntime.Extensions",
"System.Numerics.Tensors"
],
AssetFilenames = [
"pose_default.png"
],
Name = "Pose Detection",
Id = "9b74ccc0-f5f7-430f-bed0-712ffc063508",
Icon = "\uE8B3")]
internal sealed partial class PoseDetection : BaseSamplePage
{
private InferenceSession? _inferenceSession;
public PoseDetection()
{
this.Unloaded += (s, e) => _inferenceSession?.Dispose();
this.Loaded += (s, e) => Page_Loaded(); // <exclude-line>
this.InitializeComponent();
}
// <exclude>
private void Page_Loaded()
{
UploadButton.Focus(FocusState.Programmatic);
}
// </exclude>
protected override async Task LoadModelAsync(SampleNavigationParameters sampleParams)
{
try
{
await InitModel(sampleParams.ModelPath, sampleParams.WinMlSampleOptions.Policy, sampleParams.WinMlSampleOptions.EpName, sampleParams.WinMlSampleOptions.CompileModel, sampleParams.WinMlSampleOptions.DeviceType);
sampleParams.NotifyCompletion();
}
catch (Exception ex)
{
ShowException(ex, "Failed to load model.");
return;
}
await DetectPose(Path.Join(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "Assets", "pose_default.png"));
}
private Task InitModel(string modelPath, ExecutionProviderDevicePolicy? policy, string? epName, bool compileModel, string? deviceType)
{
return Task.Run(async () =>
{
if (_inferenceSession != null)
{
return;
}
var catalog = Microsoft.Windows.AI.MachineLearning.ExecutionProviderCatalog.GetDefault();
try
{
var registeredProviders = await catalog.EnsureAndRegisterCertifiedAsync();
}
catch (Exception ex)
{
Debug.WriteLine($"WARNING: Failed to install packages: {ex.Message}");
}
SessionOptions sessionOptions = new();
sessionOptions.RegisterOrtExtensions();
if (policy != null)
{
sessionOptions.SetEpSelectionPolicy(policy.Value);
}
else if (epName != null)
{
sessionOptions.AppendExecutionProviderFromEpName(epName, deviceType);
if (compileModel)
{
modelPath = sessionOptions.GetCompiledModel(modelPath, epName) ?? modelPath;
}
}
_inferenceSession = new InferenceSession(modelPath, sessionOptions);
});
}
private async void UploadButton_Click(object sender, RoutedEventArgs e)
{
var window = new Window();
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
var picker = new FileOpenPicker();
WinRT.Interop.InitializeWithWindow.Initialize(picker, hwnd);
picker.FileTypeFilter.Add(".png");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".jpg");
picker.ViewMode = PickerViewMode.Thumbnail;
var file = await picker.PickSingleFileAsync();
UploadButton.Focus(FocusState.Programmatic);
if (file != null)
{
SendSampleInteractedEvent("FileSelected"); // <exclude-line>
await DetectPose(file.Path);
}
}
private async Task DetectPose(string filePath)
{
if (!Path.Exists(filePath))
{
return;
}
Loader.IsActive = true;
Loader.Visibility = Visibility.Visible;
UploadButton.Visibility = Visibility.Collapsed;
DefaultImage.Source = new BitmapImage(new Uri(filePath));
NarratorHelper.AnnounceImageChanged(DefaultImage, "Content changed: new upload."); // <exclude-line>
using Bitmap originalImage = new(filePath);
int modelInputWidth = 256;
int modelInputHeight = 192;
using Bitmap resizedImage = BitmapFunctions.ResizeBitmap(originalImage, modelInputWidth, modelInputHeight);
var predictions = await Task.Run(() =>
{
Tensor<float> input = new DenseTensor<float>([1, 3, modelInputWidth, modelInputHeight]);
input = BitmapFunctions.PreprocessBitmapWithStdDev(resizedImage, input);
var inputMetadataName = _inferenceSession!.InputNames[0];
var onnxInputs = new List<NamedOnnxValue>
{
NamedOnnxValue.CreateFromTensor(inputMetadataName, input)
};
using IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results = _inferenceSession!.Run(onnxInputs);
var heatmaps = results[0].AsTensor<float>();
var outputName = _inferenceSession!.OutputNames[0];
var outputDimensions = _inferenceSession!.OutputMetadata[outputName].Dimensions;
float outputWidth = outputDimensions[2];
float outputHeight = outputDimensions[3];
List<(float X, float Y)> keypointCoordinates = PoseHelper.PostProcessResults(heatmaps, originalImage.Width, originalImage.Height, outputWidth, outputHeight);
return keypointCoordinates;
});
using Bitmap output = PoseHelper.RenderPredictions(originalImage, predictions, .02f);
BitmapImage outputImage = BitmapFunctions.ConvertBitmapToBitmapImage(output);
NarratorHelper.AnnounceImageChanged(DefaultImage, "Content changed: key points rendered."); // <exclude-line>
DispatcherQueue.TryEnqueue(() =>
{
DefaultImage.Source = outputImage;
Loader.IsActive = false;
Loader.Visibility = Visibility.Collapsed;
UploadButton.Visibility = Visibility.Visible;
});
}
}