-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathCustomVisionServiceWrapper.cs
More file actions
89 lines (76 loc) · 3.5 KB
/
CustomVisionServiceWrapper.cs
File metadata and controls
89 lines (76 loc) · 3.5 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
using Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training;
using Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IoTVisualAlerts.CustomVision
{
class CustomVisionServiceWrapper
{
CustomVisionTrainingClient customVisionTrainingClient = new CustomVisionTrainingClient
{
ApiKey = "{The training key for your Custom Vision Service instance}",
Endpoint = "https://westus2.api.cognitive.microsoft.com" // update with the region of your key
};
Guid targetCVSProjectGuid = new Guid("{Your Custom Vision Service target project id}");
public async Task PrepTargetProjectForTrainingAsync()
{
// delete tags
foreach (var tag in await customVisionTrainingClient.GetTagsAsync(targetCVSProjectGuid))
{
await customVisionTrainingClient.DeleteTagAsync(targetCVSProjectGuid, tag.Id);
}
// delete untagged images
var untaggedImages = await customVisionTrainingClient.GetUntaggedImagesAsync(targetCVSProjectGuid, iterationId: null, take: 256);
await customVisionTrainingClient.DeleteImagesAsync(targetCVSProjectGuid, untaggedImages.Select(i => i.Id).ToList());
// delete iterations
foreach (var iteration in await customVisionTrainingClient.GetIterationsAsync(targetCVSProjectGuid))
{
if (iteration.PublishName != null)
{
// we need to unpublish before we can delete it
await customVisionTrainingClient.UnpublishIterationAsync(targetCVSProjectGuid, iteration.Id);
}
await customVisionTrainingClient.DeleteIterationAsync(targetCVSProjectGuid, iteration.Id);
}
}
public async Task UploadTrainingImageAsync(Stream stream)
{
await customVisionTrainingClient.CreateImagesFromDataAsync(targetCVSProjectGuid, stream);
}
public async Task<Export> GetTrainedONNXExportIfAvailableAsync(DateTime minIterationTrainedTime)
{
var iterations = await customVisionTrainingClient.GetIterationsAsync(targetCVSProjectGuid);
Iteration targetIteration = iterations.Where(i => i.Status == "Completed" && i.TrainedAt > minIterationTrainedTime).FirstOrDefault();
if (targetIteration == null)
{
// no trained iteration to export at this point
return null;
}
// Trigger ONNX export and wait until it finishes
Export onnxExport;
while (true)
{
IList<Export> exports = await customVisionTrainingClient.GetExportsAsync(targetCVSProjectGuid, targetIteration.Id);
onnxExport = exports.Where(e => e.Platform == "ONNX").FirstOrDefault();
if (onnxExport == null)
{
onnxExport = await customVisionTrainingClient.ExportIterationAsync(targetCVSProjectGuid, targetIteration.Id, "onnx", flavor: "onnx12");
}
if (onnxExport.Status == "Exporting")
{
await Task.Delay(1000);
continue;
}
else
{
break;
}
}
return onnxExport;
}
}
}