Google Vision API:POST返回200 OK,但没有数据

时间:2017-11-30 13:12:17

标签: c# json asp.net-web-api2 asp.net-core-webapi google-vision

我们一直在努力通过Google Vision API发布用于识别的图片几天了。

我们正在使用Newtonsoft序列化JSON对象。似乎Google Vision处理这个转义的JSON字符串的方式不同(无效?),而不是它没有转义(有效的JSON格式)。

..当删除转义的字符并使用Postman将其直接发布到Google API时,它会起作用。

发布:



public async Task<IActionResult> Post([FromBody] ICollection<IFormFile> files)
        {
            var API_KEY = "xxxxxxxxxxxxxx";
            var AUTH_URL = "https://vision.googleapis.com/v1/images:annotate?key=" + API_KEY;


    
            string filePath = System.IO.Path.GetTempFileName();
            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    using (var stream = new MemoryStream())
                    {
                        await formFile.CopyToAsync(stream);
                        var base64Image = Extensions.ConvertToBase64(stream);
                        
                        var DetectionTypes = new List<string>() { "LABEL_DETECTION", "TEXT_DETECTION" };
                        var googleReq = new GoogleRequest();
                        var requests = googleReq.Requests;
                        requests = new List<Request>(){(new Request())};
                        requests[0].Features = new List<Feature>(){new Feature("LABEL_DETECTION"), new Feature("TEXT_DETECTION")};
                        requests[0].Image = new ugle.Image(base64Image);
                        googleReq.Requests = requests;
                        
                        var json = JsonConvert.SerializeObject(googleReq);
                        var request = new StringContent(json);
                        request.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                        var result = await httpclient.PostAsync(new Uri(AUTH_URL), request);
                        /*
                        var json = JsonConvert.SerializeObject(googleReq);
                        var content = new StringContent(json, Encoding.UTF8, "application/json");
                        var result = await httpclient.PostAsync(AUTH_URL, content);
                        */
                        return Ok( new{result=result, Request= json});
                    }

                }
            }

            return Ok(new { count = files.Count, filePath });
            //return Ok(new PictureInfo(null, null, null, BarcodeNumbers: null));

        }
&#13;
&#13;
&#13;

转义JSON(不工作)

"{\"requests\":[{\"image\":{\"content\":\"/9j/4QAuxW1f... etc.etc.. base64 image content.../Z\"},\"features\":[{\"type\":\"LABEL_DETECTION\"},{\"type\":\"TEXT_DETECTION\"}]}]}"

非逃避JSON(正常工作):

{"requests":[{"image":{"content":"/9j/W1f/Z"},"features":[{"type":"LABEL_DETECTION"},{"type":"TEXT_DETECTION"}]}]}

编辑:json(转义json)的屏幕截图 enter image description here

0 个答案:

没有答案