-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathSegmentStreets.xaml.cs
More file actions
287 lines (243 loc) · 10.9 KB
/
SegmentStreets.xaml.cs
File metadata and controls
287 lines (243 loc) · 10.9 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// 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.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Threading.Tasks;
using Windows.Storage.Pickers;
namespace AIDevGallery.Samples.OpenSourceModels.FFNet;
[GallerySample(
Model1Types = [ModelType.FFNet],
Scenario = ScenarioType.ImageSegmentStreet,
Name = "Segment Streetscapes",
SharedCode = [
SharedCodeEnum.Prediction,
SharedCodeEnum.BitmapFunctions,
SharedCodeEnum.DeviceUtils
],
NugetPackageReferences = [
"System.Drawing.Common",
"Microsoft.ML.OnnxRuntime.Extensions",
"System.Numerics.Tensors"
],
AssetFilenames = [
"streetscape.png",
],
Id = "9b74acc0-a5f7-430f-bed0-958ffc063598",
Icon = "\uE8B3")]
internal sealed partial class SegmentStreets : BaseSamplePage
{
private InferenceSession? _inferenceSession;
public SegmentStreets()
{
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 Segment(Path.Join(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "Assets", "streetscape.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 Segment(file.Path);
}
}
private async Task Segment(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 originalImageWidth = originalImage.Width;
int originalImageHeight = originalImage.Height;
int modelInputWidth = 2048;
int modelInputHeight = 1024;
int scaledWidth = 256;
int scaledHeight = 128;
// Resize original image to match model input
using Bitmap resizedImage = BitmapFunctions.ResizeBitmap(originalImage, modelInputWidth, modelInputHeight);
// Remove using statement for processedImage; we'll dispose of it manually later
Bitmap processedImage = await Task.Run(() =>
{
// Preprocess image and prepare input tensor
Tensor<float> input = new DenseTensor<float>([1, 3, modelInputHeight, modelInputWidth]);
input = BitmapFunctions.PreprocessBitmapWithStdDev(resizedImage, input);
var inputMetadataName = _inferenceSession!.InputNames[0];
var inputs = new List<NamedOnnxValue> { NamedOnnxValue.CreateFromTensor(inputMetadataName ?? "image", input) };
// Run inference
using IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results = _inferenceSession!.Run(inputs);
// Extract the output tensor (shape: [1, 19, 128, 256])
var output = results[0].AsTensor<float>();
int num_classes = output.Dimensions[1];
// Convert tensor output to a Bitmap of the scaled dimensions
using Bitmap scaledBitmap = new(scaledWidth, scaledHeight, PixelFormat.Format32bppArgb);
BitmapData scaledData = scaledBitmap.LockBits(
new Rectangle(0, 0, scaledBitmap.Width, scaledBitmap.Height),
ImageLockMode.WriteOnly,
PixelFormat.Format32bppArgb);
IntPtr scaledPtr = scaledData.Scan0;
int scaledBytes = Math.Abs(scaledData.Stride) * scaledBitmap.Height;
byte[] scaledRgbValues = new byte[scaledBytes];
// Assign colors to each class
Dictionary<int, Color> classColors = new()
{
{ 0, Color.FromArgb(128, Color.Red) }, // Road
{ 1, Color.FromArgb(128, Color.Green) }, // Sidewalks
{ 2, Color.FromArgb(128, Color.Blue) }, // Buildings
{ 3, Color.FromArgb(128, Color.Yellow) }, // Walls
{ 4, Color.FromArgb(128, Color.Magenta) }, // Fences
{ 5, Color.FromArgb(128, Color.Cyan) }, // Poles
{ 6, Color.FromArgb(128, Color.Orange) }, // Traffic lights
{ 7, Color.FromArgb(128, Color.Purple) }, // Steet sign
{ 8, Color.FromArgb(128, Color.Pink) }, // Nature
{ 9, Color.FromArgb(128, Color.Brown) }, // Dirt
{ 10, Color.FromArgb(128, Color.LightBlue) }, // Sky
{ 11, Color.FromArgb(128, Color.LightGreen) }, // Pedistrian
{ 12, Color.FromArgb(128, Color.Gray) }, // Rider
{ 13, Color.FromArgb(128, Color.Lime) }, // Vehicle (Continous unit)
{ 14, Color.FromArgb(128, Color.Gold) }, // Truck (Separate components)
{ 15, Color.FromArgb(128, Color.Silver) }, // Bus
{ 16, Color.FromArgb(128, Color.Maroon) }, // Train
{ 17, Color.FromArgb(128, Color.Red) }, // Motorcycle
{ 18, Color.FromArgb(128, Color.Navy) } // Bicycle
};
// Loop through each pixel to determine its color based on the best class
for (int y = 0; y < scaledHeight; y++)
{
for (int x = 0; x < scaledWidth; x++)
{
float maxProbability = -1f;
int bestClass = -1;
for (int c = 0; c < num_classes; c++)
{
if (output[0, c, y, x] > maxProbability)
{
maxProbability = output[0, c, y, x];
bestClass = c;
}
}
if (bestClass >= 0)
{
Color color = classColors[bestClass];
int index = (y * scaledData.Stride) + (x * 4);
scaledRgbValues[index] = color.B;
scaledRgbValues[index + 1] = color.G;
scaledRgbValues[index + 2] = color.R;
scaledRgbValues[index + 3] = color.A;
}
}
}
System.Runtime.InteropServices.Marshal.Copy(scaledRgbValues, 0, scaledPtr, scaledBytes);
scaledBitmap.UnlockBits(scaledData);
// Resize scaledBitmap to original dimensions
using Bitmap upscaledBitmap = new(originalImageWidth, originalImageHeight);
using (Graphics g = Graphics.FromImage(upscaledBitmap))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(scaledBitmap, 0, 0, originalImageWidth, originalImageHeight);
}
// Overlay the upscaled mask on the original image
Bitmap combinedImage = new(originalImageWidth, originalImageHeight, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(combinedImage))
{
g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
g.DrawImage(originalImage, 0, 0, originalImageWidth, originalImageHeight);
g.DrawImage(upscaledBitmap, 0, 0, originalImageWidth, originalImageHeight);
}
return combinedImage;
});
// Convert the final overlay to BitmapImage for display
BitmapImage outputImage = BitmapFunctions.ConvertBitmapToBitmapImage(processedImage);
NarratorHelper.AnnounceImageChanged(DefaultImage, "Content changed: all regions segmented."); // <exclude-line>
DispatcherQueue.TryEnqueue(() =>
{
DefaultImage.Source = outputImage;
Loader.IsActive = false;
Loader.Visibility = Visibility.Collapsed;
UploadButton.Visibility = Visibility.Visible;
});
// Dispose of processedImage after the conversion is done
processedImage.Dispose();
}
}