Kaynağa Gözat

vision api add language hint parameter

姜青儀 1 yıl önce
ebeveyn
işleme
4172f1264d

+ 14 - 1
GoogleCloud/Data/Vision/ImgesRequest.cs

@@ -12,7 +12,7 @@ namespace KdanCommon.GoogleCloud.Data.Vision
         [JsonProperty("requests")]
         public ImageRequest[] Requests { get; set; }
 
-        public ImgesRequest(List<string> inputContents)
+        public ImgesRequest(List<string> inputContents, string[] languageHints = null)
         {
             Requests = new ImageRequest[inputContents.Count];
             for (int i = 0; i < inputContents.Count; i++)
@@ -29,6 +29,10 @@ namespace KdanCommon.GoogleCloud.Data.Vision
                         {
                             Type = "TEXT_DETECTION"
                         }
+                    },
+                    ImageContext = new ImageContext()
+                    {
+                        LanguageHints = languageHints
                     }
                 };
             }
@@ -42,6 +46,9 @@ namespace KdanCommon.GoogleCloud.Data.Vision
 
         [JsonProperty("features")]
         public Feature[] Features { get; set; }
+
+        [JsonProperty("imageContext")]
+        public ImageContext ImageContext { get; set; }
     }
 
     public partial class Feature
@@ -55,4 +62,10 @@ namespace KdanCommon.GoogleCloud.Data.Vision
         [JsonProperty("content")]
         public string Content { get; set; }
     }
+
+    public partial class ImageContext
+    {
+        [JsonProperty("languageHints")]
+        public string[] LanguageHints { get; set; }
+    }
 }

+ 2 - 2
GoogleCloud/GoogleCloud.cs

@@ -43,7 +43,7 @@ namespace KdanCommon.GoogleCloud
         // 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)
+        public async Task<ImagesResponse> GetVisionImages(List<StorageFile> images, string[] langHints = null)
         {
             ImagesResponse response = null;
             var base64Contents = new List<string>();
@@ -52,7 +52,7 @@ namespace KdanCommon.GoogleCloud
                 var base64Content = await Base64Helper.StorageFileToBase64(image);
                 base64Contents.Add(base64Content);
             }
-            var imagesRequest = new ImgesRequest(base64Contents);
+            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);