Ejemplo n.º 1
0
        public async void CreateImageFromUrl()
        {
            string imageUrl = "https://raw.githubusercontent.com/Microsoft/Cognitive-CustomVision-Windows/master/Samples/Images/Hemlock/hemlock_1.jpg";

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

                using (ICustomVisionTrainingClient client = GetTrainingClient())
                {
                    var newProject          = client.CreateProjectAsync(projName, projDescription, ProjectBuilderHelper.FoodDomain).Result;
                    var tag                 = client.CreateTagAsync(newProject.Id, tagName).Result;
                    var urlImages           = new ImageUrlCreateEntry[] { new ImageUrlCreateEntry(imageUrl) };
                    var tags                = new Guid[] { tag.Id };
                    var imageCreatedFromUrl = client.CreateImagesFromUrlsAsync(newProject.Id, new ImageUrlCreateBatch(urlImages, tags)).Result;

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

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

                using (ICustomVisionTrainingClient client = GetTrainingClient())
                {
                    var newProject = await client.CreateProjectAsync("TagRetrievalTest", projDescription, ProjectBuilderHelper.GeneralDomain);

                    var numTags = 4;

                    for (var i = 0; i < numTags; i++)
                    {
                        await client.CreateTagAsync(newProject.Id, "Tag #" + i.ToString(), string.Empty);
                    }

                    var tagList = await client.GetTagsAsync(newProject.Id);

                    Assert.Equal(numTags, tagList.Count);

                    foreach (var tag in tagList)
                    {
                        Assert.NotEqual(Guid.Empty, tag.Id);
                        Assert.Contains("Tag #", tag.Name);
                        Assert.Null(tag.Description);
                        Assert.Equal(0, tag.ImageCount);
                    }

                    await client.DeleteProjectAsync(newProject.Id);
                }
            }
        }
Ejemplo n.º 3
0
        public async void CreateImagesFromData()
        {
            var dataFileName = "hemlock_1.jpg";

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

                using (ICustomVisionTrainingClient client = GetTrainingClient())
                {
                    var newProject = client.CreateProjectAsync(projName, projDescription, ProjectBuilderHelper.FoodDomain).Result;
                    var tag        = client.CreateTagAsync(newProject.Id, tagName).Result;
                    using (FileStream stream = new FileStream(Path.Combine("TestImages", "tag1", dataFileName), FileMode.Open))
                    {
                        var imageCreatedFromData = client.CreateImagesFromDataAsync(newProject.Id, stream, new Guid[] { tag.Id }).Result;

                        Assert.True(imageCreatedFromData.IsBatchSuccessful);
                        Assert.Equal(1, imageCreatedFromData.Images.Count);
                        Assert.Contains(dataFileName, imageCreatedFromData.Images[0].SourceUrl);
                        Assert.Equal("OK", imageCreatedFromData.Images[0].Status);
                        Assert.NotEqual(Guid.Empty, imageCreatedFromData.Images[0].Image.Id);
                        Assert.NotEqual(0, imageCreatedFromData.Images[0].Image.Width);
                        Assert.NotEqual(0, imageCreatedFromData.Images[0].Image.Height);
                        Assert.NotEmpty(imageCreatedFromData.Images[0].Image.OriginalImageUri);
                        Assert.NotEmpty(imageCreatedFromData.Images[0].Image.ResizedImageUri);
                        Assert.NotEmpty(imageCreatedFromData.Images[0].Image.ThumbnailUri);
                    }
                    await client.DeleteProjectAsync(newProject.Id);
                }
            }
        }
Ejemplo n.º 4
0
        public async void CreateImagesFromFiles()
        {
            var dataFileName = "hemlock_1.jpg";

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

                ICustomVisionTrainingClient client = GetTrainingClient();
                var newProject           = client.CreateProjectAsync(projName, projDescription, 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);
            }
        }
Ejemplo n.º 5
0
        public async void RegionManipulation()
        {
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                HttpMockServer.Initialize(this.GetType(), "RegionManipulation", RecorderMode);

                using (var project = CreateTrainedObjDetectionProject())
                    using (ICustomVisionTrainingClient client = BaseTests.GetTrainingClient())
                    {
                        var existingImage = (await client.GetTaggedImagesAsync(project.ProjectId)).First();
                        Assert.NotNull(existingImage);

                        var testTag = await client.CreateTagAsync(project.ProjectId, tagName);

                        var proposal = await client.GetImageRegionProposalsAsync(project.ProjectId, existingImage.Id);

                        Assert.True(proposal.Proposals.Count > 0);
                        Assert.Equal(project.ProjectId, proposal.ProjectId);
                        Assert.Equal(existingImage.Id, proposal.ImageId);
                        Assert.InRange(proposal.Proposals[0].Confidence, 0, 1);

                        var bbox = proposal.Proposals[0].BoundingBox;
                        List <ImageRegionCreateEntry> newRegions = new List <ImageRegionCreateEntry>();
                        newRegions.Add(new ImageRegionCreateEntry(existingImage.Id, testTag.Id, bbox.Left, bbox.Top, bbox.Width, bbox.Height));

                        var regions = await client.CreateImageRegionsAsync(project.ProjectId, new ImageRegionCreateBatch(newRegions));

                        Assert.Equal(1, regions.Created.Count);
                        Assert.Equal(0, regions.Duplicated.Count);
                        Assert.Equal(0, regions.Exceeded.Count);
                        Assert.Equal(bbox.Top, regions.Created[0].Top);
                        Assert.Equal(bbox.Left, regions.Created[0].Left);
                        Assert.Equal(bbox.Width, regions.Created[0].Width);
                        Assert.Equal(bbox.Height, regions.Created[0].Height);
                        Assert.Equal(existingImage.Id, regions.Created[0].ImageId);
                        Assert.Equal(testTag.Id, regions.Created[0].TagId);
                        Assert.NotEqual(Guid.Empty, regions.Created[0].RegionId);

                        var image = await client.GetImagesByIdsAsync(project.ProjectId, new List <Guid>(new Guid[] { existingImage.Id }));

                        Assert.Equal(1, image.Count);
                        Assert.Equal(2, image[0].Regions.Count);

                        await client.DeleteImageRegionsAsync(project.ProjectId, new List <Guid>(new Guid[] { regions.Created[0].RegionId }));

                        image = await client.GetImagesByIdsAsync(project.ProjectId, new List <Guid>(new Guid[] { existingImage.Id }));

                        Assert.Equal(1, image.Count);
                        Assert.Equal(1, image[0].Regions.Count);

                        await client.DeleteTagAsync(project.ProjectId, testTag.Id);
                    }
            }
        }
Ejemplo n.º 6
0
        public async void CreateUpdateDeleteTag()
        {
            var updatedName        = "New Tag Name";
            var updatedDescription = "Updated Description";

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

                using (ICustomVisionTrainingClient client = GetTrainingClient())
                {
                    var projectId = (await client.CreateProjectAsync(projName)).Id;

                    var tag = await client.CreateTagAsync(projectId, tagName, tagDescription);

                    Assert.Equal(tagName, tag.Name);
                    Assert.Equal(tagDescription, tag.Description);
                    Assert.Equal(0, tag.ImageCount);
                    Assert.NotEqual(Guid.Empty, tag.Id);


                    var updatedTag = await client.UpdateTagAsync(projectId, tag.Id, new Tag()
                    {
                        Name        = updatedName,
                        Description = updatedDescription
                    });

                    Assert.Equal(updatedName, updatedTag.Name);
                    Assert.Equal(updatedDescription, updatedTag.Description);
                    Assert.Equal(tag.ImageCount, updatedTag.ImageCount);
                    Assert.Equal(tag.Id, updatedTag.Id);

                    await client.DeleteTagAsync(projectId, tag.Id);

                    await client.DeleteProjectAsync(projectId);
                }
            }
        }