private VisionService service; private string _JsonPath = @"C:\BOTS\fcatsbot\json.json"; private VisionService CreateAuthorizedClient(string JsonPath) { GoogleCredential credential = GoogleCredential.FromStream(new FileStream(JsonPath, FileMode.Open)); // Inject the Cloud Vision scopes if (credential.IsCreateScopedRequired) { credential = credential.CreateScoped(new[] { VisionService.Scope.CloudPlatform }); } var res = new VisionService(new BaseClientService.Initializer { HttpClientInitializer = credential, GZipEnabled = false }); return res; }
private async Task<IList<AnnotateImageResponse>> DetectLabels( VisionService vision, string imageUrl) { // Convert image to Base64 encoded for JSON ASCII text based request MemoryStream ms = new MemoryStream(); using (var client = new HttpClient()) { Stream imageBytes = await client.GetStreamAsync(imageUrl); imageBytes.CopyTo(ms); } byte[] imageArray = ms.ToArray(); string imageContent = Convert.ToBase64String(imageArray); // Post label detection request to the Vision API // [START construct_request] var responses = vision.Images.Annotate( new BatchAnnotateImagesRequest() { Requests = new[] { new AnnotateImageRequest() { Features = new [] { new Feature() { Type = "LABEL_DETECTION"} }, Image = new Image() { Content = imageContent } } } }).Execute(); ms.Dispose(); return responses.Responses; }
foreach (var response in responses.Responses) { foreach (var label in response.LabelAnnotations) { double _score = label.Score == null ? 0 : Convert.ToDouble(label.Score.Value); var class = label.Description.Trim(); if (class .Contains("kitten") || class .Contains("cat") ) && (_score > 0.60)) { HasCatOrKittenClass = true;//moderation OK } } }
var file = await MainParams.TGBot.GetFileAsync(fileid); var file_path = file.FilePath; var urlImage = "https://api.telegram.org/file/bot" + MainParams.bot_token + "/" + file_path;
Source: https://habr.com/ru/post/399877/
All Articles