【问题标题】:Microsoft Emotions API, RecognizeInVideoAsync c#Microsoft Emotions API,RecognizeInVideoAsync c#
【发布时间】:2017-02-27 13:54:21
【问题描述】:

我试图通过在视频中使用识别来使用 Microsft Emotions Api,所以我下载了客户端库并尝试使用它,但是当我调试时,它只是毫无例外地退出,我认为它可能是一个线程问题 -它发生在方法中:“recognizeInVideoAsync”。

我的代码:

public static async void testEmotionApi()
{
    var emotionServiceClient = new EmotionServiceClient("c580db97556e405980212f3ff31ac762");

    VideoEmotionRecognitionOperation videoOperation;
    using (var fs = new FileStream(@"D:\Downloads\testForApp.mp4", FileMode.Open))
    {
        videoOperation = await emotionServiceClient.RecognizeInVideoAsync(fs);
    }

    VideoOperationResult operationResult;
    while (true)
    {
        operationResult = await emotionServiceClient.GetOperationResultAsync(videoOperation);
        if (operationResult.Status == VideoOperationStatus.Succeeded || operationResult.Status == VideoOperationStatus.Failed)
        {
            break;
        }

        Task.Delay(30000).Wait();
    }

    var emotionRecognitionJsonString = operationResult.ToString();
}

【问题讨论】:

标签: c# multithreading api microsoft-cognitive emotion


【解决方案1】:

这就是异步编程在 C# 中的工作方式。虽然在源代码形式中您似乎只有一个方法,但实际上该方法在await 边界处分为多个部分。换句话说,正如您编写的那样,testEmotionApi 方法在调用RecognizeInVideoAsync 后返回。该方法的其余部分将在异步调用完成时执行,但您无法等待该结果。你可以做的是:

public static async Task<VideoOperationResult> testEmotionApi()
{
    // everything here the same, except...
    return operationResult;
}

public async Task callEmotionTestApi()
{
    VideoOperationResult result = await testEmotionApi();
    ...
}

或者,如果您不希望调用者异步,

public void callEmotionTestApi()
{
   VideoOperationResult result = testEmotionApi().GetAwaiter().GetResult();
   ...
}

【讨论】:

    猜你喜欢
    • 2017-03-15
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    相关资源
    最近更新 更多