コード例 #1
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
            }));
        }
コード例 #2
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
            }));
        }
コード例 #3
0
        public void DetectSafeSearch()
        {
            Image image = LoadResourceImage("SchmidtBrinPage.jpg");
            // Snippet: DetectSafeSearch
            ImageAnnotatorClient client     = ImageAnnotatorClient.Create();
            SafeSearchAnnotation annotation = client.DetectSafeSearch(image);

            // Each category is classified as Very Unlikely, Unlikely, Possible, Likely or Very Likely.
            Console.WriteLine($"Adult? {annotation.Adult}");
            Console.WriteLine($"Spoof? {annotation.Spoof}");
            Console.WriteLine($"Violence? {annotation.Violence}");
            Console.WriteLine($"Medical? {annotation.Medical}");
            // End snippet
            Assert.InRange(annotation.Adult, Likelihood.VeryUnlikely, Likelihood.Unlikely);
            Assert.InRange(annotation.Spoof, Likelihood.VeryUnlikely, Likelihood.Unlikely);
            Assert.InRange(annotation.Violence, Likelihood.VeryUnlikely, Likelihood.Unlikely);
            Assert.InRange(annotation.Medical, Likelihood.VeryUnlikely, Likelihood.Unlikely);
        }
コード例 #4
0
        private Media AnalyseImage(ImageAnnotatorClient client, Media media, string imagePath)
        {
            var image = Image.FromFile(imagePath);

            var safeSearch = client.DetectSafeSearch(image);

            if (safeSearch.Adult == Likelihood.Likely || safeSearch.Adult == Likelihood.VeryLikely ||
                safeSearch.Violence == Likelihood.Likely || safeSearch.Violence == Likelihood.VeryLikely)
            {
                return(null);
            }

            var newMedia = media;

            newMedia.Status = MediaStatus.Approved;

            var colorList  = this.AnalyseColors(client, image);
            var facesList  = this.AnalyseFaces(client, image);
            var labelsList = this.AnalyseLabels(client, image);

            if (colorList.Any())
            {
                newMedia.Properties.AddRange(colorList);
            }

            if (facesList.Any())
            {
                newMedia.Properties.AddRange(facesList);
            }

            if (labelsList.Any())
            {
                newMedia.Properties.AddRange(labelsList);
            }

            return(newMedia);
        }