我注意到使用全新的Microsoft认知服务-通过SDK的Vision Read API确实存在一个奇怪的错误。
Here API
当前使用的SDK是Microsoft.Azure.CognitiveServices.Vision.ComputerVision 4.0.0:here
下面是我的代码(通常由官方快速入门参考获取):
[Fact]
public async Task ReadTest()
{
var subscriptionKey = "###########################";
var imageURI = "############################";
using (ComputerVisionClient computerVision = new ComputerVisionClient(
new ApiKeyServiceClientCredentials(subscriptionKey),
new System.Net.Http.DelegatingHandler[] { }))
{
computerVision.Endpoint = "https://westeurope.api.cognitive.microsoft.com";
BatchReadFileHeaders textHeaders = await computerVision.BatchReadFileAsync(imageURI, TextRecognitionMode.Handwritten);
await GetTextAsync(computerVision, textHeaders.OperationLocation);
}
}
private static async Task GetTextAsync(
ComputerVisionClient computerVision, string operationLocation)
{
string operationId = operationLocation.Substring(
operationLocation.Length - 36);
ReadOperationResult result =
await computerVision.GetReadOperationResultAsync(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.GetReadOperationResultAsync(operationId);
}
// Display the results
Console.WriteLine();
var recResults = result.RecognitionResults;
foreach (TextRecognitionResult recResult in recResults)
{
foreach (Line line in recResult.Lines)
{
Console.WriteLine(line.Text);
}
}
Console.WriteLine();
}
第一次在await computerVision.GetReadOperationResultAsync(operationId);
方法中调用GetTextAsync
时会发生此问题。基本上,错误如下:
消息:Microsoft.Rest.SerializationException:无法反序列化响应。 ---- Newtonsoft.Json.JsonReaderException:输入字符串'2.7867'不是有效的整数。路径“ recognitionResults [0] .lines [0] .boundingBox [0]”,第1行,位置161。
结果StackTrace:位于 Microsoft.Azure.CognitiveServices.Vision.ComputerVision.ComputerVisionClient.GetReadOperationResultWithHttpMessagesAsync(String operationId,Dictionary`2 customHeaders,CancellationToken cancelToken) Microsoft.Azure.CognitiveServices.Vision.ComputerVision.ComputerVisionClientExtensions.GetReadOperationResultAsync(IComputerVisionClient 操作,字符串operationId,CancellationToken cancelledToken)
在 ****。****。tests。****。GetTextAsync(ComputerVisionClient
错误非常简单且令人震惊。序列化程序尝试将包围盒的十进制值'2.7867'反序列化为corespondent模型中定义的整数:
namespace Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models
{
public class Line
{
public Line();
public Line(IList<int> boundingBox = null, string text = null, IList<Word> words = null);
[JsonProperty(PropertyName = "boundingBox")]
public IList<int> BoundingBox { get; set; }
[JsonProperty(PropertyName = "text")]
public string Text { get; set; }
[JsonProperty(PropertyName = "words")]
public IList<Word> Words { get; set; }
}
}
如您所见,模型将BoundingBox定义为整数列表,而视觉API返回小数。这怎么可能?我确定我遗漏了一些东西,因为它似乎是一个巨大而引人注目的错误... 我检查了快速入门related github,但它似乎停留在以前的版本中,几乎是空的。
最终,我找到了正确的github,并且似乎可以跟踪该问题。它仅与pdf文件相关:https://github.com/Azure/azure-sdk-for-net/issues/5547
只需在Azure支持下打开票即可。保持发布状态。