This repository has been archived by the owner on Feb 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 575
/
Copy pathImageProcessor.cs
502 lines (448 loc) · 21.1 KB
/
ImageProcessor.cs
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Microsoft Cognitive Services: http://www.microsoft.com/cognitive
//
// Microsoft Cognitive Services Github:
// https://github.com/Microsoft/Cognitive
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
using Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction;
using Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models;
using Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training;
using Microsoft.Azure.CognitiveServices.Vision.Face.Models;
using Microsoft.Azure.Storage.Blob;
using Microsoft.Rest;
using ServiceHelpers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Search;
namespace IntelligentKioskSample.Views.DigitalAssetManagement
{
public class ImageProcessor
{
public async Task<DigitalAssetData> ProcessImagesAsync(ImageProcessorSource source, IImageProcessorService[] services, int? fileLimit, int? startingFileIndex, Func<ImageInsights, Task> callback)
{
//validate
if (services == null || services.Length == 0)
{
throw new ApplicationException("No Azure services provided for image processing pipeline. Need at least 1.");
}
//get images
var insights = new List<ImageInsights>();
var (filePaths, reachedEndOfFiles) = await source.GetFilePaths(fileLimit, startingFileIndex);
//process each image - in batches
var tasks = new List<Task<ImageInsights>>();
var lastFile = filePaths.Last();
foreach (var filePath in filePaths)
{
tasks.Add(ProcessImageAsync(filePath, services));
if (tasks.Count == 8 || filePath == lastFile)
{
var results = await Task.WhenAll(tasks);
foreach (var task in tasks)
{
var insight = task.Result;
insights.Add(insight);
await callback(insight);
}
tasks.Clear();
}
}
var serviceTypes = services.Select(i => i.GetProcessorServiceType).Aggregate((i, e) => i | e);
var customVisionProjects = services.OfType<CustomVisionProcessorService>().SelectMany(i => i.ProjectIterations.Select(e => e.Project)).ToArray();
return new DigitalAssetData()
{
Info = new DigitalAssetInfo
{
Path = source.Path,
FileLimit = fileLimit,
Services = serviceTypes,
CustomVisionProjects = customVisionProjects,
Name = source.GetName(),
LastFileIndex = filePaths.Count() + (startingFileIndex ?? 0),
ReachedEndOfFiles = reachedEndOfFiles,
Source = source.GetType().Name
},
Insights = insights.ToArray()
};
}
async Task<ImageInsights> ProcessImageAsync(Uri filePath, IImageProcessorService[] services)
{
ImageAnalyzer analyzer = null;
if (filePath.IsFile)
{
analyzer = new ImageAnalyzer((await StorageFile.GetFileFromPathAsync(filePath.LocalPath)).OpenStreamForReadAsync);
}
else
{
analyzer = new ImageAnalyzer(filePath.AbsoluteUri);
}
analyzer.ShowDialogOnFaceApiErrors = true;
//run image processors
var tasks = services.Select(i => (i.ProcessImage(analyzer).ToArray())).ToArray();
await Task.WhenAll(tasks.SelectMany(i => i));
//run post image processor
await Task.WhenAll(services.SelectMany(i => (i.PostProcessImage(analyzer))));
//assign the results
var result = new ImageInsights { ImageUri = filePath };
for (int index = 0; index < services.Length; index++)
{
services[index].AssignResult(tasks[index], analyzer, result);
}
return result;
}
}
public interface IImageProcessorService
{
ImageProcessorServiceType GetProcessorServiceType { get; }
IEnumerable<Task> ProcessImage(ImageAnalyzer analyzer);
IEnumerable<Task> PostProcessImage(ImageAnalyzer analyzer);
void AssignResult(IEnumerable<Task> completeTask, ImageAnalyzer analyzer, ImageInsights result);
}
public class FaceProcessorService : IImageProcessorService
{
static readonly FaceAttributeType[] _faceFeatures = new[]
{
FaceAttributeType.Accessories,
FaceAttributeType.Age,
FaceAttributeType.Blur,
FaceAttributeType.Emotion,
FaceAttributeType.Exposure,
FaceAttributeType.FacialHair,
FaceAttributeType.Gender,
FaceAttributeType.Glasses,
FaceAttributeType.Hair,
FaceAttributeType.HeadPose,
FaceAttributeType.Makeup,
FaceAttributeType.Noise,
FaceAttributeType.Occlusion,
FaceAttributeType.Smile
};
public ImageProcessorServiceType GetProcessorServiceType { get => ImageProcessorServiceType.Face; }
public IEnumerable<Task> ProcessImage(ImageAnalyzer analyzer)
{
yield return analyzer.DetectFacesAsync(true, false, _faceFeatures);
}
public IEnumerable<Task> PostProcessImage(ImageAnalyzer analyzer)
{
yield return analyzer.FindSimilarPersistedFacesAsync();
}
public void AssignResult(IEnumerable<Task> completeTask, ImageAnalyzer analyzer, ImageInsights result)
{
// assign face api results
List<FaceInsights> faceInsightsList = new List<FaceInsights>();
foreach (var face in analyzer.DetectedFaces ?? Array.Empty<DetectedFace>())
{
FaceInsights faceInsights = new FaceInsights { FaceRectangle = face.FaceRectangle, FaceAttributes = face.FaceAttributes };
var similarFaceMatch = analyzer.SimilarFaceMatches?.FirstOrDefault(s => s.Face.FaceId == face.FaceId);
if (similarFaceMatch != null)
{
faceInsights.UniqueFaceId = similarFaceMatch.SimilarPersistedFace.PersistedFaceId.GetValueOrDefault();
}
faceInsightsList.Add(faceInsights);
}
result.FaceInsights = faceInsightsList.ToArray();
}
}
public class ComputerVisionProcessorService : IImageProcessorService
{
static readonly VisualFeatureTypes?[] _visionFeatures = new VisualFeatureTypes?[]
{
VisualFeatureTypes.Tags,
VisualFeatureTypes.Description,
VisualFeatureTypes.Objects,
VisualFeatureTypes.Brands,
VisualFeatureTypes.Categories,
VisualFeatureTypes.Adult,
VisualFeatureTypes.ImageType,
VisualFeatureTypes.Color
};
public ImageProcessorServiceType GetProcessorServiceType { get => ImageProcessorServiceType.ComputerVision; }
public IEnumerable<Task> ProcessImage(ImageAnalyzer analyzer)
{
yield return analyzer.AnalyzeImageAsync(null, visualFeatures: _visionFeatures);
yield return analyzer.RecognizeTextAsync();
}
public IEnumerable<Task> PostProcessImage(ImageAnalyzer analyzer)
{
yield break;
}
public void AssignResult(IEnumerable<Task> completeTask, ImageAnalyzer analyzer, ImageInsights result)
{
// assign computer vision results
result.VisionInsights = new VisionInsights
{
Caption = analyzer.AnalysisResult?.Description?.Captions.FirstOrDefault()?.Text,
Tags = analyzer.AnalysisResult?.Tags != null ? analyzer.AnalysisResult.Tags.Select(t => t.Name).ToArray() : new string[0],
Objects = analyzer.AnalysisResult?.Objects != null ? analyzer.AnalysisResult.Objects.Select(t => t.ObjectProperty).ToArray() : new string[0],
Celebrities = analyzer.AnalysisResult?.Categories != null ? analyzer.AnalysisResult.Categories.Where(i => i.Detail?.Celebrities != null && i.Detail.Celebrities.Count != 0).SelectMany(i => i.Detail.Celebrities).Select(i => i.Name).ToArray() : new string[0],
Landmarks = analyzer.AnalysisResult?.Categories != null ? analyzer.AnalysisResult.Categories.Where(i => i.Detail?.Landmarks != null && i.Detail.Landmarks.Count != 0).SelectMany(i => i.Detail.Landmarks).Select(i => i.Name).ToArray() : new string[0],
Brands = analyzer.AnalysisResult?.Brands != null ? analyzer.AnalysisResult.Brands.Select(t => t.Name).ToArray() : new string[0],
Adult = analyzer.AnalysisResult?.Adult,
Color = analyzer.AnalysisResult?.Color,
ImageType = analyzer.AnalysisResult?.ImageType,
Metadata = analyzer.AnalysisResult?.Metadata,
Words = analyzer.TextOperationResult?.Lines != null ? analyzer.TextOperationResult.Lines.SelectMany(i => i.Words).Select(i => i.Text).ToArray() : new string[0],
};
}
}
public class CustomVisionProcessorService : IImageProcessorService
{
SemaphoreSlim _semaphore = new SemaphoreSlim(1);
ICustomVisionPredictionClient _predictionClient;
public ProjectIteration[] ProjectIterations { get; }
public CustomVisionProcessorService(ICustomVisionPredictionClient predictionClient, ProjectIteration[] projectIterations)
{
//set fields
_predictionClient = predictionClient;
ProjectIterations = projectIterations;
}
public static async Task<ProjectIteration[]> GetProjectIterations(ICustomVisionTrainingClient trainingClient, string predictionResourceId, Guid[] projects)
{
var result = new List<ProjectIteration>();
foreach (var project in projects)
{
var iterations = await trainingClient.GetIterationsAsync(project);
var projectEntity = await trainingClient.GetProjectAsync(project);
var domain = await trainingClient.GetDomainAsync(projectEntity.Settings.DomainId);
var iteration = iterations.Where(i => i.Status == "Completed").OrderByDescending(i => i.TrainedAt.Value).FirstOrDefault();
if (iteration != null)
{
if (string.IsNullOrEmpty(iteration.PublishName))
{
await trainingClient.PublishIterationAsync(project, iteration.Id, publishName: iteration.Id.ToString(), predictionId: predictionResourceId);
iteration = await trainingClient.GetIterationAsync(project, iteration.Id);
}
result.Add(new ProjectIteration() { Project = project, Iteration = iteration.Id, ProjectName = projectEntity.Name, PublishedName = iteration.PublishName, IsObjectDetection = domain.Type == "ObjectDetection" });
}
}
return result.ToArray();
}
public ImageProcessorServiceType GetProcessorServiceType { get => ImageProcessorServiceType.CustomVision; }
public IEnumerable<Task> ProcessImage(ImageAnalyzer analyzer)
{
foreach (var projectIteration in ProjectIterations)
{
yield return PredictImage(analyzer, projectIteration, ProjectIterations.Length);
}
}
async Task<ValueTuple<ImagePrediction, ProjectIteration>> PredictImage(ImageAnalyzer analyzer, ProjectIteration projectIteration, int projectCount)
{
if (analyzer.ImageUrl != null)
{
return (await AutoRetry(async () => await _predictionClient.ClassifyImageUrlAsync(projectIteration.Project, projectIteration.PublishedName, new Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models.ImageUrl(analyzer.ImageUrl))), projectIteration);
}
else
{
//bug fix: if multiple project needed to be processed for a local file, create requests for them one at a time.
//(for some reason when the CustomVision client creates multiple requests using the same file stream it locks up creating the request - a better fix would be good as this one degrades speed)
var allowAsync = projectCount == 1;
return (await RunSequentially(async () => await AutoRetry(async () => await _predictionClient.ClassifyImageAsync(projectIteration.Project, projectIteration.PublishedName, await analyzer.GetImageStreamCallback())), allowAsync), projectIteration);
}
}
async Task<TResponse> RunSequentially<TResponse>(Func<Task<TResponse>> action, bool allowAsync)
{
if (!allowAsync)
{
await _semaphore.WaitAsync();
}
try
{
return await action();
}
finally
{
if (!allowAsync)
{
_semaphore.Release();
}
}
}
async Task<TResponse> AutoRetry<TResponse>(Func<Task<TResponse>> action)
{
int retriesLeft = 6;
int delay = 500;
TResponse response = default(TResponse);
while (true)
{
try
{
response = await action();
break;
}
catch (HttpOperationException exception) when (exception.Response.StatusCode == (System.Net.HttpStatusCode)429 && retriesLeft > 0)
{
ErrorTrackingHelper.TrackException(exception, "Custom Vision API throttling error");
await Task.Delay(delay);
retriesLeft--;
delay *= 2;
continue;
}
}
return response;
}
public IEnumerable<Task> PostProcessImage(ImageAnalyzer analyzer)
{
yield break;
}
public void AssignResult(IEnumerable<Task> completeTask, ImageAnalyzer analyzer, ImageInsights result)
{
result.CustomVisionInsights = completeTask.OfType<Task<ValueTuple<ImagePrediction, ProjectIteration>>>().Select(i => new CustomVisionInsights
{
Name = i.Result.Item2.ProjectName,
IsObjectDetection = i.Result.Item2.IsObjectDetection,
Predictions = i.Result.Item1.Predictions.Select(e => new CustomVisionPrediction
{
Name = e.TagName,
Probability = e.Probability
}).ToArray()
}).ToArray();
}
public class ProjectIteration
{
public Guid Project { get; set; }
public Guid Iteration { get; set; }
public string ProjectName { get; set; }
public string PublishedName { get; set; }
public bool IsObjectDetection { get; set; }
}
}
public abstract class ImageProcessorSource
{
protected string[] ValidExtentions = { ".png", ".jpg", ".bmp", ".jpeg", ".gif" };
public Uri Path { get; }
public ImageProcessorSource(Uri path)
{
//set fields
Path = path;
}
public abstract Task<(IEnumerable<Uri>, bool)> GetFilePaths(int? fileLimit, int? startingIndex);
public abstract string GetName();
}
public class StorageSource : ImageProcessorSource
{
public StorageSource(Uri path) : base(path) { }
public override async Task<(IEnumerable<Uri>, bool)> GetFilePaths(int? fileLimit, int? startingIndex)
{
//calculate max results
var maxResults = fileLimit;
if (fileLimit != null && startingIndex != null)
{
maxResults = fileLimit + startingIndex;
}
var container = new CloudBlobContainer(Path);
var results = new List<Uri>();
var reachedFileLimit = false;
var skipped = 0;
var files = await container.ListBlobsSegmentedAsync(null, false, BlobListingDetails.None, maxResults, null, null, null);
BlobContinuationToken continuationToken = null;
do
{
foreach (var file in files.Results)
{
//skip if not the right extention
var extentionIndex = file.Uri.LocalPath.LastIndexOf('.');
if (extentionIndex >= 0 && ValidExtentions.Contains(file.Uri.LocalPath.Substring(extentionIndex), StringComparer.OrdinalIgnoreCase))
{
//skip
if (startingIndex != null && skipped != startingIndex)
{
skipped++;
continue;
}
//create file URI
var root = Path.AbsoluteUri.Remove(Path.AbsoluteUri.Length - Path.PathAndQuery.Length);
var query = Path.Query;
var path = file.Uri.LocalPath;
var fileUri = new Uri(root + path + query);
results.Add(fileUri);
//at file limit
if (fileLimit != null && results.Count >= fileLimit.Value)
{
reachedFileLimit = true;
break;
}
}
}
//get more results
continuationToken = files.ContinuationToken;
if (files.ContinuationToken != null && !reachedFileLimit)
{
files = await container.ListBlobsSegmentedAsync(null, false, BlobListingDetails.Metadata, maxResults, files.ContinuationToken, null, null);
}
else
{
break;
}
} while (continuationToken != null);
//determine if we reached the end of all files
var reachedEndOfFiles = !reachedFileLimit;
if (reachedEndOfFiles && files.ContinuationToken != null)
{
reachedEndOfFiles = false;
}
return (results.ToArray(), reachedEndOfFiles);
}
public override string GetName()
{
return Uri.UnescapeDataString(Path.LocalPath.Replace(@"/", string.Empty));
}
}
public class FileSource : ImageProcessorSource
{
public FileSource(Uri path) : base(path) { }
public override async Task<(IEnumerable<Uri>, bool)> GetFilePaths(int? fileLimit, int? startingIndex)
{
//calulate new file limit
if (fileLimit != null && startingIndex != null)
{
fileLimit = fileLimit + startingIndex;
}
var folder = await StorageFolder.GetFolderFromPathAsync(Path.LocalPath);
var query = folder.CreateFileQueryWithOptions(new QueryOptions(CommonFileQuery.DefaultQuery, ValidExtentions));
var files = fileLimit != null ? await query.GetFilesAsync(0, (uint)fileLimit.Value) : await query.GetFilesAsync();
var filePaths = files.Select(i => new Uri(i.Path));
if (startingIndex != null)
{
filePaths = filePaths.Skip(startingIndex.Value);
}
var result = filePaths.ToArray();
var reachedEndOfFiles = fileLimit == null || result.Length < fileLimit;
return (result, reachedEndOfFiles);
}
public override string GetName()
{
return Uri.UnescapeDataString(Path.Segments.Last());
}
}
}