/// <summary> /// moderate defined file id /// </summary> /// <param name="fileid">file id</param> /// <param name="userid">user id</param> /// <returns>ModerationResult</returns> internal async Task <ModerationResult> moderateW(string fileid, int userid) { try { var file = await MainParams.TGBot.GetFileAsync(fileid); //MemoryStream ms = new MemoryStream(); var file_path = file.FilePath; var urlImage = "https://api.telegram.org/file/bot" + MainParams.bot_token + "/" + file_path; using (var client = new HttpClient()) { string json = ""; //System.Net.WebUtility.UrlEncode string fileUrl = Uri.EscapeUriString(urlImage); string wurl = watson_URL + string.Format("?api_key={0}&version={1}&url={2}", watson_apikey, watson_version, fileUrl); MainParams.nlog.Trace("FILE url=" + urlImage); // MainParams.nlog.Trace("WATSON url=" + wurl); var response = await client.GetAsync(wurl); json = await response.Content.ReadAsStringAsync(); ModerationResult res = new ModerationResult(json); var modelClassifyResponse = await response.Content.ReadAsAsync <ClassifyResponse>(); foreach (var img in modelClassifyResponse.Images) { foreach (var score in img.Scores) { foreach (var cres in score.ClassResults) { res.AddClassScore(cres.ClassId, cres.Score, cres.TypeHierarchy); } } } MainParams.nlog.Trace("WATSON picturesCurrentDay;" + MainParams.picturesCurrentDay + "; JSON;" + res.ToStringNoNewline()); Console.WriteLine("Watson OK fid=" + fileid); string tableid = MainParams.datebase.updateJson(fileid, res.ToStringNoNewline(), userid, res.maxCatScore); MainParams.picturesCurrentDay++; //add to cats table and delete from moderation if (res.HasCatOrKittenClass) { MainParams.datebase.ok_from_moderation(tableid, false, urlImage); } return(res); } } catch (Exception e) { MainParams.nlog.Debug(e); Console.WriteLine("Watson error " + e.Message); } return(new ModerationResult("")); }
// [END detect_labels] internal async Task <ModerationResult> doLabels(string inUrl) { string url = ""; ModerationResult res = new ModerationResult(""); try { // Create a new Cloud Vision client authorized via Application // Default Credentials VisionService vision = service; if (vision == null) { MainParams.nlog.Trace("!!!!!!! GOOGLE VISION NULL"); } // Use the client to get label annotations for the given image // [START parse_response] IList <AnnotateImageResponse> result = await DetectLabels(vision, inUrl); // Check if label annotations were found if (result != null) { MainParams.nlog.Trace("Labels for image: " + inUrl); // Loop through and output label annotations for the image foreach (var response in result) { foreach (var label in response.LabelAnnotations) { double _score = label.Score == null ? 0 : Convert.ToDouble(label.Score.Value); res.AddClassScore(label.Description.Trim(), _score, label.Mid); //MainParams.nlog.Trace(label.Description + " (score:" + _score + ")"); } } res.json = res.ToStringNoNewline(); if (res.json.Length > 499) { res.json = res.json.Substring(0, 499); } MainParams.nlog.Trace(res.ToStringNoNewline()); } else { MainParams.nlog.Trace("No labels found."); } } catch (Exception e) { MainParams.nlog.Debug("***NewLogs; GOOGLE doLabels!!! ;EX=;" + e.Message + ";Source=" + e.Source + ";stack=" + e.StackTrace + ";e.inner=" + e.InnerException); MainParams.nlog.Debug(e); } return(res); // [END parse_response] }
/// <summary> /// moderate jpg/png url. /// if has cat - process it to Telegram file id. /// </summary> /// <param name="url">jpg/png url</param> /// <param name="userid">user id</param> /// <param name="sendToUserid">send to user(true) or to moderator</param> /// <param name="curuser">user to send</param> /// <param name="fromUrl">we got pic from url (true) or from cat api (false)</param> /// <returns>ModerationResult</returns> internal async Task <ModerationResult> moderateWUrl(string url, int userid, bool sendToUserid = false, BotUser curuser = null, bool fromUrl = false) { try { var urlImage = url; using (var client = new HttpClient()) { string json = ""; MainParams.nlog.Trace("Moderation url=" + url); ModerationResult res = new ModerationResult(json); res = await doLabels(urlImage); MainParams.picturesCurrentDay++; //parse url and process it to fileid if (res.HasCatOrKittenClass) { Stream imageBytes = await client.GetStreamAsync(url); Telegram.Bot.Types.FileToSend fs = new Telegram.Bot.Types.FileToSend(); fs.Content = imageBytes; Uri uri = new Uri(url); fs.Filename = Path.GetFileName(uri.AbsolutePath); if (!fs.Filename.EndsWith("jpg")) { fs.Filename = fs.Filename + ".jpg"; } string fileid = ""; if (!sendToUserid) { //to create file id - send message to moderator var mesToModerator = await MainParams.TGBot.SendPhotoAsync(MainParams.moderator_id, fs, "cat from url #" + MainParams.catFromUrl); fileid = mesToModerator.Photo.Last().FileId; MainParams.nlog.Trace("Mess to Moderator sent; url=" + url); } else { string ans = ""; if (fromUrl) { ans = strings.getCatFromUrl(curuser); } else { ans = strings.getCatFromApi(curuser); } var mesToUser = await MainParams.TGBot.SendPhotoAsync(curuser.chat_id, fs, ans); fileid = mesToUser.Photo.Last().FileId; } MainParams.catFromUrl++; //string tableid = MainParams.datebase.updateJson(fileid, res.ToStringNoNewline(), userid, res.maxCatScore); //add to cats table and delete from moderation MainParams.datebase.ok_from_moderation_url(fileid, res.ToStringNoNewline(), userid, (float)res.maxCatScore, url); fs.Content.Close(); imageBytes.Close(); } else { //if no cats..send to user anyway and to manual moderation if (sendToUserid) { Stream imageBytes = await client.GetStreamAsync(url); Telegram.Bot.Types.FileToSend fs = new Telegram.Bot.Types.FileToSend(); fs.Content = imageBytes; Uri uri = new Uri(url); fs.Filename = Path.GetFileName(uri.AbsolutePath); string ans = ""; if (fromUrl) { ans = strings.getCatFromUrl(curuser); } else { ans = strings.getCatFromApi(curuser); } var mesToUser = await MainParams.TGBot.SendPhotoAsync(curuser.chat_id, fs, ans); var fileid = mesToUser.Photo.Last().FileId; //add to manual moderation MainParams.datebase.add_to_moderation(curuser.user_id, fileid); } } return(res); } } catch (Exception e) { MainParams.nlog.Debug(e); Console.WriteLine("Google error " + e.Message); } return(new ModerationResult("")); }
/// <summary> /// moderate defined file id /// </summary> /// <param name="fileid">file id</param> /// <param name="userid">user id</param> /// <returns>ModerationResult</returns> internal async Task <ModerationResult> moderateW_2(string fileid, int userid) { try { var file = await MainParams.TGBot.GetFileAsync(fileid); //MemoryStream ms = new MemoryStream(); var curUser = MainParams.cashedUsers.Find(usr => usr.user_id == userid); if (curUser == null) { curUser = MainParams.datebase.FindUserInDb(userid); } var file_path = file.FilePath; var urlImage = "https://api.telegram.org/file/bot" + MainParams.bot_token + "/" + file_path; //var res = await moderateWUrl(urlImage, userid, true, curUser, true); try { //to create new file id - send message to moderator var mesToModerator = await MainParams.TGBot.SendPhotoAsync(MainParams.moderator_id, fileid, "cat from url =" + file_path); var photo = mesToModerator.Photo.Last(); MainParams.nlog.Trace("~~~~~~~~~PHOTOSIZE len=" + photo.FileSize + " Width=" + photo.Width + " Height=" + photo.Height); fileid = photo.FileId; MainParams.nlog.Trace("Mess to Moderator sent; url old=" + urlImage); string json = ""; var newPath = photo.FilePath; var url2New = "https://api.telegram.org/file/bot" + MainParams.bot_token + "/" + newPath; MainParams.nlog.Trace("Moderation 22 url=" + url2New); ModerationResult res = new ModerationResult(json); res = await doLabelsPhoto(photo);//full stream needed MainParams.picturesCurrentDay++; MainParams.catFromUrl++; if (res.HasCatOrKittenClass) { //add to cats table MainParams.datebase.ok_from_moderation_url(fileid, res.ToStringNoNewline(), userid, (float)res.maxCatScore, urlImage); } //string tableid = MainParams.datebase.updateJson(fileid, res.ToStringNoNewline(), userid, res.maxCatScore); //add to cats table and delete from moderation //MainParams.datebase.ok_from_moderation_url(fileid, res.ToStringNoNewline(), userid, (float)res.maxCatScore, url); return(res); } catch (Exception e) { MainParams.nlog.Debug(e); Console.WriteLine("Google error " + e.Message); } return(new ModerationResult("")); //return res; } catch (Exception e) { MainParams.nlog.Debug(e); Console.WriteLine("Google error moderateW " + e.Message); } return(new ModerationResult("")); }
internal async Task <ModerationResult> doLabelsPhoto(Telegram.Bot.Types.PhotoSize inPhoto) { string url = ""; ModerationResult res = new ModerationResult(""); try { // Create a new Cloud Vision client authorized via Application // Default Credentials VisionService vision = service; if (vision == null) { MainParams.nlog.Trace("!!!!!!! GOOGLE VISION NULL"); } // Use the client to get label annotations for the given image // [START parse_response] IList <AnnotateImageResponse> result = await DetectLabelsPhoto(vision, inPhoto); // Check if label annotations were found if (result != null) { MainParams.nlog.Trace("doLabelsPhoto Labels for inPhoto: " + inPhoto.FilePath + "; result count=" + result.Count); // Loop through and output label annotations for the image int cr = 0; string json = ""; foreach (var response in result) { MainParams.nlog.Trace("response count=" + cr); if (response == null) { MainParams.nlog.Trace("doLabelsPhoto response NULL count=" + cr); } int cl = 0; if (response.LabelAnnotations == null) { MainParams.nlog.Trace("doLabelsPhoto response LabelAnnotations NULL count=" + cr); } else { foreach (var label in response.LabelAnnotations) { if (label == null) { MainParams.nlog.Trace("doLabelsPhoto label NULL count=" + cl); } if (label.Description == null) { MainParams.nlog.Trace("doLabelsPhoto label Description NULL count=" + cl); } if (label.Mid == null) { MainParams.nlog.Trace("doLabelsPhoto label Mid NULL count=" + cl); } double _score = label.Score.GetValueOrDefault(0); res.AddClassScore(label.Description.Trim(), _score, label.Mid); cl++; } } cr++; } res.json = res.ToStringNoNewline(); if (res.json.Length > 499) { res.json = res.json.Substring(0, 499); } MainParams.nlog.Trace(res.ToStringNoNewline()); } else { MainParams.nlog.Trace("No labels found."); } } catch (Exception e) { MainParams.nlog.Debug("***NewLogs; GOOGLE doLabelsPhoto!!! ;EX=;" + e.Message + ";Source=" + e.Source + ";stack=" + e.StackTrace + ";e.inner=" + e.InnerException); MainParams.nlog.Debug(e); } return(res); }
/// <summary> /// moderate jpg/png url. /// if has cat - process it to Telegram file id. /// </summary> /// <param name="url">jpg/png url</param> /// <param name="userid">user id</param> /// <param name="sendToUserid">send to user(true) or to moderator</param> /// <param name="curuser">user to send</param> /// <param name="fromUrl">we got pic from url (true) or from cat api</param> /// <returns>ModerationResult</returns> internal async Task <ModerationResult> moderateWUrl(string url, int userid, bool sendToUserid = false, BotUser curuser = null, bool fromUrl = false) { try { var urlImage = url; using (var client = new HttpClient()) { string json = ""; //System.Net.WebUtility.UrlEncode string fileUrl = Uri.EscapeUriString(urlImage); string wurl = watson_URL + string.Format("?api_key={0}&version={1}&url={2}", watson_apikey, watson_version, fileUrl); MainParams.nlog.Trace("INCOMING url=" + urlImage); //MainParams.nlog.Trace("WATSON url=" + wurl); var response = await client.GetAsync(wurl); json = await response.Content.ReadAsStringAsync(); ModerationResult res = new ModerationResult(json); var modelClassifyResponse = await response.Content.ReadAsAsync <ClassifyResponse>(); foreach (var img in modelClassifyResponse.Images) { foreach (var score in img.Scores) { foreach (var cres in score.ClassResults) { res.AddClassScore(cres.ClassId, cres.Score, cres.TypeHierarchy); } } } MainParams.nlog.Trace("WATSON picturesCurrentDay;" + MainParams.picturesCurrentDay + "; JSON;" + res.ToStringNoNewline()); Console.WriteLine("Watson OK url=" + url); MainParams.picturesCurrentDay++; //parse url and process it to fileid if (res.HasCatOrKittenClass) { Stream imageBytes = await client.GetStreamAsync(url); Telegram.Bot.Types.FileToSend fs = new Telegram.Bot.Types.FileToSend(); fs.Content = imageBytes; Uri uri = new Uri(url); fs.Filename = Path.GetFileName(uri.AbsolutePath); string fileid = ""; if (!sendToUserid) { //to create file id - send message to moderator var mesToModerator = await MainParams.TGBot.SendPhotoAsync(MainParams.moderator_id, fs, "cat from url #" + MainParams.catFromUrl); fileid = mesToModerator.Photo.Last().FileId; MainParams.nlog.Trace("Mess to Moderator sent; url=" + url); } else { string ans = ""; if (fromUrl) { ans = strings.getCatFromUrl(curuser); } else { ans = strings.getCatFromApi(curuser); } var mesToUser = await MainParams.TGBot.SendPhotoAsync(curuser.chat_id, fs, ans); fileid = mesToUser.Photo.Last().FileId; } MainParams.catFromUrl++; //string tableid = MainParams.datebase.updateJson(fileid, res.ToStringNoNewline(), userid, res.maxCatScore); //add to cats table and delete from moderation MainParams.datebase.ok_from_moderation_url(fileid, res.ToStringNoNewline(), userid, (float)res.maxCatScore, url); fs.Content.Close(); imageBytes.Close(); } else { //if no cats..send to user anyway and to manual moderation if (sendToUserid) { Stream imageBytes = await client.GetStreamAsync(url); Telegram.Bot.Types.FileToSend fs = new Telegram.Bot.Types.FileToSend(); fs.Content = imageBytes; Uri uri = new Uri(url); fs.Filename = Path.GetFileName(uri.AbsolutePath); string ans = ""; if (fromUrl) { ans = strings.getCatFromUrl(curuser); } else { ans = strings.getCatFromApi(curuser); } var mesToUser = await MainParams.TGBot.SendPhotoAsync(curuser.chat_id, fs, ans); var fileid = mesToUser.Photo.Last().FileId; //add to manual moderation MainParams.datebase.add_to_moderation(curuser.user_id, fileid); } } return(res); } } catch (Exception e) { MainParams.nlog.Debug(e); Console.WriteLine("Watson error " + e.Message); } return(new ModerationResult("")); }