Skip to content

Commit

Permalink
[Cogs Face] Update Face SDK‘s auto-generated code to 3.0.0-preview wi…
Browse files Browse the repository at this point in the history
…th latest swagger.

[Cogs Face] Update Face SDK‘s auto-generated code to 3.0.0-preview with latest swagger.
  • Loading branch information
dsgouda authored Aug 3, 2018
2 parents d372366 + 4227ce3 commit b3dafdb
Show file tree
Hide file tree
Showing 57 changed files with 14,716 additions and 2,094 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using Microsoft.Azure.CognitiveServices.Vision.Face;
using System.Net.Http;
using Face = Microsoft.Azure.CognitiveServices.Vision.Face.Models;

namespace FaceSDK.Tests
{
public abstract class BaseTests
{
public static bool IsTestTenant = false;
private static readonly string FaceSubscriptionKey;

static BaseTests()
{
// Retrieve the configuration information.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void FaceDetectionWithAttributes()
Assert.True(face.FaceAttributes.Glasses == GlassesType.ReadingGlasses);
Assert.False(face.FaceAttributes.Makeup.EyeMakeup);
Assert.False(face.FaceAttributes.Makeup.LipMakeup);
Assert.True(face.FaceAttributes.Emotion.Neutral > 0.9);
Assert.True(face.FaceAttributes.Emotion.Neutral > 0.5);
Assert.True(face.FaceAttributes.Occlusion.ForeheadOccluded);
Assert.False(face.FaceAttributes.Occlusion.EyeOccluded);
Assert.False(face.FaceAttributes.Occlusion.MouthOccluded);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
using Microsoft.Azure.CognitiveServices.Vision.Face;
using Microsoft.Azure.CognitiveServices.Vision.Face.Models;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Xunit;

namespace FaceSDK.Tests
{
public class FaceFindSimilarTests : BaseTests
{
[Fact]
public void FaceFindSimilarFacePositive()
{
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
HttpMockServer.Initialize(this.GetType().FullName, "FaceFindSimilarFacePositive");

IFaceClient client = GetFaceClient(HttpMockServer.CreateInstance());
Guid? faceId1 = null;
var faceIds = new List<Guid?>();
var satyaFaceIds = AddFaceArrayFace(client, "Satya");
var gatesFaceIds = AddFaceArrayFace(client, "Gates");
faceIds.AddRange(satyaFaceIds);
faceIds.AddRange(gatesFaceIds);

using (FileStream stream = new FileStream(Path.Combine("TestImages", "Satya4.jpg"), FileMode.Open))
{
faceId1 = client.Face.DetectWithStreamAsync(stream, true).Result[0].FaceId;
Assert.NotNull(faceId1);
}

IList<SimilarFace> findSimilarResults = client.Face.FindSimilarAsync(faceId1.Value, faceIds: faceIds).Result;
Assert.True(findSimilarResults.Count > 0);
Assert.Contains(findSimilarResults[0].FaceId, satyaFaceIds);
Assert.True(findSimilarResults[0].Confidence > 0.5);
}
}

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

IFaceClient client = GetFaceClient(HttpMockServer.CreateInstance());
Guid? faceId1 = null;
string faceListId = "face-list-id";
client.FaceList.CreateAsync(faceListId, "fakeFaceList").Wait();
try
{
var satyaPersistedFaceIds = AddFaceListFace(client, faceListId, "Satya");
var gatesPersistedFaceIds = AddFaceListFace(client, faceListId, "Gates");

using (FileStream stream = new FileStream(Path.Combine("TestImages", "Satya4.jpg"), FileMode.Open))
{
faceId1 = client.Face.DetectWithStreamAsync(stream, true).Result[0].FaceId;
Assert.NotNull(faceId1);
}

IList<SimilarFace> findSimilarResults = client.Face.FindSimilarAsync(faceId1.Value, faceListId: faceListId).Result;
Assert.True(findSimilarResults.Count > 0);
Assert.Contains(findSimilarResults[0].PersistedFaceId, satyaPersistedFaceIds);
Assert.True(findSimilarResults[0].Confidence > 0.5);
}
finally
{
client.FaceList.DeleteAsync(faceListId).Wait();
}
}
}

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

IFaceClient client = GetFaceClient(HttpMockServer.CreateInstance());
Guid? faceId1 = null;
string largeFaceListId = "large-face-list-id";
client.LargeFaceList.CreateAsync(largeFaceListId, "fakeLargeFaceList").Wait();
try
{
var satyaPersistedFaceIds = AddLargeFaceListFace(client, largeFaceListId, "Satya");
var gatesPersistedFaceIds = AddLargeFaceListFace(client, largeFaceListId, "Gates");
client.LargeFaceList.TrainAsync(largeFaceListId).Wait();

var trainingStatus = WaitForTraining(client, largeFaceListId);
Assert.Equal(TrainingStatusType.Succeeded, trainingStatus.Status);

using (FileStream stream = new FileStream(Path.Combine("TestImages", "Satya4.jpg"), FileMode.Open))
{
faceId1 = client.Face.DetectWithStreamAsync(stream, true).Result[0].FaceId;
Assert.NotNull(faceId1);
}

IList<SimilarFace> findSimilarResults = client.Face.FindSimilarAsync(faceId1.Value, largeFaceListId: largeFaceListId).Result;
Assert.True(findSimilarResults.Count > 0);
Assert.Contains(findSimilarResults[0].PersistedFaceId, satyaPersistedFaceIds);
Assert.True(findSimilarResults[0].Confidence > 0.5);
}
finally
{
client.LargeFaceList.DeleteAsync(largeFaceListId).Wait();
}
}
}

private List<Guid?> AddFaceListFace(IFaceClient client, string faceListId, string fileName)
{
var persistedFaceIds = new List<Guid?>();
for (int i = 1; i < 4; i++)
{
DetectedFace face = null;
using (FileStream stream = new FileStream(Path.Combine("TestImages", fileName + i + ".jpg"), FileMode.Open))
{
face = client.Face.DetectWithStreamAsync(stream, true).Result[0];
}

using (FileStream stream = new FileStream(Path.Combine("TestImages", fileName + i + ".jpg"), FileMode.Open))
{
var persistedFace = client.FaceList.AddFaceFromStreamAsync(faceListId, stream, null, new List<int>{
face.FaceRectangle.Left,
face.FaceRectangle.Top,
face.FaceRectangle.Width,
face.FaceRectangle.Height }).Result;

persistedFaceIds.Add(persistedFace.PersistedFaceId);
}
}

return persistedFaceIds;
}

private List<Guid?> AddLargeFaceListFace(IFaceClient client, string largeFaceListId, string fileName)
{
var persistedFaceIds = new List<Guid?>();
for (int i = 1; i < 4; i++)
{
DetectedFace face = null;
using (FileStream stream = new FileStream(Path.Combine("TestImages", fileName + i + ".jpg"), FileMode.Open))
{
face = client.Face.DetectWithStreamAsync(stream, true).Result[0];
}

using (FileStream stream = new FileStream(Path.Combine("TestImages", fileName + i + ".jpg"), FileMode.Open))
{
var persistedFace = client.LargeFaceList.AddFaceFromStreamAsync(largeFaceListId, stream, null, new List<int>{
face.FaceRectangle.Left,
face.FaceRectangle.Top,
face.FaceRectangle.Width,
face.FaceRectangle.Height }).Result;

persistedFaceIds.Add(persistedFace.PersistedFaceId);
}
}

return persistedFaceIds;
}

private List<Guid?> AddFaceArrayFace(IFaceClient client, string fileName)
{
var faceIdList = new List<Guid?>();
for (int i = 1; i < 4; i++)
{
DetectedFace face = null;
using (FileStream stream = new FileStream(Path.Combine("TestImages", fileName + i + ".jpg"), FileMode.Open))
{
face = client.Face.DetectWithStreamAsync(stream).Result[0];
}

faceIdList.Add(face.FaceId);
}

return faceIdList;
}

private TrainingStatus WaitForTraining(
IFaceClient client,
string largeFaceListId,
int timeIntervalInMilliSeconds = 1000)
{
var trainingStatus = client.LargeFaceList.GetTrainingStatusAsync(largeFaceListId).Result;

while (trainingStatus?.Status != null
&& !trainingStatus.Status.Equals(TrainingStatusType.Succeeded)
&& !trainingStatus.Status.Equals(TrainingStatusType.Failed))
{
Thread.Sleep(timeIntervalInMilliSeconds);

trainingStatus = client.LargeFaceList.GetTrainingStatusAsync(largeFaceListId).Result;
}

return trainingStatus;
}
}
}
Loading

0 comments on commit b3dafdb

Please sign in to comment.