public async Task <FighterType?> GetFighterTypeAsync(Stream fighterImage) { var image = await Image.FromStreamAsync(fighterImage); if (!await IsImageSafe(image)) { _logger.LogInformation("User uploaded inappropriate image"); return(null); } var annotations = await _annotator.DetectLabelsAsync(image); if (annotations.Any(a => a.Description.ToLower() == "dog" && a.Score > 0.9f)) { return(FighterType.Dog); } if (annotations.Any(a => a.Description.ToLower() == "cat" && a.Score > 0.9f)) { return(FighterType.Cat); } _logger.LogInformation($"User uploaded an image different than a dog or a cat. ({annotations?[0].Description})"); return(null); }
public async Task <List <string> > LabelImage(byte[] imgBytes) { try { List <string> lstLabels = new List <string>(); Image googleImage = Image.FromBytes(imgBytes); IReadOnlyList <EntityAnnotation> labels = await client.DetectLabelsAsync(googleImage); foreach (EntityAnnotation label in labels) { if (label.Score >= _confidenceThreshold) { lstLabels.Add(label.Description); } } return(lstLabels); } catch (Exception ex) { _logger.Error("Error getting info from Google", ex); return(null); } }
/// <summary> /// Get all tag for this picture /// </summary> /// <param name="imageFile">Picture base 64</param> /// <param name="extension">Extension picture</param> /// <returns>All tag with over 70% of prediction</returns> public async Task <List <EntityAnnotation> > GetTags(string imageFile, string extension) { IReadOnlyList <EntityAnnotation> labels = await clientGoogle.DetectLabelsAsync(Image.FromBytes(ImageConverter.Base64ToByte(imageFile.Replace($"data:image/{extension};base64,", "")))); return(labels.Where(l => l.Score * 100 >= 70).ToList()); }