简体   繁体   中英

Why im getting "ComputerVisionOcrErrorException: Operation returned an invalid status code 'BadRequest'"

I'm trying to test the Computer Vision SDK for .NET. For that i've created a Computer vision resource in azure.

I create a project in .NET6 and follow the Microsoft guide to implement the api call.

But when i reach the code line:

var textHeaders = await client.ReadAsync(urlFile);

i'm getting the exception: ComputerVisionOcrErrorException: Operation returned an invalid status code 'BadRequest'

This is the complete code:

public static void ComputerVisionPOC()
        {
            // Add your Computer Vision subscription key and endpoint
            const string subscriptionKey = "xxxxxxx";
            const string endpoint = @"https://xxxx.cognitiveservices.azure.com/";

            const string readTextUrlImage = "https://drive.google.com/file/d/xxxxxxxx/view?usp=sharing";
            var client = Authenticate(endpoint, subscriptionKey);

            // Extract text (OCR) from a URL image using the Read 
            ReadFileUrl(client, readTextUrlImage).Wait();
        }
        
        public static ComputerVisionClient Authenticate(string endpoint, string key)
        {
            var client =
                new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
                    { Endpoint = endpoint };
            return client;
        }

        public static async Task ReadFileUrl(ComputerVisionClient client, string urlFile)
        {
            try
            {
                var textHeaders = await client.ReadAsync(urlFile);
                // After the request, get the operation location (operation ID)
                var operationLocation = textHeaders.OperationLocation;
                Thread.Sleep(2000);

                // Retrieve the URI where the extracted text will be stored from the Operation-Location header.
                // We only need the ID and not the full URL
                const int numberOfCharsInOperationId = 36;
                var operationId = operationLocation[^numberOfCharsInOperationId..];

                // Extract the text
                ReadOperationResult results;
                Console.WriteLine($"Extracting text from URL file {Path.GetFileName(urlFile)}...");
                Console.WriteLine();
                do
                {
                    results = await client.GetReadResultAsync(Guid.Parse(operationId));
                } while ((results.Status == OperationStatusCodes.Running ||
                          results.Status == OperationStatusCodes.NotStarted));

                // Display the found text.
                Console.WriteLine();
                var textUrlFileResults = results.AnalyzeResult.ReadResults;
                foreach (var page in textUrlFileResults)
                {
                    foreach (var line in page.Lines)
                    {
                        Console.WriteLine(line.Text);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

I will really apreciate any help on this!

A Bad request error(400) occurs when a request which has been sent to the website server is incorrect/mistyped or corrupt and if the server receiving the request fails to understand it.

  • There is a possibility that the URL which has been used in the code could be wrong/mistyped.
  • Re-check the URL in readTextUrlImage and provide the valid one.
const string readTextUrlImage = "https://drive.google.com/file/d/xxxxxxxx/view?usp=sharing";
  • I have reproduced the same code with valid URL and got the expected result.

Output: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM