【问题标题】:RecognitionResult is null despite success status and a valid HTTP response尽管成功状态和有效的 HTTP 响应,RecognitionResult 仍为 null
【发布时间】:2020-10-25 18:00:07
【问题描述】:

我正在使用 .NET 认知服务 SDK。客户端是ComputerVisionClient,我通过读取 API 获取文本。

[Fact]
public async Task BenAndJerryRead()
{
    var client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(Configuration.SubscriptionKey))
    {
        Endpoint = Configuration.Endpoint
    };

    var testImage = new FileInfo(@"Images\COVID Warning.jpg");
    Assert.True(testImage.Exists);
    BatchReadFileInStreamHeaders submission;

    await using (var fileStream = testImage.OpenRead())
    {
        submission = await client.BatchReadFileInStreamAsync(fileStream, CancellationToken.None);
    }

    var operationLocation = submission.OperationLocation;
    var submissionId = operationLocation.Substring(operationLocation.Length - 36);
    Assert.NotNull(submissionId);
    Assert.NotEmpty(submissionId);
    TextOperationResult result;
    do
    {
            result = await client.GetTextOperationResultAsync(submissionId, CancellationToken.None);
            _testOutputHelper.WriteLine($"Polling:  {result.Status}");
            await Task.Delay(100);
    } while (result.Status != Succeeded && result.Status != Failed);

    Assert.Equal(Succeeded, result.Status);
    Assert.NotNull(result.RecognitionResult);
}

我正在调用认知服务读取 API。我得到初始的operationLocation 并轮询它以获取按预期返回的成功状态。 然而TextOperationResult.RecognitionResultRecognitionResult 属性是null。下面的 Fiddler 跟踪显示响应是有效的并且有数据。

GET https://ugc.cognitiveservices.azure.com//vision/v2.0/textOperations/b7d275e3-1f1c-408f-9846-f19f3cad9004 HTTP/1.1
Host: HIDDEN.cognitiveservices.azure.com
Ocp-Apim-Subscription-Key: HIDDEN
User-Agent: FxVersion/4.700.20.20201 OSName/Windows OSVersion/Microsoft.Windows.10.0.18363 Microsoft.Azure.CognitiveServices.Vision.ComputerVision.ComputerVisionClient/5.0.19.28102
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 2432
Content-Type: application/json; charset=utf-8
Expires: -1
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
CSP-Billing-Usage: CognitiveServices.ComputerVision.Transaction=1
apim-request-id: a77adf8b-8012-4a66-ad63-97319a305f3b
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
x-content-type-options: nosniff
Date: Sun, 05 Jul 2020 14:13:59 GMT

{"status":"Succeeded","recognitionResults":[{"page":1,"clockwiseOrientation":359.81,"width":410,"height":230,"unit":"pixel","lines":[{"boundingBox":[20,22,383,21,384,47,21,48],"text":"GOV.UK CORONAVIRUS ALERT","words":[{"boundingBox":[22,23,113,23,113,48,24,48],"text":"GOV.UK"},{"boundingBox":[121,23,303,22,303,48,122,48],"text":"CORONAVIRUS"},{"boundingBox":[308,22,384,22,383,47,308,48],"text":"ALERT"}]},{"boundingBox":[22,53,341,55,340,80,21,78],"text":"New rules in force now: you","words":[{"boundingBox":[22,56,69,55,68,78,22,78],"text":"New"},{"boundingBox":[79,55,139,55,138,79,78,79],"text":"rules"},{"boundingBox":[143,55,163,55,163,79,142,79],"text":"in"},{"boundingBox":[171,55,230,56,230,80,170,79],"text":"force"},{"boundingBox":[236,56,295,57,294,80,235,80],"text":"now:"},{"boundingBox":[299,57,340,58,339,80,298,80],"text":"you"}]},{"boundingBox":[21,86,379,82,380,109,22,113],"text":"must stay at home. More info &","words":[{"boundingBox":[22,86,81,86,81,113,22,113],"text":"must"},{"boundingBox":[86,86,137,86,137,112,87,113],"text":"stay"},{"boundingBox":[142,86,163,86,164,112,142,112],"text":"at"},{"boundingBox":[169,86,242,85,242,111,169,112],"text":"home."},{"boundingBox":[248,85,308,84,309,110,248,111],"text":"More"},{"boundingBox":[314,84,362,83,362,109,314,110],"text":"info"},{"boundingBox":[368,83,380,83,380,109,368,109],"text":"&"}]},{"boundingBox":[20,115,275,114,276,142,21,143],"text":"exemptions at gov.uk/","words":[{"boundingBox":[23,118,158,115,158,144,23,141],"text":"exemptions"},{"boundingBox":[162,115,186,115,186,143,162,144],"text":"at"},{"boundingBox":[191,115,274,116,275,142,191,143],"text":"gov.uk/"}]},{"boundingBox":[20,146,322,145,323,171,21,172],"text":"coronavirus Stay at home.","words":[{"boundingBox":[22,149,158,146,158,173,22,170],"text":"coronavirus"},{"boundingBox":[165,146,215,146,214,173,165,173],"text":"Stay"},{"boundingBox":[222,146,246,146,245,172,221,173],"text":"at"},{"boundingBox":[250,146,323,147,322,170,249,172],"text":"home."}]},{"boundingBox":[20,175,343,174,344,200,21,201],"text":"Protect the NHS. Save lives.","words":[{"boundingBox":[22,178,109,176,109,201,22,200],"text":"Protect"},{"boundingBox":[113,176,151,176,151,201,113,201],"text":"the"},{"boundingBox":[156,176,221,175,221,201,156,201],"text":"NHS."},{"boundingBox":[226,175,280,175,281,201,226,201],"text":"Save"},{"boundingBox":[286,175,344,176,344,200,286,201],"text":"lives."}]}]}]}

为什么 JSON 没有被反序列化成结果?

【问题讨论】:

  • 您好,请问我在下面提供的答案是否对您的问题有帮助?如果有帮助,您能否将我的答案标记为“已接受”?先谢谢了~

标签: .net azure ocr microsoft-cognitive azure-cognitive-services


【解决方案1】:

原因是属性名称与TextOperationResult类中的属性不一样,所以无法通过属性名称反序列化为json。

我们可以在TextOperationResult类的代码中看到两个属性(StatusRecognitionResult)。

代码[JsonProperty(PropertyName = "status")]用于将Stauts对应到status,这样就可以得到result.Status成功。

代码[JsonProperty(PropertyName = "recognitionResult")]是用来对应RecognitionResultrecognitionResult,但是你的结果数据中的属性是recognitionResults,所以你的代码result.RecognitionResult不能获取数据成功,只会返回你具有空值。

希望对你有帮助~

【讨论】:

    【解决方案2】:

    您可能使用了错误的 API 对并相应地得到了​​错误的结果。

    请将 GetTextOperationResultAsync() 替换为 GetReadOperationResultAsync()

    参考BatchReadFile的示例代码:

    • 调用 BatchReadFileInStreamAsync() 来发布请求
    • 调用 GetReadOperationResultAsync() 获取结果

    这是您可能对RecognizeText 感兴趣的另一个示例:

    • 调用 RecognizeTextInStreamAsync() 来发布请求
    • 调用 GetTextOperationResultAsync() 获取结果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2013-12-19
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      • 2019-05-18
      相关资源
      最近更新 更多