Reputation: 10386
I am using below method to detect text in an image. But a single execution of this method results in 9 transactions in my Azure dashboard. Can someone please guide if I am missing something? or is there anything wrong with the code?
public async Task<IActionResult> ConvertToText(string url)
{
string subscriptionKey = "jfh3879rhf4389terhkjy86";
ComputerVisionClient computerVision = new ComputerVisionClient(
new ApiKeyServiceClientCredentials(subscriptionKey),
new System.Net.Http.DelegatingHandler[] { });
// Specify the Azure region
computerVision.Endpoint = "https://westcentralus.api.cognitive.microsoft.com";
if (!Uri.IsWellFormedUriString(url, UriKind.Absolute))
{
return null;
}
TextRecognitionMode textRecognitionMode = TextRecognitionMode.Printed;
int numberOfCharsInOperationId = 36;
// Start the async process to recognize the text
RecognizeTextHeaders textHeaders =
await computerVision.RecognizeTextAsync(url, textRecognitionMode);
// Retrieve the URI where the recognized text will be
// stored from the Operation-Location header
string operationId = textHeaders.OperationLocation.Substring(
textHeaders.OperationLocation.Length - numberOfCharsInOperationId);
TextOperationResult result =
await computerVision.GetTextOperationResultAsync(operationId);
// Wait for the operation to complete
int i = 0;
int maxRetries = 10;
while ((result.Status == TextOperationStatusCodes.Running ||
result.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries)
{
Console.WriteLine("Server status: {0}, waiting {1} seconds...", result.Status, i);
await Task.Delay(1000);
result = await computerVision.GetTextOperationResultAsync(operationId);
}
// Display the results
var lines = result.RecognitionResult.Lines;
string details = "";
foreach (Line line in lines)
{
details += line.Text + Environment.NewLine;
}
return Content(details);
}
Thank you.
Upvotes: 0
Views: 632
Reputation: 15621
Taken from the Cognitive Services pricing page:
What constitutes a transaction for Computer Vision API?
For Recognize Text each POST call counts as a transaction. All GET calls to see the results of the async service are counted as transactions but are free of charge.
My guess would be that the 'issue' is in the while
-statement since you're getting the result while the job is (possibly) still running. You're probably getting the status a couple of times before the job is done. The good thing: those API calls are free.
I see your code is the same as in the Quickstart: Extract text using the Computer Vision SDK and C#, so it should be OK.
Upvotes: 1