|
@@ -0,0 +1,251 @@
|
|
|
|
+using KdanCommon.GoogleCloud.Data;
|
|
|
|
+using KdanCommon.GoogleCloud.Data.Vision;
|
|
|
|
+using KdanCommon.Helpers;
|
|
|
|
+using Newtonsoft.Json;
|
|
|
|
+using System;
|
|
|
|
+using System.Collections.Generic;
|
|
|
|
+using System.Collections.ObjectModel;
|
|
|
|
+using System.Linq;
|
|
|
|
+using System.Net.Http;
|
|
|
|
+using System.Text;
|
|
|
|
+using System.Threading;
|
|
|
|
+using System.Threading.Tasks;
|
|
|
|
+using Windows.Globalization;
|
|
|
|
+using Windows.Storage;
|
|
|
|
+using Windows.System;
|
|
|
|
+
|
|
|
|
+namespace KdanCommon.GoogleCloud
|
|
|
|
+{
|
|
|
|
+ public class GoogleCloud
|
|
|
|
+ {
|
|
|
|
+ public static string VisionDomain = "https://vision.googleapis.com";
|
|
|
|
+ private Uri _visionImagesUri = null;
|
|
|
|
+
|
|
|
|
+ private HttpClient _httpClient = null;
|
|
|
|
+ private string _key = null;
|
|
|
|
+
|
|
|
|
+ public GoogleCloud(string key)
|
|
|
|
+ {
|
|
|
|
+ _key = key;
|
|
|
|
+ _httpClient = new HttpClient();
|
|
|
|
+ _visionImagesUri = new Uri($"{VisionDomain}/v1/images:annotate?key={_key}");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ~GoogleCloud()
|
|
|
|
+ {
|
|
|
|
+ _httpClient.Dispose();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Document : https://cloud.google.com/vision/docs/reference/rest/v1/images/annotate
|
|
|
|
+ // Image files sent to the Vision API should not exceed 20MB.
|
|
|
|
+ // Files exceeding 20MB generate an error.
|
|
|
|
+ // The Vision API does not resize files of this size. Reducing your file size can significantly improve throughput;
|
|
|
|
+ // however, be careful not to reduce image quality in the process.
|
|
|
|
+ // Note that the Vision API imposes a 10MB JSON request size limit;
|
|
|
|
+ // larger files should be hosted on Cloud Storage or on the web, rather than being passed as base64-encoded content in the JSON itself.
|
|
|
|
+ public async Task<ImagesResponse> GetVisionImages(List<StorageFile> images, string[] langHints = null)
|
|
|
|
+ {
|
|
|
|
+ ImagesResponse response = null;
|
|
|
|
+ var base64Contents = new List<string>();
|
|
|
|
+ foreach (var image in images)
|
|
|
|
+ {
|
|
|
|
+ var base64Content = await Base64Helper.StorageFileToBase64(image);
|
|
|
|
+ base64Contents.Add(base64Content);
|
|
|
|
+ }
|
|
|
|
+ var imagesRequest = new ImgesRequest(base64Contents, langHints);
|
|
|
|
+ string requestJsonStr = JsonConvert.SerializeObject(imagesRequest);
|
|
|
|
+ var content = new StringContent(requestJsonStr, System.Text.Encoding.UTF8, "application/json");
|
|
|
|
+ var result = await _httpClient.PostAsync(_visionImagesUri, content);
|
|
|
|
+ if (result.StatusCode == System.Net.HttpStatusCode.OK)
|
|
|
|
+ {
|
|
|
|
+ var jsonString = await result.Content.ReadAsStringAsync();
|
|
|
|
+ response = JsonTool.DeserializeJSON<ImagesResponse>(jsonString);
|
|
|
|
+ }
|
|
|
|
+ return response;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // source : https://cloud.google.com/vision/docs/languages
|
|
|
|
+ public static List<Lang> GetVisionSupportedLanguages()
|
|
|
|
+ {
|
|
|
|
+ var langs = new List<Lang>();
|
|
|
|
+ langs.Add(new Lang("Afrikaans", "af"));
|
|
|
|
+ langs.Add(new Lang("Albanian", "sq"));
|
|
|
|
+ langs.Add(new Lang("Arabic", "ar"));
|
|
|
|
+ langs.Add(new Lang("Armenian", "hy"));
|
|
|
|
+ langs.Add(new Lang("Belarusian", "be"));
|
|
|
|
+ langs.Add(new Lang("Bengali", "bn"));
|
|
|
|
+ langs.Add(new Lang("Bulgarian", "bg"));
|
|
|
|
+ langs.Add(new Lang("Catalan", "ca"));
|
|
|
|
+ langs.Add(new Lang("Chinese-Simplified", "zh"));
|
|
|
|
+ langs.Add(new Lang("Chinese-Traditional", "zh-Hant"));
|
|
|
|
+ langs.Add(new Lang("Croatian", "hr"));
|
|
|
|
+ langs.Add(new Lang("Czech", "cs"));
|
|
|
|
+ langs.Add(new Lang("Danish", "da"));
|
|
|
|
+ langs.Add(new Lang("Dutch", "nl"));
|
|
|
|
+ langs.Add(new Lang("English", "en"));
|
|
|
|
+ langs.Add(new Lang("Estonian", "et"));
|
|
|
|
+ langs.Add(new Lang("Filipino", "tl"));
|
|
|
|
+ langs.Add(new Lang("Finnish", "fi"));
|
|
|
|
+ langs.Add(new Lang("French", "fr"));
|
|
|
|
+ langs.Add(new Lang("German", "de"));
|
|
|
|
+ langs.Add(new Lang("Greek", "el"));
|
|
|
|
+ langs.Add(new Lang("Gujarati", "gu"));
|
|
|
|
+ langs.Add(new Lang("Hebrew", "iw"));
|
|
|
|
+ langs.Add(new Lang("Hindi", "hi"));
|
|
|
|
+ langs.Add(new Lang("Hungarian", "hu"));
|
|
|
|
+ langs.Add(new Lang("Icelandic", "is"));
|
|
|
|
+ langs.Add(new Lang("Indonesian", "id"));
|
|
|
|
+ langs.Add(new Lang("Italian", "it"));
|
|
|
|
+ langs.Add(new Lang("Japanese", "ja"));
|
|
|
|
+ langs.Add(new Lang("Kannada", "kn"));
|
|
|
|
+ langs.Add(new Lang("Khmer", "km"));
|
|
|
|
+ langs.Add(new Lang("Korean", "ko"));
|
|
|
|
+ langs.Add(new Lang("Lao", "lo"));
|
|
|
|
+ langs.Add(new Lang("Latvian", "lv"));
|
|
|
|
+ langs.Add(new Lang("Lithuanian", "lt"));
|
|
|
|
+ langs.Add(new Lang("Macedonian", "mk"));
|
|
|
|
+ langs.Add(new Lang("Malay", "ms"));
|
|
|
|
+ langs.Add(new Lang("Malayalam", "ml"));
|
|
|
|
+ langs.Add(new Lang("Marathi", "mr"));
|
|
|
|
+ langs.Add(new Lang("Nepali", "ne"));
|
|
|
|
+ langs.Add(new Lang("Norwegian", "no"));
|
|
|
|
+ langs.Add(new Lang("Persian", "fa"));
|
|
|
|
+ langs.Add(new Lang("Polish", "pl"));
|
|
|
|
+ langs.Add(new Lang("Portuguese", "pt"));
|
|
|
|
+ langs.Add(new Lang("Punjabi", "pa"));
|
|
|
|
+ langs.Add(new Lang("Romanian", "ro"));
|
|
|
|
+ langs.Add(new Lang("Russian", "ru"));
|
|
|
|
+ langs.Add(new Lang("Serbian", "sr"));
|
|
|
|
+ langs.Add(new Lang("Slovak", "sk"));
|
|
|
|
+ langs.Add(new Lang("Slovenian", "sl"));
|
|
|
|
+ langs.Add(new Lang("Spanish", "es"));
|
|
|
|
+ langs.Add(new Lang("Swedish", "sv"));
|
|
|
|
+ langs.Add(new Lang("Tamil", "ta"));
|
|
|
|
+ langs.Add(new Lang("Telugu", "te"));
|
|
|
|
+ langs.Add(new Lang("Thai", "th"));
|
|
|
|
+ langs.Add(new Lang("Turkish", "tr"));
|
|
|
|
+ langs.Add(new Lang("Ukrainian", "uk"));
|
|
|
|
+ langs.Add(new Lang("Vietnamese", "vi"));
|
|
|
|
+ langs.Add(new Lang("Yiddish", "yi"));
|
|
|
|
+ return langs;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // source : https://cloud.google.com/translate/docs/languages
|
|
|
|
+ public static List<Lang> GetTranslateSupportedLanguages()
|
|
|
|
+ {
|
|
|
|
+ var langs = new List<Lang>();
|
|
|
|
+ langs.Add(new Lang("Afrikaans", "af"));
|
|
|
|
+ langs.Add(new Lang("Albanian", "sq"));
|
|
|
|
+ langs.Add(new Lang("Amharic", "am"));
|
|
|
|
+ langs.Add(new Lang("Arabic", "ar"));
|
|
|
|
+ langs.Add(new Lang("Armenian", "hy"));
|
|
|
|
+ langs.Add(new Lang("Azerbaijani", "az"));
|
|
|
|
+ langs.Add(new Lang("Basque", "eu"));
|
|
|
|
+ langs.Add(new Lang("Belarusian", "be"));
|
|
|
|
+ langs.Add(new Lang("Bengali", "bn"));
|
|
|
|
+ langs.Add(new Lang("Bosnian", "bs"));
|
|
|
|
+ langs.Add(new Lang("Bulgarian", "bg"));
|
|
|
|
+ langs.Add(new Lang("Catalan", "ca"));
|
|
|
|
+ //langs.Add(new Lang("Cebuano", "ceb")); // 文件說有支援但其實不能用
|
|
|
|
+ langs.Add(new Lang("Chinese-Simplified", "zh-CN"));
|
|
|
|
+ langs.Add(new Lang("Chinese-Traditional", "zh-TW"));
|
|
|
|
+ langs.Add(new Lang("Corsican", "co"));
|
|
|
|
+ langs.Add(new Lang("Croatian", "hr"));
|
|
|
|
+ langs.Add(new Lang("Czech", "cs"));
|
|
|
|
+ langs.Add(new Lang("Danish", "da"));
|
|
|
|
+ langs.Add(new Lang("Dutch", "nl"));
|
|
|
|
+ langs.Add(new Lang("English", "en"));
|
|
|
|
+ langs.Add(new Lang("Esperanto", "eo"));
|
|
|
|
+ langs.Add(new Lang("Estonian", "et"));
|
|
|
|
+ langs.Add(new Lang("Finnish", "fi"));
|
|
|
|
+ langs.Add(new Lang("French", "fr"));
|
|
|
|
+ langs.Add(new Lang("Frisian", "fy"));
|
|
|
|
+ langs.Add(new Lang("Galician", "gl"));
|
|
|
|
+ langs.Add(new Lang("Georgian", "ka"));
|
|
|
|
+ langs.Add(new Lang("German", "de"));
|
|
|
|
+ langs.Add(new Lang("Greek", "el"));
|
|
|
|
+ langs.Add(new Lang("Guarani", "gn"));
|
|
|
|
+ langs.Add(new Lang("Gujarati", "gu"));
|
|
|
|
+ langs.Add(new Lang("Haitian Creole", "ht"));
|
|
|
|
+ langs.Add(new Lang("Hausa", "ha"));
|
|
|
|
+ //langs.Add(new Lang("Hawaiian", "haw")); // 文件說有支援但其實不能用
|
|
|
|
+ langs.Add(new Lang("Hebrew", "he"));
|
|
|
|
+ langs.Add(new Lang("Hindi", "hi"));
|
|
|
|
+ //langs.Add(new Lang("Hmong", "hmn")); // 文件說有支援但其實不能用
|
|
|
|
+ langs.Add(new Lang("Hungarian", "hu"));
|
|
|
|
+ langs.Add(new Lang("Icelandic", "is"));
|
|
|
|
+ langs.Add(new Lang("Igbo", "ig"));
|
|
|
|
+ langs.Add(new Lang("Indonesian", "id"));
|
|
|
|
+ langs.Add(new Lang("Irish", "ga"));
|
|
|
|
+ langs.Add(new Lang("Italian", "it"));
|
|
|
|
+ langs.Add(new Lang("Japanese", "ja"));
|
|
|
|
+ langs.Add(new Lang("Javanese", "jv"));
|
|
|
|
+ langs.Add(new Lang("Kannada", "kn"));
|
|
|
|
+ langs.Add(new Lang("Kazakh", "kk"));
|
|
|
|
+ langs.Add(new Lang("Khmer", "km"));
|
|
|
|
+ langs.Add(new Lang("Kinyarwanda", "rw"));
|
|
|
|
+ langs.Add(new Lang("Korean", "ko"));
|
|
|
|
+ langs.Add(new Lang("Kurdish", "ku"));
|
|
|
|
+ langs.Add(new Lang("Kyrgyz", "ky"));
|
|
|
|
+ langs.Add(new Lang("Lao", "lo"));
|
|
|
|
+ langs.Add(new Lang("Latin", "la"));
|
|
|
|
+ langs.Add(new Lang("Latvian", "lv"));
|
|
|
|
+ langs.Add(new Lang("Lithuanian", "lt"));
|
|
|
|
+ langs.Add(new Lang("Luxembourgish", "lb"));
|
|
|
|
+ langs.Add(new Lang("Macedonian", "mk"));
|
|
|
|
+ langs.Add(new Lang("Malagasy", "mg"));
|
|
|
|
+ langs.Add(new Lang("Malay", "ms"));
|
|
|
|
+ langs.Add(new Lang("Malayalam", "ml"));
|
|
|
|
+ langs.Add(new Lang("Maltese", "mt"));
|
|
|
|
+ langs.Add(new Lang("Maori", "mi"));
|
|
|
|
+ langs.Add(new Lang("Marathi", "mr"));
|
|
|
|
+ langs.Add(new Lang("Mongolian", "mn"));
|
|
|
|
+ langs.Add(new Lang("Myanmar (Burmese)", "my"));
|
|
|
|
+ langs.Add(new Lang("Nepali", "ne"));
|
|
|
|
+ langs.Add(new Lang("Norwegian", "no"));
|
|
|
|
+ langs.Add(new Lang("Nyanja (Chichewa)", "ny"));
|
|
|
|
+ langs.Add(new Lang("Odia (Oriya)", "or"));
|
|
|
|
+ langs.Add(new Lang("Pashto", "ps"));
|
|
|
|
+ langs.Add(new Lang("Persian", "fa"));
|
|
|
|
+ langs.Add(new Lang("Polish", "pl"));
|
|
|
|
+ langs.Add(new Lang("Portuguese", "pt"));
|
|
|
|
+ langs.Add(new Lang("Punjabi", "pa"));
|
|
|
|
+ langs.Add(new Lang("Romanian", "ro"));
|
|
|
|
+ langs.Add(new Lang("Russian", "ru"));
|
|
|
|
+ langs.Add(new Lang("Samoan", "sm"));
|
|
|
|
+ langs.Add(new Lang("Sanskrit", "sa"));
|
|
|
|
+ langs.Add(new Lang("Scots Gaelic", "gd"));
|
|
|
|
+ langs.Add(new Lang("Serbian", "sr"));
|
|
|
|
+ langs.Add(new Lang("Sesotho", "st"));
|
|
|
|
+ langs.Add(new Lang("Shona", "sn"));
|
|
|
|
+ langs.Add(new Lang("Sindhi", "sd"));
|
|
|
|
+ langs.Add(new Lang("Sinhala (Sinhalese) ", "si"));
|
|
|
|
+ langs.Add(new Lang("Slovak", "sk"));
|
|
|
|
+ langs.Add(new Lang("Slovenian", "sl"));
|
|
|
|
+ langs.Add(new Lang("Somali", "so"));
|
|
|
|
+ langs.Add(new Lang("Spanish", "es"));
|
|
|
|
+ langs.Add(new Lang("Sundanese", "su"));
|
|
|
|
+ langs.Add(new Lang("Swahili", "sw"));
|
|
|
|
+ langs.Add(new Lang("Swedish", "sv"));
|
|
|
|
+ langs.Add(new Lang("Tagalog (Filipino)", "tl"));
|
|
|
|
+ langs.Add(new Lang("Tajik", "tg"));
|
|
|
|
+ langs.Add(new Lang("Tamil", "ta"));
|
|
|
|
+ langs.Add(new Lang("Tatar", "tt"));
|
|
|
|
+ langs.Add(new Lang("Telugu", "te"));
|
|
|
|
+ langs.Add(new Lang("Thai", "th"));
|
|
|
|
+ langs.Add(new Lang("Turkish", "tr"));
|
|
|
|
+ langs.Add(new Lang("Turkmen", "tk"));
|
|
|
|
+ langs.Add(new Lang("Ukrainian", "uk"));
|
|
|
|
+ langs.Add(new Lang("Urdu", "ur"));
|
|
|
|
+ langs.Add(new Lang("Uyghur", "ug"));
|
|
|
|
+ langs.Add(new Lang("Uzbek", "uz"));
|
|
|
|
+ langs.Add(new Lang("Vietnamese", "vi"));
|
|
|
|
+ langs.Add(new Lang("Welsh", "cy"));
|
|
|
|
+ langs.Add(new Lang("Xhosa", "xh"));
|
|
|
|
+ langs.Add(new Lang("Yiddish", "yi"));
|
|
|
|
+ langs.Add(new Lang("Yoruba", "yo"));
|
|
|
|
+ langs.Add(new Lang("Zulu", "zu"));
|
|
|
|
+ return langs;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|