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"}]}]}