Exemplo n.º 1
0
        public async Task <FaceToNameMatchResult> MatchFaceToNameAsync(
            Stream photo,
            string countryCode,
            int maxSimilarFaces)
        {
            Guard.Argument(photo, nameof(photo)).NotNull();
            Guard.Argument(countryCode, nameof(countryCode)).NotNull().NotWhiteSpace();
            Guard.Argument(maxSimilarFaces, nameof(maxSimilarFaces)).InRange(1, 1000);

            DetectedFace face = await this.DetectSingleFaceAsync(photo).ConfigureAwait(false);

            FaceCategory category = GetFaceCategory(face, countryCode);

            using (this.logger.BeginScope(
                       new Dictionary <string, object> {
                { "gender", category.Gender }
            }))
            {
                IEnumerable <FaceToNameMatch> matches
                    = await this.MatchFaceToNameAsync(face, category, maxSimilarFaces).ConfigureAwait(false);

                FaceToNameMatchResult result = new FaceToNameMatchResult(category);
                result.Matches.AddRange(matches);
                return(result);
            }
        }
Exemplo n.º 2
0
        public async Task OnPostAsync_UserImage_Success_NormalizeResults()
        {
            // Arrange
            IndexModel unitUnderTest = this.CreateIndexModel();

            unitUnderTest.CountryCode    = "ru";
            unitUnderTest.ServerImageUrl = null;
            unitUnderTest.UserImage      = this.mockUserImage.Object;
            this.SetupMockUserImage(this.mockUserImage);

            FaceToNameMatchResult fakeResult =
                new FaceToNameMatchResult(new FaceCategory("ru", FaceGender.Female));

            fakeResult.Matches.AddRange(new[]
            {
                new FaceToNameMatch("First", 0.20),
                new FaceToNameMatch("Second", 0.80)
            });

            this.mockFaceMatcher
            .Setup(mock => mock.MatchFaceToNameAsync(It.IsAny <Stream>(), It.IsAny <string>(), It.IsAny <int>()))
            .Returns(Task.FromResult(fakeResult));

            // Act
            await unitUnderTest.OnPostAsync();

            // Assert
            unitUnderTest.Matches[0].FirstName.ShouldBe("Second");
            unitUnderTest.Matches[0].Score.ShouldBe(60.0);
            unitUnderTest.Matches[1].FirstName.ShouldBe("First");
            unitUnderTest.Matches[1].Score.ShouldBe(40.0);
        }
Exemplo n.º 3
0
        public async Task WhatsYourFace(MatchFaceToNamesCmd commandArgs)
        {
            var cmd = this.serviceProvider.GetRequiredService <MatchFaceToNamesCmd>();
            FaceToNameMatchResult result = await cmd.ExecuteAsync(commandArgs);

            this.console.WriteObject(result);
        }
Exemplo n.º 4
0
        public void WriteObject(FaceToNameMatchResult result)
        {
            Console.WriteLine($"{result.Category.CountryCode}, {result.Category.Gender}");

            foreach (FaceToNameMatch score in result.Matches)
            {
                Console.WriteLine($"{score.FirstName}\t{score.Score}");
            }
        }
Exemplo n.º 5
0
        public async Task OnPostAsync_UserImage_Success()
        {
            // Arrange
            const string CountryCode   = "ro";
            IndexModel   unitUnderTest = this.CreateIndexModel();

            unitUnderTest.CountryCode    = CountryCode;
            unitUnderTest.ServerImageUrl = null;
            unitUnderTest.UserImage      = this.mockUserImage.Object;
            this.SetupMockUserImage(this.mockUserImage);

            MemoryStream fakeImageStream = new MemoryStream();

            this.mockUserImage.Setup(mock => mock.OpenReadStream()).Returns(fakeImageStream);

            FaceToNameMatchResult fakeResult =
                new FaceToNameMatchResult(new FaceCategory(CountryCode, FaceGender.Male));

            fakeResult.Matches.AddRange(new[]
            {
                new FaceToNameMatch("Florin", 69.5),
                new FaceToNameMatch("Tiberiu", 28.5)
            });

            this.mockFaceMatcher
            .Setup(mock => mock.MatchFaceToNameAsync(
                       fakeImageStream, CountryCode, this.fakeFaceMatchSettings.MaxNumberOfFaces))
            .Returns(Task.FromResult(fakeResult));

            // Act
            await unitUnderTest.OnPostAsync();

            // Assert
            unitUnderTest.Matches.ShouldNotBeNull();
            unitUnderTest.Matches.Count.ShouldBe(2);
            unitUnderTest.Matches[0].FirstName.ShouldBe("Florin");
            unitUnderTest.Matches[0].Score.ShouldNotBe(0);
            unitUnderTest.Matches[1].FirstName.ShouldBe("Tiberiu");
            unitUnderTest.Matches[1].Score.ShouldNotBe(0);
            unitUnderTest.ErrorMessage.ShouldBeNull();
        }
Exemplo n.º 6
0
        public async Task MatchFaceToNameAsync_Success()
        {
            // Arrange
            var    unitUnderTest   = this.CreateFaceMatcher();
            Stream photo           = new MemoryStream();
            string countryCode     = "ro";
            int    maxSimilarFaces = 10;

            Guid         fakeFaceId       = Guid.NewGuid();
            DetectedFace fakeDetectedFace = new DetectedFace
            {
                FaceId         = fakeFaceId,
                FaceAttributes = new FaceAttributes(gender: Gender.Female)
            };
            var detectFaceResponse = new HttpOperationResponse <IList <DetectedFace> >
            {
                Body = new[] { fakeDetectedFace }
            };

            this.mockFaceOperations
            .Setup(mock => mock.DetectWithStreamWithHttpMessagesAsync(
                       photo,
                       true,  // returnFaceId
                       false, // returnFaceLandmarks
                       It.Is <IList <FaceAttributeType> >(list => list.Count == 1 && list.Contains(FaceAttributeType.Gender)),
                       null,  // customeHeaders
                       It.IsAny <System.Threading.CancellationToken>()))
            .Returns(Task.FromResult(detectFaceResponse));

            string fakeFaceListId       = "faces-ro-female";
            Guid   fakePersistedFaceId1 = Guid.Parse("11110000-1100-1100-1100-111111000000");
            Guid   fakePersistedFaceId2 = Guid.Parse("22220000-2200-2200-2200-222222000000");
            var    fakeSimilarFaces     = new[]
            {
                new SimilarFace()
                {
                    PersistedFaceId = fakePersistedFaceId1, Confidence = 0.8765
                },
                new SimilarFace()
                {
                    PersistedFaceId = fakePersistedFaceId2, Confidence = 0.4321
                },
            };

            var findSimilarResponse = new HttpOperationResponse <IList <SimilarFace> >
            {
                Body = fakeSimilarFaces
            };

            this.mockFaceOperations
            .Setup(mock => mock.FindSimilarWithHttpMessagesAsync(
                       fakeFaceId,
                       fakeFaceListId,
                       null, // largeFaceListId
                       null, // faceIds
                       maxSimilarFaces,
                       FindSimilarMatchMode.MatchFace,
                       null, // customheaders
                       It.IsAny <System.Threading.CancellationToken>()))
            .Returns(Task.FromResult(findSimilarResponse));

            var category = new FaceCategory(countryCode, FaceGender.Female);

            this.mockFaceIdLookup
            .Setup(mock => mock.LookupName(fakePersistedFaceId1, category))
            .Returns("Maria");
            this.mockFaceIdLookup
            .Setup(mock => mock.LookupName(fakePersistedFaceId2, category))
            .Returns("Madalina");

            // Act
            FaceToNameMatchResult result = await unitUnderTest.MatchFaceToNameAsync(
                photo,
                countryCode,
                maxSimilarFaces);

            // Assert
            result.ShouldNotBeNull();
            result.Category.ShouldNotBeNull();
            result.Category.CountryCode.ShouldBe("ro");
            result.Category.Gender.ShouldBe(FaceGender.Female);
            result.Matches.ShouldNotBeNull();
            result.Matches.Count.ShouldBe(2);
            result.Matches[0].FirstName.ShouldBe("Maria");
            result.Matches[0].Score.ShouldBe(0.8765);
            result.Matches[1].FirstName.ShouldBe("Madalina");
            result.Matches[1].Score.ShouldBe(0.4321);
        }