Example #1
0
        public void ThrowOnAnyError()
        {
            // Snippet: ThrowOnAnyError
            Image image = new Image(); // No content or source!
            // Just a single request in this example, but usually BatchAnnotateImages would be
            // used with multiple requests.
            var request = new AnnotateImageRequest
            {
                Image    = image,
                Features = { new Feature {
                                 Type = Feature.Types.Type.SafeSearchDetection
                             } }
            };
            ImageAnnotatorClient client = ImageAnnotatorClient.Create();

            try
            {
                BatchAnnotateImagesResponse response = client.BatchAnnotateImages(new[] { request });
                // ThrowOnAnyError will throw if any individual response in response.Responses
                // contains an error. Other responses may still have useful results.
                // Errors can be detected manually by checking the Error property in each
                // individual response.
                response.ThrowOnAnyError();
            }
            catch (AggregateException e)
            {
                // Because a batch can have multiple errors, the exception thrown is AggregateException.
                // Each inner exception is an AnnotateImageException
                foreach (AnnotateImageException innerException in e.InnerExceptions)
                {
                    Console.WriteLine(innerException.Response.Error);
                }
            }
            // End snippet
        }
 public void BatchAnnotateImages()
 {
     // Snippet: BatchAnnotateImages(IEnumerable<AnnotateImageRequest>,CallSettings)
     // Create client
     ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.Create();
     // Initialize request argument(s)
     IEnumerable <AnnotateImageRequest> requests = new List <AnnotateImageRequest>();
     // Make the request
     BatchAnnotateImagesResponse response = imageAnnotatorClient.BatchAnnotateImages(requests);
     // End snippet
 }
Example #3
0
 /// <summary>Snippet for BatchAnnotateImages</summary>
 public void BatchAnnotateImages_RequestObject()
 {
     // Snippet: BatchAnnotateImages(BatchAnnotateImagesRequest,CallSettings)
     // Create client
     ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.Create();
     // Initialize request argument(s)
     BatchAnnotateImagesRequest request = new BatchAnnotateImagesRequest
     {
         Requests = { },
     };
     // Make the request
     BatchAnnotateImagesResponse response = imageAnnotatorClient.BatchAnnotateImages(request);
     // End snippet
 }
        public void BatchAnnotateImages()
        {
            Image image1 = LoadResourceImage("SchmidtBrinPage.jpg");
            Image image2 = LoadResourceImage("Chrome.png");
            // Sample: BatchAnnotateImages
            // Additional: BatchAnnotateImages(IEnumerable<AnnotateImageRequest>,*)
            ImageAnnotatorClient client = ImageAnnotatorClient.Create();
            // Perform face recognition on one image, and logo recognition on another.
            AnnotateImageRequest request1 = new AnnotateImageRequest
            {
                Image    = image1,
                Features = { new Feature {
                                 Type = Feature.Types.Type.FaceDetection
                             } }
            };
            AnnotateImageRequest request2 = new AnnotateImageRequest
            {
                Image    = image2,
                Features = { new Feature {
                                 Type = Feature.Types.Type.LogoDetection
                             } }
            };

            BatchAnnotateImagesResponse response = client.BatchAnnotateImages(new[] { request1, request2 });

            Console.WriteLine("Faces in image 1:");
            foreach (FaceAnnotation face in response.Responses[0].FaceAnnotations)
            {
                string poly = string.Join(" - ", face.BoundingPoly.Vertices.Select(v => $"({v.X}, {v.Y})"));
                Console.WriteLine($"  Confidence: {(int)(face.DetectionConfidence * 100)}%; BoundingPoly: {poly}");
            }
            Console.WriteLine("Logos in image 2:");
            foreach (EntityAnnotation logo in response.Responses[1].LogoAnnotations)
            {
                Console.WriteLine($"Description: {logo.Description}");
            }
            foreach (Status error in response.Responses.Select(r => r.Error))
            {
                Console.WriteLine($"Error detected: error");
            }
            // End sample

            Assert.Equal(3, response.Responses[0].FaceAnnotations.Count);
            Assert.Equal(1, response.Responses[1].LogoAnnotations.Count);
        }
        private IEnumerable <AnalysisResponse> Analyze(ImageAnnotatorClient client, List <AnnotateImageRequest> requests, List <Guid> requestSystemIds)
        {
            BatchAnnotateImagesResponse response = client.BatchAnnotateImages(requests);
            var requestIndex = 0;

            foreach (var res in response.Responses)
            {
                var imageSystemId = requestSystemIds[requestIndex];
                var labels        = res.LabelAnnotations.Select(a => a.Description);
                var landmarks     = res.LandmarkAnnotations.Select(a => a.Description);
                requestIndex++;
                yield return(new AnalysisResponse()
                {
                    SystemId = imageSystemId, Tags = landmarks.Concat(labels)
                });
            }
            requests.Clear();
            requestSystemIds.Clear();
        }
        public static int Main(string[] args)
        {
            // Create client
            ImageAnnotatorClient client = ImageAnnotatorClient.Create();

            // Initialize request argument(s)
            IEnumerable <AnnotateImageRequest> requests = new[]
            {
                new AnnotateImageRequest
                {
                    Image = new Image
                    {
                        Source = new ImageSource
                        {
                            GcsImageUri = "gs://cloud-samples-data/vision/face_detection/celebrity_recognition/sergey.jpg",
                        },
                    },
                    Features =
                    {
                        new Feature
                        {
                            Type = Feature.Types.Type.FaceDetection,
                        },
                    },
                },
            };

            // Call API method
            BatchAnnotateImagesResponse response = client.BatchAnnotateImages(requests);

            // Show the result
            Console.WriteLine(response);

            // Success
            Console.WriteLine("Smoke test passed OK");
            return(0);
        }
Example #7
0
        public static int Main(string[] args)
        {
            // Create client
            ImageAnnotatorClient client = ImageAnnotatorClient.Create();

            // Initialize request argument(s)
            IEnumerable <AnnotateImageRequest> requests = new[]
            {
                new AnnotateImageRequest
                {
                    Image = new Image
                    {
                        Source = new ImageSource
                        {
                            GcsImageUri = "gs://gapic-toolkit/President_Barack_Obama.jpg",
                        },
                    },
                    Features =
                    {
                        new Feature
                        {
                            Type = Feature.Types.Type.FaceDetection,
                        },
                    },
                },
            };

            // Call API method
            BatchAnnotateImagesResponse response = client.BatchAnnotateImages(requests);

            // Show the result
            Console.WriteLine(response);

            // Success
            Console.WriteLine("Smoke test passed OK");
            return(0);
        }
Example #8
0
        public static Dictionary <string, List <TagSuggestion> > RequestBatchVisionAnalysis(IEnumerable <string> paths)
        {
            if (client == null)
            {
                SetVisionAuthViaDialog();
            }
            if (client == null)
            {
                return(new Dictionary <string, List <TagSuggestion> >());
            }
            var result = new Dictionary <string, List <TagSuggestion> >();

            var req            = new List <AnnotateImageRequest>();
            var imageFilePaths = new List <string>(paths);

            for (int i = 0; i < imageFilePaths.Count; i++)
            {
                var reqItem = new AnnotateImageRequest();
                reqItem.Image = Image.FromFile(imageFilePaths[i]);
                reqItem.AddAllFeatures();
                req.Add(reqItem);
            }
            try
            {
                var batchResponse = client.BatchAnnotateImages(req);
                for (int i = 0; i < imageFilePaths.Count; i++)
                {
                    var res = batchResponse.Responses[i];
                    result.Add(imageFilePaths[i], ParseAnnotations(res));
                }
            }
            catch (Exception e)
            {
                MessageBox.Show($"vision API has encountered an error: \n\n {e}");
            }
            return(result);
        }