locked
Computer vision RRS feed

  • Question

  • Hi,

    When I upload images from official the website, It can detect text from images. It fails when from API.

    I get no detect from API. I don't know why? 

    As follow:

    {"language":"unk","orientation":"NotDetected","textAngle":0.0,"regions":[]}

    I am appreciated if there have someone help me out. 

    Thanks in advance.



    • Edited by SparkSQLMVP Monday, September 17, 2018 2:09 AM
    Monday, September 17, 2018 2:07 AM

All replies

  • can you post the code you tried?

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. profile for Sajeetharan at Stack Overflow, Q&A for professional and enthusiast programmers

    Monday, September 17, 2018 2:10 AM
  •  var client = new HttpClient();
                var queryString = HttpUtility.ParseQueryString(string.Empty);

                // Request headers
                client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "MyKey");
                // Request parameters
                queryString["language"] = "unk";
                queryString["detectOrientation "] = "true";
                var uri = "https://australiaeast.api.cognitive.microsoft.com/vision/v2.0/ocr?" + queryString;

                HttpResponseMessage response;
                string responseContent;
                // Request body
                byte[] byteData = GetImageAsByteArray(filename);

                using (var content = new ByteArrayContent(byteData))
                {
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                    response = await client.PostAsync(uri, content);
                    responseContent = response.Content.ReadAsStringAsync().Result;
                }

                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    if (responseContent != "[]")
                    {
                        Log(responseContent.ToString());
                    }
                }

     static byte[] GetImageAsByteArray(string imageFilePath)
            {
                FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
                BinaryReader binaryReader = new BinaryReader(fileStream);
                return binaryReader.ReadBytes((int)fileStream.Length);
            }

    Thanks

    Monday, September 17, 2018 2:21 AM
  • Hi,

    Using the sample here (shortened below), this works for me.  Can you give this a try?

    https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/quickstarts/csharp-print-text

    Can you try this below sample?

    Thanks.

    James

    using System;
    using System.IO;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Threading.Tasks;
    using Newtonsoft.Json.Linq;
    namespace ComputerVisionExample
    {
        class Program
        {
            const string subscriptionKey = "<SUBSCRIPTION_KEY>";
            const string uriBase = "https://<REGION>.api.cognitive.microsoft.com/vision/v2.0/ocr";
            static void Main()
            {
                string imageFilePath = "C:\\PATH\\TO\\IMAGE\\image.png";
                MakeOCRRequest(imageFilePath).Wait();
                Console.WriteLine("\nPress Enter to exit...");
                Console.ReadLine();
            }
            static async Task MakeOCRRequest(string imageFilePath)
            {
                try
                {
                    HttpClient client = new HttpClient();
                    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
                    string requestParameters = "language=unk&detectOrientation=true";
                    string uri = uriBase + "?" + requestParameters;
                    HttpResponseMessage response;
                    byte[] byteData = GetImageAsByteArray(imageFilePath);
                    using (ByteArrayContent content = new ByteArrayContent(byteData))
                    {
                        content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                        response = await client.PostAsync(uri, content);
                    }
                    string contentString = await response.Content.ReadAsStringAsync();
                    Console.WriteLine("\nResponse:\n\n{0}\n",
                    JToken.Parse(contentString).ToString());
                }
                catch (Exception e)
                {
                    Console.WriteLine("\n" + e.Message);
                }
            }
            static byte[] GetImageAsByteArray(string imageFilePath)
            {
                FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
                BinaryReader binaryReader = new BinaryReader(fileStream);
                return binaryReader.ReadBytes((int)fileStream.Length);
            }
        }
    }

    Friday, September 21, 2018 11:03 PM