Telegram бот и использование Google Cloud Vision

! IBM Watson, Google Cloud Vision .

خلفية صغيرة:

عمل برنامج الروبوت الخاص بي بنجاح كبير لبضعة أشهر باستخدام التعرف على IBM Watson ، ولكن بعد ذلك ظهرت مقالة حول رؤية سحابة google على المحور وتبين أن Google يتعرف على الصور بشكل أفضل من IBM. في اليوم نفسه ، اشتركت في وحدة تحكم مطور النظام الأساسي لسحاب Google وبدأت في إعادة كتابة حظر الإشراف على القطط في برنامج الروبوت الخاص بي.

بعد قليل من البحث ، وجدت مثالًا مناسبًا في C # على GoogleCloudPlatform github . لقد غيرت المصادقة من المثال وقمت بصنعه من ملف json بمفتاح خاص ، والذي أخذته في قسم "حسابات الخدمة" في وحدة التحكم.

كود التفويض في ج #
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;
        }


(label detection). DetectLabels , , Telegram, . file_id, .

label detection
 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;
        }

Responses label , 0.6, , :

labels
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
    }
  }
}


API Telegram file_id, C# telegram bot:

telegram getFile
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;


sendPhoto, file_id .

, ( thecatapi.com), file_id Telegram sendPhoto. Google cloud vision , IBM Watson

Source: https://habr.com/ru/post/ar399877/


All Articles