Exemplo n.º 1
0
        public async void CreateImagesFromFiles()
        {
            var dataFileName = "hemlock_1.jpg";

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                HttpMockServer.Initialize(this.GetType(), "CreateImagesFromFiles", RecorderMode);

                using (ICustomVisionTrainingClient client = GetTrainingClient())
                {
                    var newProject           = client.CreateProjectAsync(projName, projDescription, ProjectBuilderHelper.FoodDomain).Result;
                    var tag                  = client.CreateTagAsync(newProject.Id, tagName).Result;
                    var fileName             = Path.Combine("TestImages", "tag1", dataFileName);
                    var fileImages           = new ImageFileCreateEntry[] { new ImageFileCreateEntry(dataFileName, File.ReadAllBytes(fileName)) };
                    var tags                 = new Guid[] { tag.Id };
                    var imageCreatedFromFile = client.CreateImagesFromFilesAsync(newProject.Id, new ImageFileCreateBatch(fileImages, tags)).Result;

                    Assert.True(imageCreatedFromFile.IsBatchSuccessful);
                    Assert.Equal(1, imageCreatedFromFile.Images.Count);
                    Assert.Contains(dataFileName, imageCreatedFromFile.Images[0].SourceUrl);
                    Assert.Equal("OK", imageCreatedFromFile.Images[0].Status);
                    Assert.NotEqual(Guid.Empty, imageCreatedFromFile.Images[0].Image.Id);
                    Assert.NotEqual(0, imageCreatedFromFile.Images[0].Image.Width);
                    Assert.NotEqual(0, imageCreatedFromFile.Images[0].Image.Height);
                    Assert.NotEmpty(imageCreatedFromFile.Images[0].Image.OriginalImageUri);
                    Assert.NotEmpty(imageCreatedFromFile.Images[0].Image.ResizedImageUri);
                    Assert.NotEmpty(imageCreatedFromFile.Images[0].Image.ThumbnailUri);

                    await client.DeleteProjectAsync(newProject.Id);
                }
            }
        }
Exemplo n.º 2
0
        public async void QuerySuggestedImageCount()
        {
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                HttpMockServer.Initialize(this.GetType(), "QuerySuggestedImageCount", RecorderMode);

                using (var project = CreateTrainedImageClassificationProject())
                    using (ICustomVisionTrainingClient client = BaseTests.GetTrainingClient())
                    {
                        // Add an untagged image we expect to be classified as tag1
                        var images = new ImageFileCreateEntry[] {
                            new ImageFileCreateEntry("suggest_ic.jpg", File.ReadAllBytes(Path.Combine("TestImages", "suggest_ic.jpg")))
                        };
                        var imageResult = await client.CreateImagesFromFilesAsync(project.ProjectId, new ImageFileCreateBatch(images));

                        Assert.True(imageResult.IsBatchSuccessful);
                        Assert.Equal(1, imageResult.Images.Count);

                        // Ask for suggestions
                        var suggestions = client.SuggestTagsAndRegions(project.ProjectId, project.IterationId, new Guid[] { imageResult.Images[0].Image.Id });

                        // Validate result
                        Assert.Equal(1, suggestions.Count);

                        var tags = await client.GetTagsAsync(project.ProjectId, project.IterationId);

                        // We expect to get Tag1 as the primary suggestion, so query with a high prediction
                        var countMapping = await client.QuerySuggestedImageCountAsync(project.ProjectId, project.IterationId, new TagFilter()
                        {
                            Threshold = 0.75,
                            TagIds    = new Guid[] { tags[0].Id }
                        });

                        Assert.Equal(1, countMapping.Count);
                        Assert.Equal(1, countMapping[tags[0].Id.ToString()]);

                        // We expect to get Tag2 to have a low probabilty, make sure we don't find it with a high threshold
                        countMapping = await client.QuerySuggestedImageCountAsync(project.ProjectId, project.IterationId, new TagFilter()
                        {
                            Threshold = 0.75,
                            TagIds    = new Guid[] { tags[1].Id }
                        });

                        Assert.Equal(1, countMapping.Count);
                        Assert.Equal(0, countMapping[tags[1].Id.ToString()]);

                        // Get results for all tags with a high threshold.
                        countMapping = await client.QuerySuggestedImageCountAsync(project.ProjectId, project.IterationId, new TagFilter()
                        {
                            Threshold = 0.75,
                            TagIds    = tags.Select(t => t.Id).ToList()
                        });

                        Assert.Equal(2, countMapping.Count);
                        Assert.Equal(1, countMapping[tags[0].Id.ToString()]);
                        Assert.Equal(0, countMapping[tags[1].Id.ToString()]);
                    }
            }
        }
Exemplo n.º 3
0
        public async void QuerySuggestedImages()
        {
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                HttpMockServer.Initialize(this.GetType(), "QuerySuggestedImages", RecorderMode);

                using (var project = CreateTrainedImageClassificationProject())
                    using (ICustomVisionTrainingClient client = BaseTests.GetTrainingClient())
                    {
                        // Add an untagged image we expect to be classified as tag1
                        var images = new ImageFileCreateEntry[] {
                            new ImageFileCreateEntry("suggest_ic.jpg", File.ReadAllBytes(Path.Combine("TestImages", "suggest_ic.jpg")))
                        };
                        var imageResult = await client.CreateImagesFromFilesAsync(project.ProjectId, new ImageFileCreateBatch(images));

                        Assert.True(imageResult.IsBatchSuccessful);
                        Assert.Equal(1, imageResult.Images.Count);

                        // Ask for suggestions
                        var suggestions = client.SuggestTagsAndRegions(project.ProjectId, project.IterationId, new Guid[] { imageResult.Images[0].Image.Id });

                        // Validate result
                        Assert.Equal(1, suggestions.Count);

                        // Get tags and build a query
                        var tags = await client.GetTagsAsync(project.ProjectId, project.IterationId);

                        var query = new SuggestedTagAndRegionQueryToken()
                        {
                            MaxCount  = 10,
                            TagIds    = tags.Select(t => t.Id).ToList(),
                            Threshold = 0
                        };

                        // This will return all suggested images (1 in this case)
                        var suggestedImages = await client.QuerySuggestedImagesAsync(project.ProjectId, project.IterationId, query);

                        Assert.Equal(1, suggestedImages.Results.Count);
                    }
            }
        }
Exemplo n.º 4
0
        public async void SuggestTagAndRegions()
        {
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                HttpMockServer.Initialize(this.GetType(), "SuggestTagAndRegions", RecorderMode);

                using (var project = CreateTrainedImageClassificationProject())
                    using (ICustomVisionTrainingClient client = BaseTests.GetTrainingClient())
                    {
                        // Add an untagged image we expect to be classified as tag1
                        var images = new ImageFileCreateEntry[] {
                            new ImageFileCreateEntry("suggest_ic.jpg", File.ReadAllBytes(Path.Combine("TestImages", "suggest_ic.jpg")))
                        };
                        var imageResult = await client.CreateImagesFromFilesAsync(project.ProjectId, new ImageFileCreateBatch(images));

                        Assert.True(imageResult.IsBatchSuccessful);
                        Assert.Equal(1, imageResult.Images.Count);

                        // Ask for suggestions
                        var suggestions = client.SuggestTagsAndRegions(project.ProjectId, project.IterationId, new Guid[] { imageResult.Images[0].Image.Id });

                        // Validate result
                        Assert.Equal(1, suggestions.Count);
                        Assert.Equal(project.ProjectId, suggestions[0].Project);
                        Assert.Equal(project.IterationId, suggestions[0].Iteration);
                        Assert.NotEqual(0, suggestions[0].Predictions.Count);

                        foreach (var prediction in suggestions[0].Predictions)
                        {
                            var tag = await client.GetTagAsync(project.ProjectId, prediction.TagId, project.IterationId);

                            Assert.Equal(tag.Name, prediction.TagName);
                            Assert.Equal(tag.Id, prediction.TagId);
                            Assert.InRange(prediction.Probability, 0, 1);
                        }

                        // We expect Tag1 to have the highest probability
                        Assert.Equal("Tag1", suggestions[0].Predictions.OrderByDescending(p => p.Probability).First().TagName);
                    }
            }
        }