Esempio n. 1
0
        public IReadOnlyList <EntityAnnotation> GetLabelsAnnotationsResponse(Image image)
        {
            if (image == null)
            {
                throw new ArgumentNullException();
            }
            var labelDetectionResponse = _imageAnnotatorClient.DetectLabels(image);

            return(labelDetectionResponse);
        }
Esempio n. 2
0
        public static List <String> containsElement(Image image_name)
        {
            List <String> myList = new List <String>();

            //setting up the Google Credentials
            try
            {
                string           API_key = Directory.GetCurrentDirectory() + "\\key_thing.json";;
                GoogleCredential cred    = GoogleCredential.FromFile(API_key);
                Channel          channel = new Channel(
                    ImageAnnotatorClient.DefaultEndpoint.Host, ImageAnnotatorClient.DefaultEndpoint.Port, cred.ToChannelCredentials());
                ImageAnnotatorClient client = ImageAnnotatorClient.Create(channel);

                // Shutdown the channel when it is no longer required.
                //channel.ShutdownAsync().Wait();
                // Load an image from a local file
                var image = image_name;

                // create client, get response
                //var client = ImageAnnotatorClient.Create();


                var response = client.DetectLabels(image);



                foreach (var annotation in response)
                {
                    String current = annotation.Description;
                    if (current != null)
                    {
                        myList.Add(current.ToLower());
                    }
                }
                return(myList);
            }

            //Console.WriteLine(annotation.Description);



            catch (Grpc.Core.RpcException ex)
            {
                System.Windows.Forms.MessageBox.Show("Authentication error with the API. Check your \"key_thing.json\" Close this program and try again");
                return(myList);
            }
            catch (FileNotFoundException ex)

            {
                System.Windows.Forms.MessageBox.Show("\"key_thing.json\" Not found. Close this program and try again");
                return(myList);
            }
        }
        public List <string> GetImageTags(string url)
        {
            List <string> annotations = new List <string>();
            var           image       = Image.FromUri(url);
            var           response    = ImageAnnotator.DetectLabels(image);

            foreach (var annotation in response)
            {
                annotations.Add(annotation.Description);
            }
            return(annotations);
        }
Esempio n. 4
0
        public IActionResult CaptureImage(IFormFile file)
        {
            if (file == null)
            {
                return(Json(new
                {
                    Status = 0
                }));
            }
            byte[] imageBytes = null;
            using (var ms = new MemoryStream())
            {
                file.CopyTo(ms);
                imageBytes = ms.ToArray();
            }

            Image image5 = Image.FromBytes(imageBytes);
            ImageAnnotatorClient             client = ImageAnnotatorClient.Create();
            IReadOnlyList <EntityAnnotation> labels = client.DetectLabels(image5);

            client.DetectFaces(image5);
            var faceAttributeList = new List <FaceAttribute>();

            foreach (EntityAnnotation label in labels)
            {
                faceAttributeList.Add(new FaceAttribute()
                {
                    Score       = ((int)(label.Score * 100)).ToString(),
                    Description = label.Description
                });;
                // Console.WriteLine($"Score: {(int)(label.Score * 100)}%; Description: {label.Description}");
            }
            IReadOnlyList <FaceAnnotation> faceAnnotations = client.DetectFaces(image5);

            IReadOnlyList <EntityAnnotation> texts = client.DetectText(image5);
            var textList = new List <FaceAttribute>();

            foreach (EntityAnnotation label in texts)
            {
                textList.Add(new FaceAttribute()
                {
                    Score       = ((int)(label.Score * 100)).ToString(),
                    Description = label.Description
                });;
            }
            SafeSearchAnnotation searchAnnotations = client.DetectSafeSearch(image5);

            return(Json(new FaceResponse()
            {
                labels = faceAttributeList, faceAnnotations = faceAnnotations, texts = textList, searchAnnotations = searchAnnotations
            }));
        }
Esempio n. 5
0
        public ClassificationResult GetResult(string inputImageLocation, string pictureName)
        {
            ImageAnnotatorClient client = ImageAnnotatorClient.Create();
            Image image = Image.FromFile(inputImageLocation);
            IReadOnlyList <EntityAnnotation> results;
            var startTime = DateTime.Now;

            try
            {
                results = client.DetectLabels(image, null, Constants.maxLabelsReturned);
            }
            catch (Exception e)
            {
                throw new Exception("Error during detect label call Google.", e);
            }

            var endTime = DateTime.Now;

            var labels = new List <string>();
            var scores = new List <float>();

            foreach (var label in results)
            {
                if (!string.IsNullOrEmpty(label.Description) && label.Score != 0)
                {
                    labels.Add(label.Description);
                    scores.Add(label.Score * 100);
                }
                else
                {
                    throw new Exception("Exception during Google processing of image " + inputImageLocation);
                }
            }

            var classificationResult = new ClassificationResult()
            {
                APIName = "Google"
            };

            classificationResult.ProcessingTimeMilliseconds = endTime.Subtract(startTime).TotalMilliseconds;
            classificationResult.InputLabel          = pictureName;
            classificationResult.ReturnedLabel1      = labels[0];
            classificationResult.ReturnedConfidence1 = scores[0];
            classificationResult.ReturnedLabel2      = labels[1];
            classificationResult.ReturnedConfidence2 = scores[1];
            classificationResult.ReturnedLabel3      = labels[2];
            classificationResult.ReturnedConfidence3 = scores[2];
            classificationResult.FilePath            = inputImageLocation;
            return(classificationResult);
        }
Esempio n. 6
0
        public static void VisionUseGoogle(System.Drawing.Image img)
        {
            string p = $"{Environment.CurrentDirectory}{ConfigurationManager.AppSettings.Get(VisionConstants.GoogleAppCredentials)}";

            Environment.SetEnvironmentVariable(VisionConstants.GoogleAppKey, p);
            using (MemoryStream memStream = new MemoryStream())
            {
                img.Save(memStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                memStream.Seek(0, SeekOrigin.Begin);

                ImageAnnotatorClient client = ImageAnnotatorClient.Create();
                Image image    = Image.FromStream(memStream);
                var   response = client.DetectLabels(image);
            }
        }
Esempio n. 7
0
        public ActionResult Capture(string base64String)
        {
            byte[] imageBytes = null;

            if (!string.IsNullOrEmpty(base64String))
            {
                var imageParts = base64String.Split(',').ToList <string>();
                imageBytes = Convert.FromBase64String(imageParts[1]);
            }


            Image image5 = Image.FromBytes(imageBytes);
            ImageAnnotatorClient             client = ImageAnnotatorClient.Create();
            IReadOnlyList <EntityAnnotation> labels = client.DetectLabels(image5);

            client.DetectFaces(image5);
            var faceAttributeList = new List <FaceAttribute>();

            foreach (EntityAnnotation label in labels)
            {
                faceAttributeList.Add(new FaceAttribute()
                {
                    Score       = ((int)(label.Score * 100)).ToString(),
                    Description = label.Description
                });;
                // Console.WriteLine($"Score: {(int)(label.Score * 100)}%; Description: {label.Description}");
            }
            IReadOnlyList <FaceAnnotation>   faceAnnotations = client.DetectFaces(image5);
            IReadOnlyList <EntityAnnotation> texts           = client.DetectText(image5);
            var textList = new List <FaceAttribute>();

            foreach (EntityAnnotation label in texts)
            {
                textList.Add(new FaceAttribute()
                {
                    Score       = ((int)(label.Score * 100)).ToString(),
                    Description = label.Description
                });;
            }
            SafeSearchAnnotation searchAnnotations = client.DetectSafeSearch(image5);

            return(Json(new FaceResponse()
            {
                labels = faceAttributeList, faceAnnotations = faceAnnotations, texts = textList, searchAnnotations = searchAnnotations
            }));
        }
        public void DetectLabels()
        {
            Image image = LoadResourceImage("Gladiolos.jpg");
            // Snippet: DetectLabels
            ImageAnnotatorClient             client = ImageAnnotatorClient.Create();
            IReadOnlyList <EntityAnnotation> labels = client.DetectLabels(image);

            foreach (EntityAnnotation label in labels)
            {
                Console.WriteLine($"Score: {(int)(label.Score * 100)}%; Description: {label.Description}");
            }
            // End snippet

            // Not exhaustive, but let's check certain basic labels.
            var descriptions = labels.Select(l => l.Description).ToList();

            Assert.Contains("flower", descriptions);
            Assert.Contains("plant", descriptions);
            Assert.Contains("vase", descriptions);
        }