Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom vision C# SDK #4152

Merged
merged 5 commits into from
Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.Azure.Test.HttpRecorder;
using System;
using System.Net.Http;

namespace Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Tests
{
public abstract class BaseTests
{
private static readonly string PredictionKey;
protected static readonly Guid ProjectId;
static BaseTests()
{
PredictionKey = "";
ProjectId = Guid.Parse("e222c033-5f5d-4a23-bde9-8343f19c0a01");
}

protected IPredictionEndpoint GetPredictionEndpointClient(DelegatingHandler handler)
{
IPredictionEndpoint client = new PredictionEndpoint(handlers: handler)
{
ApiKey = PredictionKey
};

return client;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$([MSBuild]::GetPathOfFileAbove('AzSdk.test.reference.props'))" />
<PropertyGroup>
<Description>Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Tests Class Library</Description>
<AssemblyName>Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Tests</AssemblyName>
<VersionPrefix>1.0.0</VersionPrefix>
</PropertyGroup>

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" Version="1.6.0-preview" />
<PackageReference Include="xunit.runner.console" Version="2.4.0-beta.1.build3958" />
<ProjectReference Include="..\Prediction\Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="SessionRecords\**\*.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestImages\*.jpg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestImages\*.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="testsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Models;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using System;
using System.IO;
using Xunit;

namespace Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Tests
{
public class PredictionTests : BaseTests
{
[Fact]
public void PredictImage()
{
using (MockContext context = MockContext.Start(this.GetType().Name))
{
HttpMockServer.Initialize(this.GetType().Name, "PredictImage");

IPredictionEndpoint client = GetPredictionEndpointClient(HttpMockServer.CreateInstance());
using (FileStream stream = new FileStream(Path.Combine("TestImages", "test_image.jpg"), FileMode.Open))
{
ImagePredictionResultModel results = client.PredictImageAsync(ProjectId, stream).Result;
ValidateResults(results);
}
}
}

[Fact]
public void PredictImageNoStore()
{
using (MockContext context = MockContext.Start(this.GetType().Name))
{
HttpMockServer.Initialize(this.GetType().Name, "PredictImageNoStore");

IPredictionEndpoint client = GetPredictionEndpointClient(HttpMockServer.CreateInstance());
using (FileStream stream = new FileStream(Path.Combine("TestImages", "test_image.jpg"), FileMode.Open))
{
ImagePredictionResultModel results = client.PredictImageWithNoStoreAsync(ProjectId, stream).Result;
ValidateResults(results);
}
}
}


[Fact]
public void PredictImageUrl()
{
string testImageUrl = "https://raw.githubusercontent.com/Microsoft/Cognitive-CustomVision-Windows/master/Samples/Images/Test/test_image.jpg";
using (MockContext context = MockContext.Start(this.GetType().Name))
{
HttpMockServer.Initialize(this.GetType().Name, "PredictImageUrl");

IPredictionEndpoint client = GetPredictionEndpointClient(HttpMockServer.CreateInstance());
ImageUrl url = new ImageUrl(testImageUrl);

ImagePredictionResultModel results = client.PredictImageUrlAsync(ProjectId, url).Result;
ValidateResults(results);
}
}

[Fact]
public void PredictImageUrlNoStore()
{
string testImageUrl = "https://raw.githubusercontent.com/Microsoft/Cognitive-CustomVision-Windows/master/Samples/Images/Test/test_image.jpg";
using (MockContext context = MockContext.Start(this.GetType().Name))
{
HttpMockServer.Initialize(this.GetType().Name, "PredictImageUrlNoStore");

IPredictionEndpoint client = GetPredictionEndpointClient(HttpMockServer.CreateInstance());
ImageUrl url = new ImageUrl(testImageUrl);

ImagePredictionResultModel results = client.PredictImageUrlWithNoStoreAsync(ProjectId, url).Result;
ValidateResults(results);
}
}

private static void ValidateResults(ImagePredictionResultModel results)
{
Assert.Equal(ProjectId, results.Project);
Assert.Equal(Guid.Parse("015deca4-aa39-4d20-81fe-73de4efd6acf"), results.Iteration);
Assert.Equal(2, results.Predictions.Count);
Assert.Equal("Hemlock", results.Predictions[0].Tag);
Assert.Equal(1, results.Predictions[0].Probability);
Assert.Equal(Guid.Parse("f7304b5d-0318-4a29-b98c-114c6f90c81d"), results.Predictions[0].TagId);
Assert.Equal("Japanese Cherry", results.Predictions[1].Tag);
Assert.InRange(results.Predictions[1].Probability, 0, 1e-6);
Assert.Equal(Guid.Parse("5408cebc-c28d-4578-8515-7a4718f5e0d3"), results.Predictions[1].TagId);
}
}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"Entries": [
{
"RequestUri": "/customvision/v1.1/Prediction/e222c033-5f5d-4a23-bde9-8343f19c0a01/url",
"EncodedRequestUri": "L2N1c3RvbXZpc2lvbi92MS4xL1ByZWRpY3Rpb24vZTIyMmMwMzMtNWY1ZC00YTIzLWJkZTktODM0M2YxOWMwYTAxL3VybA==",
"RequestMethod": "POST",
"RequestBody": "{\r\n \"Url\": \"https://raw.githubusercontent.com/Microsoft/Cognitive-CustomVision-Windows/master/Samples/Images/Test/test_image.jpg\"\r\n}",
"RequestHeaders": {
"Content-Type": [
"application/json; charset=utf-8"
],
"Content-Length": [
"133"
],
"Prediction-Key": [
""
],
"User-Agent": [
"FxVersion/4.6.26011.01",
"Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.PredictionEndpoint/1.0.0.0"
]
},
"ResponseBody": "{\r\n \"Id\": \"e880a5ec-1ed4-461a-9813-c9c94997bd12\",\r\n \"Project\": \"e222c033-5f5d-4a23-bde9-8343f19c0a01\",\r\n \"Iteration\": \"015deca4-aa39-4d20-81fe-73de4efd6acf\",\r\n \"Created\": \"2018-02-22T05:18:58.5724905Z\",\r\n \"Predictions\": [\r\n {\r\n \"TagId\": \"f7304b5d-0318-4a29-b98c-114c6f90c81d\",\r\n \"Tag\": \"Hemlock\",\r\n \"Probability\": 1.0\r\n },\r\n {\r\n \"TagId\": \"5408cebc-c28d-4578-8515-7a4718f5e0d3\",\r\n \"Tag\": \"Japanese Cherry\",\r\n \"Probability\": 3.60627153E-12\r\n }\r\n ]\r\n}",
"ResponseHeaders": {
"Content-Length": [
"387"
],
"Content-Type": [
"application/json; charset=utf-8"
],
"Date": [
"Thu, 22 Feb 2018 05:18:58 GMT"
],
"apim-request-id": [
"c0d2916c-91de-4eb0-9c78-1baf6bc39356"
],
"Strict-Transport-Security": [
"max-age=31536000; includeSubDomains; preload"
],
"x-content-type-options": [
"nosniff"
]
},
"StatusCode": 200
}
],
"Names": {},
"Variables": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"Entries": [
{
"RequestUri": "/customvision/v1.1/Prediction/e222c033-5f5d-4a23-bde9-8343f19c0a01/url/nostore",
"EncodedRequestUri": "L2N1c3RvbXZpc2lvbi92MS4xL1ByZWRpY3Rpb24vZTIyMmMwMzMtNWY1ZC00YTIzLWJkZTktODM0M2YxOWMwYTAxL3VybC9ub3N0b3Jl",
"RequestMethod": "POST",
"RequestBody": "{\r\n \"Url\": \"https://raw.githubusercontent.com/Microsoft/Cognitive-CustomVision-Windows/master/Samples/Images/Test/test_image.jpg\"\r\n}",
"RequestHeaders": {
"Content-Type": [
"application/json; charset=utf-8"
],
"Content-Length": [
"133"
],
"Prediction-Key": [
""
],
"User-Agent": [
"FxVersion/4.6.26011.01",
"Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.PredictionEndpoint/1.0.0.0"
]
},
"ResponseBody": "{\r\n \"Id\": \"cec12479-70b6-4c2b-a78c-5574338f288a\",\r\n \"Project\": \"e222c033-5f5d-4a23-bde9-8343f19c0a01\",\r\n \"Iteration\": \"015deca4-aa39-4d20-81fe-73de4efd6acf\",\r\n \"Created\": \"2018-02-22T05:22:17.1888743Z\",\r\n \"Predictions\": [\r\n {\r\n \"TagId\": \"f7304b5d-0318-4a29-b98c-114c6f90c81d\",\r\n \"Tag\": \"Hemlock\",\r\n \"Probability\": 1.0\r\n },\r\n {\r\n \"TagId\": \"5408cebc-c28d-4578-8515-7a4718f5e0d3\",\r\n \"Tag\": \"Japanese Cherry\",\r\n \"Probability\": 3.60627153E-12\r\n }\r\n ]\r\n}",
"ResponseHeaders": {
"Content-Length": [
"387"
],
"Content-Type": [
"application/json; charset=utf-8"
],
"Date": [
"Thu, 22 Feb 2018 05:22:17 GMT"
],
"apim-request-id": [
"3e485211-2411-4403-89af-057f3b1b690b"
],
"Strict-Transport-Security": [
"max-age=31536000; includeSubDomains; preload"
],
"x-content-type-options": [
"nosniff"
]
},
"StatusCode": 200
}
],
"Names": {},
"Variables": {}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.16
MinimumVisualStudioVersion = 10.0.40219.1
Project("{6558086F-5F69-4793-B2C5-7300B2510100}") = "Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction", "Prediction\Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.csproj", "{338BB7D5-245A-4508-BA7D-71B37A254367}"
EndProject
Project("{6558086F-5F69-4793-B2C5-7300B2510100}") = "Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Tests", "Prediction.Tests\Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction.Tests.csproj", "{F47DEF2A-CD64-4CB6-A074-6931B31FA253}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{338BB7D5-245A-4508-BA7D-71B37A254367}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{338BB7D5-245A-4508-BA7D-71B37A254367}.Debug|Any CPU.Build.0 = Debug|Any CPU
{338BB7D5-245A-4508-BA7D-71B37A254367}.Release|Any CPU.ActiveCfg = Release|Any CPU
{338BB7D5-245A-4508-BA7D-71B37A254367}.Release|Any CPU.Build.0 = Release|Any CPU
{F47DEF2A-CD64-4CB6-A074-6931B31FA253}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F47DEF2A-CD64-4CB6-A074-6931B31FA253}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F47DEF2A-CD64-4CB6-A074-6931B31FA253}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F47DEF2A-CD64-4CB6-A074-6931B31FA253}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Loading