public SafeSearchModel(SafeSearchAnnotation safeSearchAnnotation) { Adult = safeSearchAnnotation.Adult; Violence = safeSearchAnnotation.Violence; Spoof = safeSearchAnnotation.Spoof; Medical = safeSearchAnnotation.Medical; }
private Dictionary <string, string> GetTrigeredFlags(SafeSearchAnnotation response) { var flags = new Dictionary <string, string>(); if (response.Adult > Likelihood.Possible) { flags["Adult"] = response.Adult.ToString(); } if (response.Medical > Likelihood.Possible) { flags["Medical"] = response.Medical.ToString(); } if (response.Racy > Likelihood.Possible) { flags["Racy"] = response.Racy.ToString(); } if (response.Violence > Likelihood.Possible) { flags["Violence"] = response.Violence.ToString(); } if (flags.Count == 0) { flags["SAFE"] = "1.00"; } return(flags); }
public static async Task <List <string> > CheckImageUrl(string url, SocketUserMessage msg, SocketMessage arg, ITextChannel reportChan) { if (Utils.IsImage(url.Split('.').Last())) { var image = await Google.Cloud.Vision.V1.Image.FetchFromUriAsync(url); List <string> flags = new List <string>(); SafeSearchAnnotation response = await Program.P.imageClient.DetectSafeSearchAsync(image); if (response.Adult > Likelihood.Possible || response.Medical > Likelihood.Possible || response.Racy > Likelihood.Possible || response.Violence > Likelihood.Possible) { if (response.Adult > Likelihood.Possible) { flags.Add("Adult(" + response.Adult.ToString() + ")"); } if (response.Medical > Likelihood.Possible) { flags.Add("Medical(" + response.Medical.ToString() + ")"); } if (response.Racy > Likelihood.Possible) { flags.Add("Racy(" + response.Racy.ToString() + ")"); } if (response.Violence > Likelihood.Possible) { flags.Add("Violence(" + response.Violence.ToString() + ")"); } string fileName = "SPOILER_" + GenerateFileName() + "." + url.Split('.').Last(); using (HttpClient hc = new HttpClient()) File.WriteAllBytes(fileName, await hc.GetByteArrayAsync(url)); string word = (reportChan == null) ? "deleted" : "reported"; string text = "The message of " + arg.Author.ToString() + " was " + word + " because it trigger the following flags: " + string.Join(", ", flags); if (reportChan == null) { await msg.Channel.SendMessageAsync(text); await msg.Channel.SendFileAsync(fileName); await msg.DeleteAsync(); } else { await reportChan.SendMessageAsync(text); await reportChan.SendFileAsync(fileName); } File.Delete(fileName); return(flags.Select(x => x.Split('(')[0]).ToList()); } flags.Add("SAFE"); return(flags); } return(null); }
private async Task <bool> IsSfw(string url, bool isChanNsfw) { var image = await Google.Cloud.Vision.V1.Image.FetchFromUriAsync(url); SafeSearchAnnotation response = await imageClient.DetectSafeSearchAsync(image); if (isChanNsfw) { return((int)response.Medical < 3 && (int)response.Violence < 3); } return((int)response.Adult < 3 && (int)response.Medical < 3 && (int)response.Violence < 3); }
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 })); }
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 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); }
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)); }
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)); } }