Beispiel #1
0
        public IList <FaceAnnotation> detectFaces(string path, int maxResults)
        {
            byte[] data = File.ReadAllBytes(path);

            AnnotateImageRequest request = new AnnotateImageRequest();

            Google.Apis.Vision.v1.Data.Image img = new Google.Apis.Vision.v1.Data.Image();
            img.Content   = Convert.ToBase64String(data);
            request.Image = img;

            Feature feature = new Feature();

            feature.Type       = "FACE_DETECTION";
            feature.MaxResults = maxResults;

            request.Features = new List <Feature>()
            {
                feature
            };



            BatchAnnotateImagesRequest batchAnnotate = new BatchAnnotateImagesRequest();

            batchAnnotate.Requests = new List <AnnotateImageRequest>()
            {
                request
            };
            ImagesResource.AnnotateRequest annotate = _vision.Images.Annotate(batchAnnotate);

            BatchAnnotateImagesResponse batchResponse = annotate.Execute();

            AnnotateImageResponse response = batchResponse.Responses[0];

            if (response.FaceAnnotations == null)
            {
                throw new Exception(response.Error.Message);
            }

            return(response.FaceAnnotations);
        }
        IEnumerable <FaceAnnotation> detectFaces(IEnumerable <byte> inputImage, int maxResults)
        {
            var img = new Google.Apis.Vision.v1.Data.Image();

            img.Content = Convert.ToBase64String(inputImage.ToArray());

            AnnotateImageRequest request = new AnnotateImageRequest();

            request.Image    = img;
            request.Features = new List <Feature>();
            request.Features.Add(new Feature()
            {
                MaxResults = maxResults, Type = "FACE_DETECTION"
            });

            var batch = new BatchAnnotateImagesRequest();

            batch.Requests = new List <AnnotateImageRequest>();
            batch.Requests.Add(request);
            var annotate = _visionService.Images.Annotate(batch);
            //annotate.Key = "AIzaSyDbuBnG-8f41OVET1BXXoHjhZRTlQFFnvU";

            BatchAnnotateImagesResponse batchResponse = annotate.Execute();

            AnnotateImageResponse response = batchResponse.Responses[0];

            if (response.FaceAnnotations == null)
            {
                throw new IOException(
                          response.Error != null
                        ? response.Error.Message
                        : "Unknown error getting image annotations");
            }

            return(response.FaceAnnotations);
        }