与Microsoft BotFramework集成的Bing Speech API

时间:2018-07-27 16:02:26

标签: c# botframework speech-to-text microsoft-cognitive bing-speech

我认为我会尽可能详细地说明这一点,希望在那里的人对这种设置有一定的经验。

前端:ASP.Net MVC Razer网站。

  • .Net Framework 4.6.1

后端:Bot框架Web API(RESTful)。

  • .Net Framework 4.6

后端::我使用各种位于Azure的认知服务,但在这种情况下,它只是Bing Speech API。

相关SDK:

  • Microsoft.Bing.Speech(版本:2.0.2)
    • Bond.Core.CSharp(版本:8.0.0)〜依赖项
    • Bond.CSharp(版本:8.0.0)〜依赖项
    • Bond.Runtime.CSharp(版本:8.0.0)〜依赖项

我正在网站上使用getUserMedia来录制一些麦克风代码请求的用户麦克风,这会创建一个Blob URL。

然后,我将ContentUrl中的blob网址作为Attachment传递给Activity

当这碰到Bot框架时,我会进行一些基本验证(与此问题无关),然后传递给自定义Dialog<T>

这是 我在努力 的地方,让Bing Speech API可以执行我想要的操作。

我从Dialog<T>内部使用此方法:

public async Task Run(string audioFile, string locale, Uri serviceUrl)
{
    // create the preferences object
    var preferences = new Preferences(locale, serviceUrl, new CognitiveServicesAuthorizationProvider(subscriptionKey));

    using (var speechClient = new SpeechClient(preferences))
    {
        speechClient.SubscribeToPartialResult(this.OnPartialResult);
        speechClient.SubscribeToRecognitionResult(this.OnRecognitionResult);

        using (WebClient webClient = new WebClient())
        {
            using (Stream stream = webClient.OpenRead(audioFile))
            {
                var deviceMetadata = new DeviceMetadata(DeviceType.Near, DeviceFamily.Desktop, NetworkType.Ethernet, OsName.Windows, "1607", "Dell", "T3600");
                var applicationMetadata = new ApplicationMetadata("SampleApp", "1.0.0");
                var requestMetadata = new RequestMetadata(Guid.NewGuid(), deviceMetadata, applicationMetadata, "SampleAppService");

                try
                {
                    await speechClient.RecognizeAsync(new SpeechInput(stream, requestMetadata), this.cts.Token).ConfigureAwait(false);
                }
                catch (Exception genEx)
                {
                    // Was just using this try/catch for debugging reasons
                }
            }
        }
    }
}

我正在使用 WebClient 来获取流,而不是此方法在Microsoft中使用的 FileStream 示例代码,因为 Filestream 不会从网址流中获取。

当前问题:

点击此行:

await speechClient.RecognizeAsync(new SpeechInput(stream, requestMetadata), this.cts.Token).ConfigureAwait(false);

它会引发关于Bond.IO.dll的错误

融合日志:

我正在使用Microsoft Bot Framework Emulator在本地调试,这就是为什么您会看到本地文件路径的原因。

=== Pre-bind state information ===
LOG: DisplayName = Bond.IO, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
 (Fully-specified)
LOG: Appbase = file:///[project folder]
LOG: Initial PrivatePath = \bin
Calling assembly : Microsoft.Bing.Speech, Version=2.0.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file:\web.config
LOG: Using host configuration file: \aspnet.config
LOG: Using machine configuration file from \machine.config.
LOG: Post-policy reference: Bond.IO, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: Attempting download of new URL file:///C:/Users/[USER]/AppData/Local/Temp/Temporary ASP.NET Files/vs/0f4bb63f/ca796715/Bond.IO.DLL.
LOG: Attempting download of new URL file:///C:/Users/[USER]/AppData/Local/Temp/Temporary ASP.NET Files/vs/0f4bb63f/ca796715/Bond.IO/Bond.IO.DLL.
LOG: Attempting download of new URL file:///C:/[USER]/[PROJECT PATH]/bin/Bond.IO.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

奇怪的是,如果我将bing api回滚到2.0.1并手动插入示例项目中安装的Bond.IO软件包的旧版本(版本4.0.1),则不会抛出此错误,它还会引发其他错误。

真的问:

如果我只想将.wav音频文件发送到我的API,然后使用Bing.Speech API的转录功能将语音转换为文本,那么最好的方法是什么这个?我至少在朝着正确的方向前进。

奖金 如果您的答案与我已经做过的事情有关,则指向积分。

2 个答案:

答案 0 :(得分:3)

  

我正在使用WebClient来获取Stream,而不是此方法在Microsoft示例代码中使用的FileStream,因为Filestream不会从URL进行流传输。

并非所有流都具有相同的功能。 FileStream是读/写随机访问流。 NetworkStream是仅向前的只读流。

因此,在将.wav缓冲到MemoryStream之前,将其传递给API。

{{ book.booktypes }}

答案 1 :(得分:0)

尽管David的回答绝对是一个不错的选择(正如我最肯定的是混合使用流),但烦人的是,上述问题的实际答案是对Microsoft.Bing.Speech API的有限支持之一。

在github上Bond.IO项目上工作的人们介绍了较低版本和nuget当前列出的两个最新版本(7.0.1和8.0.0)之间的重大变化。

  

这是故意在5.x和6.x之间进行的重大更改,以启用   微软以外的人来构建和使用具有名字的签名的Bond   程序集。


  

突破性的变化现在,债券组合件已使用存储库中的bond.snk密钥而不是Microsoft密钥进行了强名称签名。这样,任何人都可以生成兼容的程序集,而不仅仅是Microsoft。债券的官方发行将继续>>   Authenticode已使用Microsoft证书签名。问题#414


  

程序集的新公共密钥现在为 [截短公共密钥示例]

     

突破性变化Bond程序集现在具有与其NuGet软件包版本相对应的程序集和文件版本。强名   现在,身份将根据NuGet更改发布版本   软件包版本。问题#325 1

这似乎意味着将Microsoft.Bing.Speech api升级到最新版本2.0.1和2.0.2(请记住,这是nuget上仅有的两个版本)只能安装Bond.IO 7.0。 1或以上。但是,它们仍然包含Bond.IO的1.0.0.0版的内部要求(或更明确地说,是7.0.1之前的任何版本)。

值得一提的是,如果您从microsoft示例项目中手动安装了以Microsoft.Bing.Speech程序集和Bond.IO版本4.2.1程序集的较旧版本为目标的程序包,则上述代码可以正常工作。 2

Microsoft.Bind.Speech程序集即将贬值的一位贡献者在Microsoft Docs页面的其中之一上也发表了评论(如果他们将其标记为这样,那会很好,对吗? 。) 3

总结,对于上述问题,最接近的答案是,除非您要使用没有持续支持的过时程序集,否则请不要使用Microsoft.Bing.Speech nuget程序包。他们建议改用Speech SDK(尽管如果在BotFramework WebAPI中使用它,则要准备一场艰苦的战斗,因为它本身也有一些内部错误)。 4 。 / p>

我已经花了几天的时间进行研究,因此我非常有信心这是该库的当前状态。


1 Please see this issue against the Bond.IO Github

2 Comment on a similar question supporting this.

3 Look under closed comments at the bottom of this page, the response by 'Zhouwangzw' suggests using the latest Speech SDK.

3 Found the GitHub issue that linked to the docs here

4 Current breaking error in a webAPI using the Speech SDK.