PTI.Microservices.Library.AzureCustomVision 7.0.0

dotnet add package PTI.Microservices.Library.AzureCustomVision --version 7.0.0                
NuGet\Install-Package PTI.Microservices.Library.AzureCustomVision -Version 7.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PTI.Microservices.Library.AzureCustomVision" Version="7.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PTI.Microservices.Library.AzureCustomVision --version 7.0.0                
#r "nuget: PTI.Microservices.Library.AzureCustomVision, 7.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PTI.Microservices.Library.AzureCustomVision as a Cake Addin
#addin nuget:?package=PTI.Microservices.Library.AzureCustomVision&version=7.0.0

// Install PTI.Microservices.Library.AzureCustomVision as a Cake Tool
#tool nuget:?package=PTI.Microservices.Library.AzureCustomVision&version=7.0.0                

PTI.Microservices.Library.AzureCustomVision

Facilitates the consumption of the APIs in Azure Custom Vision

Examples:

Note: The examples below are passing null for the logger, if you want to use the logger make sure to pass the parameter with a value other than null

Analyze Image

string iterationCode = ITERATION_CODE;
Guid projectId = Guid.Parse(PROJECT_ID);
Guid imageId = Guid.Parse(IMAGE_ID);
AzureCustomVisionService azureCustomVisionService = CreateCustomVisionClient();
var originalImageBytes = new HttpClient()
    .GetByteArrayAsync(IMAGE_URL)
    .Result;
MemoryStream originalImageStream = new MemoryStream(originalImageBytes);
Stream sstr = File.Open(IMAGE_FILE_PATH, FileMode.Open);
var predictionResult = await azureCustomVisionService.DetectImageAsync(sstr,
    projectId, iterationCode);
Assert.IsNotNull(predictionResult);
//cropping seems to be working, but prediction has a higher probability for incorrect section,
//custom vision projects needs more training.
var ingredientsRegions = predictionResult.Predictions.Where(p => p.TagName == "Ingredients")
    .Where(p=>p.Probability > 0.5)
    .OrderByDescending(p => p.Probability).ToArray();
ImagesService imagesService = new ImagesService(null);
for (int i=0; i < ingredientsRegions.Count(); i++)
{
    var ingredientsRegion = ingredientsRegions[i];
    originalImageStream = new MemoryStream(originalImageBytes);
    System.Drawing.Image img = System.Drawing.Image.FromStream(originalImageStream);
    originalImageStream = new MemoryStream(originalImageBytes);
    var croppedImage = imagesService.Crop(originalImageStream,
        (float)ingredientsRegion.BoundingBox.Width * img.Width,
        (float)ingredientsRegion.BoundingBox.Height * img.Height,
        (float)ingredientsRegion.BoundingBox.Left * img.Width,
        (float)ingredientsRegion.BoundingBox.Top * img.Height);
    System.IO.File.WriteAllBytes($@"C:\Temp\PredictedImage_{i}.png", croppedImage);
}

Create Image From Regions

Guid projectId = Guid.Parse(PROJECT_ID);
Guid imageId = Guid.Parse(IMAGE_ID);
AzureCustomVisionService azureCustomVisionService = CreateCustomVisionClient();
var images = await azureCustomVisionService.CreateImageFromRegionsAsync(projectId, imageId, "Ingredients");
Assert.IsTrue(images.Count() > 0);
for (int i = 0; i < images.Count(); i++)
{
    System.IO.File.WriteAllBytes($@"C:\Temp\{i}.png", images[i].ToArray());
}

Query Predictions

Guid projectId = Guid.Parse(PROJECT_ID);
AzureCustomVisionService azureCustomVisionService = CreateCustomVisionClient();
var results = await azureCustomVisionService.QueryPredictionsAsync(projectId);
var imageUrl = results.Results.First().OriginalImageUri;
var imageBytes = new HttpClient().GetByteArrayAsync(imageUrl).Result;
System.IO.File.WriteAllBytes(@"C:\TestResultImage.png", imageBytes);

Basic Import/Upload Flow

AzureCustomVisionService azureCustomVisionService = CreateCustomVisionClient();
var firstDomain = (await azureCustomVisionService.GetDomainsAsync()).First();
string projectName = "Automated Tests Project";
var existentProject = await azureCustomVisionService.GetProjectByNameAsync(projectName);
if (existentProject != null)
    await azureCustomVisionService.DeleteProjectAsync(existentProject.Id);
var project = await azureCustomVisionService.CreateProjectAsync(projectName, "Created from PTI Microservices library Automated Tests",
    firstDomain.Id, AzureCustomVisionService.ClassificationType.Multiclass);
Assert.IsNotNull(project);
System.Collections.Generic.List<Uri> lstUris = new List<Uri>()
{
    new Uri(this.TestComputerVisionImageUri),
    new Uri(this.TestFaceImageUrl)
};
var uploadImagesResult = await azureCustomVisionService.UploadImagesAsync(lstUris,
    project.Id);
List<string> testTags = new List<string>()
{
    "Cat0","Cat1","Cat2"
};
foreach (var singleImage in uploadImagesResult)
{
    var createImageTagsResult = await azureCustomVisionService
        .CreateImageTagsAsync(project.Id, singleImage.Image.Id, testTags, addTagsToSuggestedRegions: true);
    await Task.Delay(TimeSpan.FromSeconds(5));
}
try
{
    var trainingResult = await azureCustomVisionService.TrainProjectAsync(project.Id);
}
catch (Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training.Models.CustomVisionErrorException ex)
{
    //expected exception since we are not sending enough images tagged
}
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
7.0.0 401 11/13/2022
2.0.0-preview 221 7/9/2021