Microsoft Emotions API, RecognizeInVideoAsync c#

本文关键字:RecognizeInVideoAsync API Emotions Microsoft | 更新日期: 2023-09-27 18:13:26

我试图通过在视频中使用识别来使用微软情感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();
}

Microsoft Emotions API, RecognizeInVideoAsync c#

这就是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();
   ...
}
相关文章:
  • 没有找到相关文章