예제 #1
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);
                    }
            }
        }