Problem getting information from the Azure cognitive service (OCR)

2

I'm developing a windows service that downloads a web image, "flip" it horizontally and sends it to the Azure (OCR) cognitive service, the idea is to capture the texts of that image. However, when I send the image via request, the return is empty.

I have tested the same method with different images and it worked normally.

I also tested, via the azure online test ( link ) which also worked normally (with both images).

public void FlipaImagem(string url)
{
    using (var wc = new WebClient())
    {
        using (var imgStream = new MemoryStream(wc.DownloadData(url)))
        {
            using (var objImage = System.Drawing.Image.FromStream(imgStream))
            {
               objImage.RotateFlip(RotateFlipType.Rotate180FlipY);
                if (File.Exists("arquivoFlipado.jpeg"))
                    File.Delete("arquivoFlipado.jpeg");
                objImage.Save("arquivoFlipado.jpeg", ImageFormat.Jpeg);
                objImage.Dispose();
            }
        }
    }
}

The above method saves the "flipped" image, if I take this saved image and send via online test (which I mentioned above), it works.

Below I'm sending to Azure (Remember that if I pass URLs from other images it works normally):

public RespostaAzure PostServicoAsync(string urlImagem)
    {
        httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionkey);
        FlipaImagem(urlImagem);
        var imagem = Image.FromFile(@"arquivoFlipado.jpeg");

        ImageConverter _imageConverter = new ImageConverter();
        byte[] xByte = (byte[])_imageConverter.ConvertTo(imagem, typeof(byte[]));

        MemoryStream stream = new MemoryStream(xByte);



        var streamContent = new StreamContent(stream);

        streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        var queryString = HttpUtility.ParseQueryString(string.Empty);
        queryString["language"] = "pt";
        queryString["detectOrientation "] = "true";
        var novo = urlAzure + queryString;

        try
        {
            var response = httpClient.PostAsync(novo, streamContent).Result;

            var x = response.Content.ReadAsStringAsync().Result;

            var obj = JsonConvert.DeserializeObject<RespostaAzure>(x);


             return obj;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            _imageConverter = null;
            xByte = null;
            imagem.Dispose();
        }
    }

Any idea what it might be?

Thank you.

=================== UPDATE ====================

When testing directly from the API ( link ), I noticed that displays the same behavior, but fails to recognize "easy-to-analyze" images, but the Azure website works perfectly.

Ps: In azure it says it is using API 2.0 but when using it, it has the same behavior as 1.0, and when checking via console I see that the Azure website sends the request to a totally different URL that I believe is restricted only for them.

It may be clearer if I leave an image that I'm trying to read and the problem occurs along with the links:

  • Azure Link (Here you can return the texts correctly): link

  • API Link (Does not return the texts):
    link

  • Sample image: link

asked by anonymous 10.09.2018 / 19:50

1 answer

1

Apparently the Azure sample API is another, in this case Recognition Text. I've taken this as a "solution" this topic . Although it was not what I was looking for, it bypasses the problem and delivers the same response from site .

    
13.09.2018 / 13:50