GoogleCloud.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using KdanCommon.GoogleCloud.Data;
  2. using KdanCommon.GoogleCloud.Data.Vision;
  3. using KdanCommon.Helpers;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Linq;
  9. using System.Net.Http;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using Windows.Globalization;
  14. using Windows.Storage;
  15. using Windows.System;
  16. namespace KdanCommon.GoogleCloud
  17. {
  18. public class GoogleCloud
  19. {
  20. public static string VisionDomain = "https://vision.googleapis.com";
  21. private Uri _visionImagesUri = null;
  22. private HttpClient _httpClient = null;
  23. private string _key = null;
  24. public GoogleCloud(string key)
  25. {
  26. _key = key;
  27. _httpClient = new HttpClient();
  28. _visionImagesUri = new Uri($"{VisionDomain}/v1/images:annotate?key={_key}");
  29. }
  30. ~GoogleCloud()
  31. {
  32. _httpClient.Dispose();
  33. }
  34. // Document : https://cloud.google.com/vision/docs/reference/rest/v1/images/annotate
  35. // Image files sent to the Vision API should not exceed 20MB.
  36. // Files exceeding 20MB generate an error.
  37. // The Vision API does not resize files of this size. Reducing your file size can significantly improve throughput;
  38. // however, be careful not to reduce image quality in the process.
  39. // Note that the Vision API imposes a 10MB JSON request size limit;
  40. // larger files should be hosted on Cloud Storage or on the web, rather than being passed as base64-encoded content in the JSON itself.
  41. public async Task<ImagesResponse> GetVisionImages(List<StorageFile> images, string[] langHints = null)
  42. {
  43. ImagesResponse response = null;
  44. var base64Contents = new List<string>();
  45. foreach (var image in images)
  46. {
  47. var base64Content = await Base64Helper.StorageFileToBase64(image);
  48. base64Contents.Add(base64Content);
  49. }
  50. var imagesRequest = new ImgesRequest(base64Contents, langHints);
  51. string requestJsonStr = JsonConvert.SerializeObject(imagesRequest);
  52. var content = new StringContent(requestJsonStr, System.Text.Encoding.UTF8, "application/json");
  53. var result = await _httpClient.PostAsync(_visionImagesUri, content);
  54. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  55. {
  56. var jsonString = await result.Content.ReadAsStringAsync();
  57. response = JsonTool.DeserializeJSON<ImagesResponse>(jsonString);
  58. }
  59. return response;
  60. }
  61. // source : https://cloud.google.com/vision/docs/languages
  62. public static List<Lang> GetVisionSupportedLanguages()
  63. {
  64. var langs = new List<Lang>();
  65. langs.Add(new Lang("Afrikaans", "af"));
  66. langs.Add(new Lang("Albanian", "sq"));
  67. langs.Add(new Lang("Arabic", "ar"));
  68. langs.Add(new Lang("Armenian", "hy"));
  69. langs.Add(new Lang("Belarusian", "be"));
  70. langs.Add(new Lang("Bengali", "bn"));
  71. langs.Add(new Lang("Bulgarian", "bg"));
  72. langs.Add(new Lang("Catalan", "ca"));
  73. langs.Add(new Lang("Chinese-Simplified", "zh"));
  74. langs.Add(new Lang("Chinese-Traditional", "zh-Hant"));
  75. langs.Add(new Lang("Croatian", "hr"));
  76. langs.Add(new Lang("Czech", "cs"));
  77. langs.Add(new Lang("Danish", "da"));
  78. langs.Add(new Lang("Dutch", "nl"));
  79. langs.Add(new Lang("English", "en"));
  80. langs.Add(new Lang("Estonian", "et"));
  81. langs.Add(new Lang("Filipino", "tl"));
  82. langs.Add(new Lang("Finnish", "fi"));
  83. langs.Add(new Lang("French", "fr"));
  84. langs.Add(new Lang("German", "de"));
  85. langs.Add(new Lang("Greek", "el"));
  86. langs.Add(new Lang("Gujarati", "gu"));
  87. langs.Add(new Lang("Hebrew", "iw"));
  88. langs.Add(new Lang("Hindi", "hi"));
  89. langs.Add(new Lang("Hungarian", "hu"));
  90. langs.Add(new Lang("Icelandic", "is"));
  91. langs.Add(new Lang("Indonesian", "id"));
  92. langs.Add(new Lang("Italian", "it"));
  93. langs.Add(new Lang("Japanese", "ja"));
  94. langs.Add(new Lang("Kannada", "kn"));
  95. langs.Add(new Lang("Khmer", "km"));
  96. langs.Add(new Lang("Korean", "ko"));
  97. langs.Add(new Lang("Lao", "lo"));
  98. langs.Add(new Lang("Latvian", "lv"));
  99. langs.Add(new Lang("Lithuanian", "lt"));
  100. langs.Add(new Lang("Macedonian", "mk"));
  101. langs.Add(new Lang("Malay", "ms"));
  102. langs.Add(new Lang("Malayalam", "ml"));
  103. langs.Add(new Lang("Marathi", "mr"));
  104. langs.Add(new Lang("Nepali", "ne"));
  105. langs.Add(new Lang("Norwegian", "no"));
  106. langs.Add(new Lang("Persian", "fa"));
  107. langs.Add(new Lang("Polish", "pl"));
  108. langs.Add(new Lang("Portuguese", "pt"));
  109. langs.Add(new Lang("Punjabi", "pa"));
  110. langs.Add(new Lang("Romanian", "ro"));
  111. langs.Add(new Lang("Russian", "ru"));
  112. langs.Add(new Lang("Serbian", "sr"));
  113. langs.Add(new Lang("Slovak", "sk"));
  114. langs.Add(new Lang("Slovenian", "sl"));
  115. langs.Add(new Lang("Spanish", "es"));
  116. langs.Add(new Lang("Swedish", "sv"));
  117. langs.Add(new Lang("Tamil", "ta"));
  118. langs.Add(new Lang("Telugu", "te"));
  119. langs.Add(new Lang("Thai", "th"));
  120. langs.Add(new Lang("Turkish", "tr"));
  121. langs.Add(new Lang("Ukrainian", "uk"));
  122. langs.Add(new Lang("Vietnamese", "vi"));
  123. langs.Add(new Lang("Yiddish", "yi"));
  124. return langs;
  125. }
  126. // source : https://cloud.google.com/translate/docs/languages
  127. public static List<Lang> GetTranslateSupportedLanguages()
  128. {
  129. var langs = new List<Lang>();
  130. langs.Add(new Lang("Afrikaans", "af"));
  131. langs.Add(new Lang("Albanian", "sq"));
  132. langs.Add(new Lang("Amharic", "am"));
  133. langs.Add(new Lang("Arabic", "ar"));
  134. langs.Add(new Lang("Armenian", "hy"));
  135. langs.Add(new Lang("Azerbaijani", "az"));
  136. langs.Add(new Lang("Basque", "eu"));
  137. langs.Add(new Lang("Belarusian", "be"));
  138. langs.Add(new Lang("Bengali", "bn"));
  139. langs.Add(new Lang("Bosnian", "bs"));
  140. langs.Add(new Lang("Bulgarian", "bg"));
  141. langs.Add(new Lang("Catalan", "ca"));
  142. //langs.Add(new Lang("Cebuano", "ceb")); // 文件說有支援但其實不能用
  143. langs.Add(new Lang("Chinese-Simplified", "zh-CN"));
  144. langs.Add(new Lang("Chinese-Traditional", "zh-TW"));
  145. langs.Add(new Lang("Corsican", "co"));
  146. langs.Add(new Lang("Croatian", "hr"));
  147. langs.Add(new Lang("Czech", "cs"));
  148. langs.Add(new Lang("Danish", "da"));
  149. langs.Add(new Lang("Dutch", "nl"));
  150. langs.Add(new Lang("English", "en"));
  151. langs.Add(new Lang("Esperanto", "eo"));
  152. langs.Add(new Lang("Estonian", "et"));
  153. langs.Add(new Lang("Finnish", "fi"));
  154. langs.Add(new Lang("French", "fr"));
  155. langs.Add(new Lang("Frisian", "fy"));
  156. langs.Add(new Lang("Galician", "gl"));
  157. langs.Add(new Lang("Georgian", "ka"));
  158. langs.Add(new Lang("German", "de"));
  159. langs.Add(new Lang("Greek", "el"));
  160. langs.Add(new Lang("Guarani", "gn"));
  161. langs.Add(new Lang("Gujarati", "gu"));
  162. langs.Add(new Lang("Haitian Creole", "ht"));
  163. langs.Add(new Lang("Hausa", "ha"));
  164. //langs.Add(new Lang("Hawaiian", "haw")); // 文件說有支援但其實不能用
  165. langs.Add(new Lang("Hebrew", "he"));
  166. langs.Add(new Lang("Hindi", "hi"));
  167. //langs.Add(new Lang("Hmong", "hmn")); // 文件說有支援但其實不能用
  168. langs.Add(new Lang("Hungarian", "hu"));
  169. langs.Add(new Lang("Icelandic", "is"));
  170. langs.Add(new Lang("Igbo", "ig"));
  171. langs.Add(new Lang("Indonesian", "id"));
  172. langs.Add(new Lang("Irish", "ga"));
  173. langs.Add(new Lang("Italian", "it"));
  174. langs.Add(new Lang("Japanese", "ja"));
  175. langs.Add(new Lang("Javanese", "jv"));
  176. langs.Add(new Lang("Kannada", "kn"));
  177. langs.Add(new Lang("Kazakh", "kk"));
  178. langs.Add(new Lang("Khmer", "km"));
  179. langs.Add(new Lang("Kinyarwanda", "rw"));
  180. langs.Add(new Lang("Korean", "ko"));
  181. langs.Add(new Lang("Kurdish", "ku"));
  182. langs.Add(new Lang("Kyrgyz", "ky"));
  183. langs.Add(new Lang("Lao", "lo"));
  184. langs.Add(new Lang("Latin", "la"));
  185. langs.Add(new Lang("Latvian", "lv"));
  186. langs.Add(new Lang("Lithuanian", "lt"));
  187. langs.Add(new Lang("Luxembourgish", "lb"));
  188. langs.Add(new Lang("Macedonian", "mk"));
  189. langs.Add(new Lang("Malagasy", "mg"));
  190. langs.Add(new Lang("Malay", "ms"));
  191. langs.Add(new Lang("Malayalam", "ml"));
  192. langs.Add(new Lang("Maltese", "mt"));
  193. langs.Add(new Lang("Maori", "mi"));
  194. langs.Add(new Lang("Marathi", "mr"));
  195. langs.Add(new Lang("Mongolian", "mn"));
  196. langs.Add(new Lang("Myanmar (Burmese)", "my"));
  197. langs.Add(new Lang("Nepali", "ne"));
  198. langs.Add(new Lang("Norwegian", "no"));
  199. langs.Add(new Lang("Nyanja (Chichewa)", "ny"));
  200. langs.Add(new Lang("Odia (Oriya)", "or"));
  201. langs.Add(new Lang("Pashto", "ps"));
  202. langs.Add(new Lang("Persian", "fa"));
  203. langs.Add(new Lang("Polish", "pl"));
  204. langs.Add(new Lang("Portuguese", "pt"));
  205. langs.Add(new Lang("Punjabi", "pa"));
  206. langs.Add(new Lang("Romanian", "ro"));
  207. langs.Add(new Lang("Russian", "ru"));
  208. langs.Add(new Lang("Samoan", "sm"));
  209. langs.Add(new Lang("Sanskrit", "sa"));
  210. langs.Add(new Lang("Scots Gaelic", "gd"));
  211. langs.Add(new Lang("Serbian", "sr"));
  212. langs.Add(new Lang("Sesotho", "st"));
  213. langs.Add(new Lang("Shona", "sn"));
  214. langs.Add(new Lang("Sindhi", "sd"));
  215. langs.Add(new Lang("Sinhala (Sinhalese) ", "si"));
  216. langs.Add(new Lang("Slovak", "sk"));
  217. langs.Add(new Lang("Slovenian", "sl"));
  218. langs.Add(new Lang("Somali", "so"));
  219. langs.Add(new Lang("Spanish", "es"));
  220. langs.Add(new Lang("Sundanese", "su"));
  221. langs.Add(new Lang("Swahili", "sw"));
  222. langs.Add(new Lang("Swedish", "sv"));
  223. langs.Add(new Lang("Tagalog (Filipino)", "tl"));
  224. langs.Add(new Lang("Tajik", "tg"));
  225. langs.Add(new Lang("Tamil", "ta"));
  226. langs.Add(new Lang("Tatar", "tt"));
  227. langs.Add(new Lang("Telugu", "te"));
  228. langs.Add(new Lang("Thai", "th"));
  229. langs.Add(new Lang("Turkish", "tr"));
  230. langs.Add(new Lang("Turkmen", "tk"));
  231. langs.Add(new Lang("Ukrainian", "uk"));
  232. langs.Add(new Lang("Urdu", "ur"));
  233. langs.Add(new Lang("Uyghur", "ug"));
  234. langs.Add(new Lang("Uzbek", "uz"));
  235. langs.Add(new Lang("Vietnamese", "vi"));
  236. langs.Add(new Lang("Welsh", "cy"));
  237. langs.Add(new Lang("Xhosa", "xh"));
  238. langs.Add(new Lang("Yiddish", "yi"));
  239. langs.Add(new Lang("Yoruba", "yo"));
  240. langs.Add(new Lang("Zulu", "zu"));
  241. return langs;
  242. }
  243. }
  244. }