예제 #1
0
        public virtual IEnumerable <Rectangle> ExtractFaces()
        {
            FacesResponse ??= _client.Annotate(new AnnotateImageRequest()
            {
                Image    = _image,
                Features =
                {
                    new Feature {
                        Type = Feature.Types.Type.FaceDetection
                    }
                }
            });

            return(FacesResponse.FaceAnnotations
                   .Select(f => GoogleVisionCoordinateTranslator.AbsolutePolyToRectangle(f.BoundingPoly))
                   .Select(f => GoogleVisionCoordinateTranslator.RotateRectangle(f, _rotationToApplyForFaceProcessing, Width, Height)));
        }
        public void Annotate()
        {
            Image image = LoadResourceImage("SchmidtBrinPage.jpg");
            // Snippet: Annotate
            ImageAnnotatorClient client  = ImageAnnotatorClient.Create();
            AnnotateImageRequest request = new AnnotateImageRequest
            {
                Image    = image,
                Features =
                {
                    new Feature {
                        Type = Feature.Types.Type.FaceDetection
                    },
                    // By default, no limits are put on the number of results per annotation.
                    // Use the MaxResults property to specify a limit.
                    new Feature {
                        Type = Feature.Types.Type.LandmarkDetection, MaxResults = 5
                    },
                }
            };
            AnnotateImageResponse response = client.Annotate(request);

            Console.WriteLine("Faces:");
            foreach (FaceAnnotation face in response.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("Landmarks:");
            foreach (EntityAnnotation landmark in response.LandmarkAnnotations)
            {
                Console.WriteLine($"Score: {(int)(landmark.Score * 100)}%; Description: {landmark.Description}");
            }
            if (response.Error != null)
            {
                Console.WriteLine($"Error detected: {response.Error}");
            }
            // End snippet

            Assert.Equal(3, response.FaceAnnotations.Count);
            Assert.Equal(0, response.LandmarkAnnotations.Count);
        }
예제 #3
0
        public static List <TagSuggestion> RequestVisionAnalysis(string imageFilePath)
        {
            if (client == null)
            {
                SetVisionAuthViaDialog();
            }
            if (client == null)
            {
                return(new List <TagSuggestion>());
            }

            var req = new AnnotateImageRequest()
            {
                Image = Image.FromFile(imageFilePath)
            };

            req.AddAllFeatures();

            var res = client.Annotate(req);

            return(ParseAnnotations(res));
        }
예제 #4
0
        public Response AnalyzeImage(Image image, float minConfidence)
        {
            try
            {
                var annotate = new AnnotateImageRequest();
                annotate.Image = image;
                annotate.Features.AddRange(defaultFeatures);

                var response = _client.Annotate(annotate);

                bool isSafe        = CheckSafeSearch(response);
                bool containsLogos = CheckLogos(response, minConfidence);

                bool containsPeople  = false;
                bool containsAnimals = false;

                var labelList = response.LabelAnnotations.Where(x => x.Score >= minConfidence);

                foreach (var label in labelList)
                {
                    string descr = label.Description.ToUpper();

                    if (descr.Equals("PEOPLE"))
                    {
                        containsPeople = true;
                    }

                    else if (acceptedAnimals.Contains(descr))
                    {
                        containsAnimals = true;
                    }
                }

                containsPeople = containsPeople || (response.FaceAnnotations.Count > 0);

                bool Success = isSafe && !containsLogos && !containsPeople && containsAnimals;

                if (Success)
                {
                    return(new Response
                    {
                        Sucesso = true,
                        ContemAnimal = containsAnimals,
                        ContemLogomarca = containsLogos,
                        ContemPessoa = containsPeople,
                        ConteudoSeguro = isSafe
                    });
                }
                else
                {
                    return(new Response
                    {
                        Sucesso = false,
                        Descricao = "A imagem não é adequada.",
                        ContemAnimal = containsAnimals,
                        ContemLogomarca = containsLogos,
                        ContemPessoa = containsPeople,
                        ConteudoSeguro = isSafe
                    });
                }
            }

            catch (Exception e)
            {
                return(new Response
                {
                    Sucesso = false,
                    Descricao = string.Format("Ocorreu um erro ao tentar analisar a imagem ({0}).", e.Message)
                });
            }
        }