public void AddImages_Success()
        {
            IClient  client  = Substitute.For <IClient>();
            IRequest request = Substitute.For <IRequest>();

            client.PostAsync(Arg.Any <string>())
            .Returns(request);

            VisualRecognitionService service = new VisualRecognitionService(client);
            var versionDate = "versionDate";

            service.VersionDate = versionDate;

            var collectionId = "collectionId";
            var imagesFile   = new List <FileWithMetadata>()
            {
                new FileWithMetadata()
                {
                    Filename    = "filename",
                    ContentType = "contentType",
                    Data        = new System.IO.MemoryStream()
                }
            };
            var imageUrl = new List <string>()
            {
                "imageUrl0", "imageUrl1"
            };
            var trainingData = "trainingData";

            var result = service.AddImages(collectionId: collectionId, imagesFile: imagesFile, imageUrl: imageUrl, trainingData: trainingData);

            request.Received().WithArgument("version", versionDate);
            client.Received().PostAsync($"{service.ServiceUrl}/v4/collections/{collectionId}/images");
        }
Example #2
0
        public IEnumerator TestAddImage()
        {
            Log.Debug("VisualRecognitionServiceV4IntegrationTests", "Attempting to Add Images...");
            ImageDetailsList        imageDetailsList = null;
            List <FileWithMetadata> imagesFile       = new List <FileWithMetadata>();

            using (FileStream fs1 = File.OpenRead(giraffeImageFilepath), fs2 = File.OpenRead(turtleImageFilepath))
            {
                using (MemoryStream ms1 = new MemoryStream(), ms2 = new MemoryStream())
                {
                    fs1.CopyTo(ms1);
                    fs2.CopyTo(ms2);
                    FileWithMetadata fileWithMetadata = new FileWithMetadata()
                    {
                        Data        = ms1,
                        ContentType = "image/jpeg",
                        Filename    = Path.GetFileName(giraffeImageFilepath)
                    };
                    imagesFile.Add(fileWithMetadata);

                    FileWithMetadata fileWithMetadata2 = new FileWithMetadata()
                    {
                        Data        = ms2,
                        ContentType = "image/jpeg",
                        Filename    = Path.GetFileName(turtleImageFilepath)
                    };
                    imagesFile.Add(fileWithMetadata2);

                    service.AddImages(
                        callback: (DetailedResponse <ImageDetailsList> response, IBMError error) =>
                    {
                        Log.Debug("VisualRecognitionServiceV4IntegrationTests", "AddImages result: {0}", response.Response);
                        imageDetailsList = response.Result;
                        Assert.IsNotNull(imageDetailsList.Images);
                        Assert.IsTrue(imageDetailsList.Images.Count > 0);
                        Assert.IsNull(error);
                        imageId = imageDetailsList.Images[0].ImageId;
                    },
                        collectionId: collectionId,
                        imagesFile: imagesFile
                        );

                    while (imageDetailsList == null)
                    {
                        yield return(null);
                    }
                }
            }
        }
        public void AddImages()
        {
            IamAuthenticator authenticator = new IamAuthenticator(
                apikey: "{apikey}"
                );

            VisualRecognitionService service = new VisualRecognitionService("2019-02-11", authenticator);

            string trainingData = File.ReadAllText("training_data.json");
            var    result       = service.AddImages(
                collectionId: "60b4a98f-2472-4e2b-9c73-28bcaea6fa00",
                imageUrl: new List <string> {
                "https://watson-developer-cloud.github.io/doc-tutorial-downloads/visual-recognition/2018-Honda-Fit.jpg"
            },
                trainingData: trainingData
                );

            Console.WriteLine(result.Response);
        }
        public void Images_Success()
        {
            string testCollectionName        = "testCollection";
            string testCollectionDescription = ".NET test collection";

            service.WithHeader("X-Watson-Test", "1");
            var createCollectionResult = service.CreateCollection(
                name: Utility.ConvertToUtf8(testCollectionName),
                description: Utility.ConvertToUtf8(testCollectionDescription));

            var createdCollectionId = createCollectionResult.Result.CollectionId;

            DetailedResponse <ImageDetailsList> addImagesResult = null;
            List <FileWithMetadata>             imagesFile      = new List <FileWithMetadata>();

            using (FileStream fs = File.OpenRead(localGiraffePositiveExamplesFilePath))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    fs.CopyTo(ms);

                    FileWithMetadata fileWithMetadata = new FileWithMetadata()
                    {
                        Data        = ms,
                        ContentType = "application/zip",
                        Filename    = Path.GetFileName(localGiraffePositiveExamplesFilePath)
                    };
                    imagesFile.Add(fileWithMetadata);

                    service.WithHeader("X-Watson-Test", "1");
                    addImagesResult = service.AddImages(
                        collectionId: createdCollectionId,
                        imagesFile: imagesFile);
                }
            }
            var imageId = addImagesResult.Result.Images[0].ImageId;

            service.WithHeader("X-Watson-Test", "1");
            var addImageViaUrlResult = service.AddImages(
                collectionId: createdCollectionId,
                imageUrl: new List <string>()
            {
                dogImageUrl, catImageUrl
            });

            service.WithHeader("X-Watson-Test", "1");
            var listImageResult = service.ListImages(
                collectionId: createdCollectionId);

            service.WithHeader("X-Watson-Test", "1");
            var getImageResult = service.GetImageDetails(
                collectionId: createdCollectionId,
                imageId: imageId);

            service.WithHeader("X-Watson-Test", "1");
            var getJpgImageResult = service.GetJpegImage(
                collectionId: createdCollectionId,
                imageId: imageId,
                size: "thumbnail"
                );

            //  Save file
            using (FileStream fs = File.Create("giraffe.jpg"))
            {
                getJpgImageResult.Result.WriteTo(fs);
                fs.Close();
                getJpgImageResult.Result.Close();
            }

            service.WithHeader("X-Watson-Test", "1");
            var deleteImageResult = service.DeleteImage(
                collectionId: createdCollectionId,
                imageId: imageId);

            service.WithHeader("X-Watson-Test", "1");
            var deleteCollectionResult = service.DeleteCollection(
                collectionId: createdCollectionId);

            Assert.IsTrue(deleteImageResult.StatusCode == 200);
            Assert.IsNotNull(getJpgImageResult.Result);
            Assert.IsNotNull(getImageResult.Result);
            Assert.IsTrue(getImageResult.Result.ImageId == imageId);
            Assert.IsNotNull(listImageResult.Result);
            Assert.IsNotNull(listImageResult.Result.Images);
            Assert.IsTrue(listImageResult.Result.Images.Count > 0);
            Assert.IsNotNull(addImagesResult.Result);
            Assert.IsNotNull(addImagesResult.Result.Images);
            Assert.IsTrue(addImagesResult.Result.Images.Count > 0);
        }