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.
const string readTextUrlImage = "https://drive.google.com/file/d/xxxxxxxx/view?usp=sharing";
Output: