我正在使用以下方法来检测图像中的文本。但是,一次执行此方法将在我的Azure仪表板中产生9个事务。如果我缺少什么可以请人指导吗?还是代码有什么问题?
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);
}
谢谢。
答案 0 :(得分:1)
取自Cognitive Services pricing page:
什么是计算机视觉API的交易?
对于“识别文本”,每个POST调用都计为一个事务。用于查看异步服务结果的所有GET调用均计为事务,但免费。
我的猜测是,“问题”处于while
语句中,因为您正在(可能)仍在运行作业时获得结果。在完成工作之前,您可能已经获得了几次状态。好东西:这些API调用是免费的。
我看到您的代码与Quickstart: Extract text using the Computer Vision SDK and C#中的代码相同,因此应该可以。