1

We have been struggling with posting images for recognition through the Google Vision API for some days now..

We are serializing the JSON object using Newtonsoft. It seems that Google Vision is handling this escaped JSON-string differently (not valid?), than when it's not escaped (valid JSON format).

.. cause when removing the escaped characters, and posting it directly to the Google API using Postman it works.

POST :

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));

        }

Escaped JSON (not working)

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

Non-Escaped JSON (working) :

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

Edited : Screenshot of json (escaped json) enter image description here

Terje Nygård
  • 1,233
  • 6
  • 25
  • 48
  • Please show a screenshot of how you got the 'escaped json'. – mjwills Nov 30 '17 at 13:15
  • Possible duplicate of [Replace "\\" with "\" in a string in C#](https://stackoverflow.com/questions/7482360/replace-with-in-a-string-in-c-sharp) – mjwills Nov 30 '17 at 13:16
  • What happens if you let the httpClient do the serializeng, and simply try `var result = await httpclient.PostAsync(new Uri(AUTH_URL), googleReq);`? – oerkelens Nov 30 '17 at 13:20
  • Will try your approach first @oerkelens :) Be right back.... – Terje Nygård Nov 30 '17 at 13:39
  • @oerkelens : Wrong type... PostAsync requires HttpContent, but googleReq is a GoogleRequest... – Terje Nygård Nov 30 '17 at 13:43
  • @mjwills : the JSON escaped string is with only one \, and that is correct way to escape the JSON (verified by escaping through : https://www.freeformatter.com/json-escape.html#ad-output – Terje Nygård Nov 30 '17 at 13:53
  • The json string is not really escaped, the debugger is just adding the backslashes to visualize that the quote is not the "ending" quote of the string. Try logging it to the console, the backslashes are not there. – boogiehound Nov 30 '17 at 14:36
  • Read the link in the second comment @TerjeNygård . – mjwills Nov 30 '17 at 20:12

0 Answers0