예제 #1
0
        private static object DetectWeb(Image image)
        {
            // [START vision_web_detection_gcs]
            // [START vision_web_detection]
            var          client     = ImageAnnotatorClient.Create();
            WebDetection annotation = client.DetectWebInformation(image);

            foreach (var matchingImage in annotation.FullMatchingImages)
            {
                Console.WriteLine("MatchingImage Score:\t{0}\tUrl:\t{1}",
                                  matchingImage.Score, matchingImage.Url);
            }
            foreach (var page in annotation.PagesWithMatchingImages)
            {
                Console.WriteLine("PageWithMatchingImage Score:\t{0}\tUrl:\t{1}",
                                  page.Score, page.Url);
            }
            foreach (var matchingImage in annotation.PartialMatchingImages)
            {
                Console.WriteLine("PartialMatchingImage Score:\t{0}\tUrl:\t{1}",
                                  matchingImage.Score, matchingImage.Url);
            }
            foreach (var entity in annotation.WebEntities)
            {
                Console.WriteLine("WebEntity Score:\t{0}\tId:\t{1}\tDescription:\t{2}",
                                  entity.Score, entity.EntityId, entity.Description);
            }
            // [END vision_web_detection]
            // [END vision_web_detection_gcs]
            return(0);
        }
예제 #2
0
        public WebDetectionModel(WebDetection webDetection)
        {
            WebEntities                = new List <WebEntityModel>();
            FullyMatchingWebImages     = new List <WebImageModel>();
            PartiallyMatchingWebImages = new List <WebImageModel>();
            SimilarWebImages           = new List <WebImageModel>();
            PagesWithMatchingImages    = new List <WebPageModel>();

            foreach (var webEntity in webDetection.WebEntities)
            {
                WebEntities.Add(new WebEntityModel(webEntity));
            }
            foreach (var matchingImage in webDetection.FullMatchingImages)
            {
                FullyMatchingWebImages.Add(new WebImageModel(matchingImage));
            }
            foreach (var partialMatchingImage in webDetection.PartialMatchingImages)
            {
                PartiallyMatchingWebImages.Add(new WebImageModel(partialMatchingImage));
            }
            foreach (var similarWebImage in webDetection.VisuallySimilarImages)
            {
                SimilarWebImages.Add(new WebImageModel(similarWebImage));
            }
            foreach (var pageWithMatchingImage in webDetection.PagesWithMatchingImages)
            {
                PagesWithMatchingImages.Add(new WebPageModel(pageWithMatchingImage));
            }
        }
        public void DetectWebInformation()
        {
            Image image = LoadResourceImage("SchmidtBrinPage.jpg");
            // Snippet: DetectWebInformation
            ImageAnnotatorClient client       = ImageAnnotatorClient.Create();
            WebDetection         webDetection = client.DetectWebInformation(image);

            foreach (WebDetection.Types.WebImage webImage in webDetection.FullMatchingImages)
            {
                Console.WriteLine($"Full image: {webImage.Url} ({webImage.Score})");
            }
            foreach (WebDetection.Types.WebImage webImage in webDetection.PartialMatchingImages)
            {
                Console.WriteLine($"Partial image: {webImage.Url} ({webImage.Score})");
            }
            foreach (WebDetection.Types.WebPage webPage in webDetection.PagesWithMatchingImages)
            {
                Console.WriteLine($"Page with matching image: {webPage.Url} ({webPage.Score})");
            }
            foreach (WebDetection.Types.WebEntity entity in webDetection.WebEntities)
            {
                Console.WriteLine($"Web entity: {entity.EntityId} / {entity.Description} ({entity.Score})");
            }
            // End snippet
        }
예제 #4
0
        public IEnumerable <IMachineTag> GetTagsForImageUrl(string imageUrl)
        {
            var image = Image.FromUri(imageUrl);
            IReadOnlyList <EntityAnnotation> labels = null;
            WebDetection webInfos = null;

            try
            {
                labels   = this.client.DetectLabels(image);
                webInfos = this.client.DetectWebInformation(image);
            }
            catch (Exception e)
            {
                if (e.Message.Contains("The URL does not appear to be accessible by us.") ||
                    e.Message.Contains("We can not access the URL currently."))
                {
                    yield break;
                }

                Console.WriteLine(e);
            }

            if (labels == null || webInfos == null)
            {
                yield break;
            }

            foreach (var machineTag in ToMTags(labels, webInfos))
            {
                yield return(machineTag);
            }
        }
예제 #5
0
        public void GetWeb(string imageFile, string extension)
        {
            WebDetection webDetection = clientGoogle.DetectWebInformation(Image.FromBytes(ImageConverter.Base64ToByte(imageFile.Replace($"data:image/{extension};base64,", ""))));

            foreach (WebDetection.Types.WebEntity entity in webDetection.WebEntities)
            {
                Console.WriteLine($"Web entity: {entity.EntityId} / {entity.Description} ({entity.Score})");
            }
        }
예제 #6
0
 public void ResetProcessing()
 {
     this._sourceImage         = null;
     this._webDetection        = null;
     this._dbpediaResult       = null;
     this._beginProcessingTime = 0.0f;
     this._endProcessingTime   = 0.0f;
     this.output = null;
     //this.annotateImageResponses = null;
     this._completed  = false;
     this._processing = false;
 }
예제 #7
0
        public Detector(string textDocument,
                        string label,
                        WebDetection webDetection,
                        IReadOnlyList <EntityAnnotation> entityAnnotations)
        {
            _textDocument      = textDocument;
            _label             = label;
            _webDetection      = webDetection;
            _entityAnnotations = entityAnnotations;

            Initialize();
        }
예제 #8
0
        protected List <Entity> GetWebEntities(WebDetection detection)
        {
            var entities = new List <Entity>();

            foreach (var webEntity in detection.WebEntities)
            {
                entities.Add(new Entity
                {
                    KeyWord = webEntity.Description,
                    Score   = webEntity.Score
                });
            }

            return(entities);
        }
예제 #9
0
        public Annotations(IList <EntityAnnotation> labelAnnotations, IList <EntityAnnotation> textAnnotations, IList <EntityAnnotation> logoAnnotations,
                           IList <EntityAnnotation> landmarkAnnotations, IList <FaceAnnotation> faceAnnotations, SafeSearchAnnotation safeSearchAnnotation,
                           ImageProperties imageProperties, WebDetection webDetection, CropHintsAnnotation cropHintsAnnoation)
        {
            Labels    = new List <EntityAnnotationModel>();
            Text      = new List <EntityAnnotationModel>();
            Logos     = new List <EntityAnnotationModel>();
            Landmarks = new List <EntityAnnotationModel>();
            Faces     = new List <FaceAnnotationModel>();

            SafeSearch      = new SafeSearchModel(safeSearchAnnotation);
            ImageProperties = new ImagePropertiesModel(imageProperties);
            WebDetections   = new WebDetectionModel(webDetection);
            CropHints       = new CropHintsAnnotationModel(cropHintsAnnoation);

            foreach (var labelAnnotation in labelAnnotations)
            {
                Labels.Add(new EntityAnnotationModel(labelAnnotation));
            }
            foreach (var textAnnotation in textAnnotations)
            {
                Text.Add(new EntityAnnotationModel(textAnnotation));
            }
            foreach (var logoAnnotation in logoAnnotations)
            {
                Logos.Add(new EntityAnnotationModel(logoAnnotation));
            }
            foreach (var landmarkAnnotation in landmarkAnnotations)
            {
                Landmarks.Add(new EntityAnnotationModel(landmarkAnnotation));
            }
            foreach (var faceAnnotation in faceAnnotations)
            {
                Faces.Add(new FaceAnnotationModel(faceAnnotation));
            }
        }
예제 #10
0
        public Annotations SendRequest(string imagePath)
        {
            var imageArray   = File.ReadAllBytes(imagePath);
            var imageContent = Convert.ToBase64String(imageArray);

            var responses = _visionService.Images.Annotate(new BatchAnnotateImagesRequest()
            {
                Requests = new[]
                {
                    new AnnotateImageRequest()
                    {
                        Features = new[]
                        {
                            new Feature()
                            {
                                Type       = DetectionTypes.LabelDetection,
                                MaxResults = _maxResults
                            },
                            new Feature()
                            {
                                Type       = DetectionTypes.TextDetection,
                                MaxResults = _maxResults
                            },
                            new Feature()
                            {
                                Type       = DetectionTypes.LandmarkDetection,
                                MaxResults = _maxResults
                            },
                            new Feature()
                            {
                                Type       = DetectionTypes.LogoDetection,
                                MaxResults = _maxResults
                            },
                            new Feature()
                            {
                                Type       = DetectionTypes.FaceDetection,
                                MaxResults = _maxResults
                            },
                            new Feature()
                            {
                                Type       = DetectionTypes.SafeSearchDetection,
                                MaxResults = _maxResults
                            },
                            new Feature()
                            {
                                Type       = DetectionTypes.ImageProperties,
                                MaxResults = _maxResults
                            },
                        },
                        Image = new Image()
                        {
                            Content = imageContent
                        }
                    }
                }
            }).Execute();

            var labelAnnotations     = new List <EntityAnnotation>();
            var textAnnotations      = new List <EntityAnnotation>();
            var landmarkAnnotations  = new List <EntityAnnotation>();
            var logoAnnotations      = new List <EntityAnnotation>();
            var faceAnnotations      = new List <FaceAnnotation>();
            var safeSearchAnnotation = new SafeSearchAnnotation();
            var imageProperties      = new ImageProperties();
            var webDetection         = new WebDetection();
            var cropHints            = new CropHintsAnnotation();

            foreach (var response in responses.Responses)
            {
                if (response.LabelAnnotations != null)
                {
                    foreach (var labelAnnotation in response.LabelAnnotations)
                    {
                        labelAnnotations.Add(labelAnnotation);
                    }
                }

                if (response.LandmarkAnnotations != null)
                {
                    foreach (var landmarkAnnotation in response.LandmarkAnnotations)
                    {
                        landmarkAnnotations.Add(landmarkAnnotation);
                    }
                }

                if (response.LogoAnnotations != null)
                {
                    foreach (var logoAnnotation in response.LogoAnnotations)
                    {
                        logoAnnotations.Add(logoAnnotation);
                    }
                }

                if (response.TextAnnotations != null)
                {
                    foreach (var textAnnotation in response.TextAnnotations)
                    {
                        textAnnotations.Add(textAnnotation);
                    }
                }

                if (response.FaceAnnotations != null)
                {
                    foreach (var faceAnnotation in response.FaceAnnotations)
                    {
                        faceAnnotations.Add(faceAnnotation);
                    }
                }

                if (response.WebDetection != null)
                {
                    webDetection = response.WebDetection;
                }

                if (response.CropHintsAnnotation != null)
                {
                    cropHints = response.CropHintsAnnotation;
                }

                if (response.SafeSearchAnnotation != null)
                {
                    safeSearchAnnotation = response.SafeSearchAnnotation;
                }

                if (response.ImagePropertiesAnnotation != null)
                {
                    imageProperties = response.ImagePropertiesAnnotation;
                }
            }

            return(new Annotations(labelAnnotations, textAnnotations, logoAnnotations, landmarkAnnotations, faceAnnotations, safeSearchAnnotation, imageProperties, webDetection, cropHints));
        }
예제 #11
0
    //private void PrintDebugMessage(string msg)
    //{
    //    this.debugMessage.text = msg;
    //}

    IEnumerator BeginProcessing()
    {
        this.debug.Print("in beginprocessing");
        //timestamp the beginning of the processing cycle
        this._beginProcessingTime = Time.time;

        //we have the source image.
        //we need to create a request and invoke the machine vision service

        //convert the source image into a base64-encoded string
        byte[] jpg    = this._sourceImage.EncodeToJPG();
        string base64 = System.Convert.ToBase64String(jpg);

        //create the request collection object
        AnnotateImageRequests requests = new AnnotateImageRequests();

        requests.requests = new List <AnnotateImageRequest>();
        //create the request object
        AnnotateImageRequest request = new AnnotateImageRequest();

        //configure the request object
        request.image         = new Image();
        request.image.content = base64;
        request.features      = new List <Feature>();
        Feature feature = new Feature();

        //we are only interested in web detections
        feature.type = FeatureType.WEB_DETECTION.ToString();
        //we only want a single response
        feature.maxResults = this.maxResults;
        request.features.Add(feature);
        requests.requests.Add(request);
        //creat a MachineVisionClient to post the request
        MachineVisionClient client = new MachineVisionClient();

        //send the request
        yield return(client.SendWebRequest(requests));

        //the MachineVisionClient will send the request and create response object
        //get a reference to the response object
        MachineVisionResponse res = client.response;

        //check if an error was encountered during the request
        if (res.isError)
        {
            //an error occurred during processing
            Debug.Log("Web detection object is null.");
            //return a null web detection object
            this._webDetection = null;
            //set the DPpedia result to null
            this.output = new DBpediaResult();
            yield break;
        }
        //if we get here, then we have successully returned a web detection from the Google Vision API
        this._webDetection = res.annotateImageResponse.webDetection;
        //now run the dbpedia queries
        yield return(StartCoroutine(RunDBPediaQueries()));

        if (this._dbpediaResult == null)
        {
            //the retrieval of results from dbpedia failed, so create a null dbpedia result
            this.output = new DBpediaResult();
        }
        else
        {
            this.output = this._dbpediaResult;
        }
        //if we get here then we have a result to process
        //timestamp the end processing time
        this._endProcessingTime = Time.time;
        //mark the processing as completed
        this._completed = true;
        this.debug.Print("db-result: " + this._dbpediaResult.Success.ToString());
    }
예제 #12
0
        public JsonResult Capture(string base64String)
        {
            //List de resultado que se recibiran
            List <Model> result = new List <Model>();

            //Clase que se recibira resultado de etiquetas
            WebDetection resultOne = new WebDetection();

            try
            {
                //Obtener informacion de la imagen
                var imageParts = base64String.Split(',').ToList <string>();

                //Convertir imagen en byte
                byte[] imageBytes = Convert.FromBase64String(imageParts[1]);

                //Obtener ruta de credenciales para validar consumo de la API
                string credential_path = @"" + ConfigurationManager.AppSettings["rutaCredenciales"].ToString();

                //Enviar JSON de credenciales
                System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credential_path);

                //Crear objeto consumiendo los datos de GOOGLE VISION API
                var client = ImageAnnotatorClient.Create();

                //Enviar imagen en byte al API
                var image = Image.FromBytes(imageBytes);

                //Obtener texto de las imagenes
                var response = client.DetectText(image);

                //Obtener atributos y imagenes relacionadas
                resultOne = client.DetectWebInformation(image);

                //Setear textos encontrados en una lista
                foreach (var annotation in response)
                {
                    Model item = new Model();
                    item.Locale      = annotation.Locale;
                    item.Mid         = annotation.Mid;
                    item.Description = annotation.Description;
                    result.Add(item);
                    if (annotation.Description != null)
                    {
                        Console.WriteLine(annotation.Description);
                    }
                }
            }
            catch (Exception ex)
            {
                //Enviar mensaje de excepcion en caso ocurra un error
                Model item = new Model();
                item.Locale      = "";
                item.Mid         = "";
                item.Description = ex.Message;
                result.Add(item);
            }

            //Retorna data con los resultados
            return(Json(new { data = result, dataDetail = resultOne }, JsonRequestBehavior.AllowGet));
        }
예제 #13
0
        private static IEnumerable <IMachineTag> ToMTags(IReadOnlyList <EntityAnnotation> labels, WebDetection webInfos)
        {
            foreach (var x in labels)
            {
                if (string.IsNullOrEmpty(x.Description))
                {
                    continue;
                }

                var mtag = new MachineTag {
                    Name = x.Description, Score = x.Score, Source = KeyLabel
                };
                yield return(mtag);
            }

            foreach (var x in webInfos.WebEntities)
            {
                if (string.IsNullOrEmpty(x.Description))
                {
                    continue;
                }

                var mtag = new MachineTag {
                    Name = x.Description, Score = x.Score, Source = KeyWeb
                };
                yield return(mtag);
            }
        }