Как использовать пространство имен «Microsoft.CognitiveServices.Speech» с функциями Azure? - PullRequest
1 голос
/ 11 июля 2019

Я тестирую службы речи Microsoft Azure, в частности пытаюсь использовать Text-To-Speech. Итак, я использую бесплатный слой Azure и создал функцию TimeTrigger Azure для чтения электронной почты, просмотра HTML-кода и последующего вызова речевой службы с помощью SDK Microsoft.CognitiveServices.Speech. Я использую function.proj для загрузки пакетов nuget, загружая S22.Imap и HtmlAgilityPack без проблем. Но речевой пакет вызывает исключение: Unable to load DLL 'Microsoft.CognitiveServices.Speech.core.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E). Могу ли я использовать этот пакет в функции Azure? Если так, что я делаю не так?

Я попытался удалить строку <PackageReference Include="Microsoft.CognitiveServices.Speech" Version="1.6.0" /> из function.proj и удалил project.assets.json, чтобы перезагрузить пакет, но он не сработал.

Это мой function.proj:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="S22.Imap" Version="3.6.0" />
        <PackageReference Include="HtmlAgilityPack" Version="1.11.9" />
        <PackageReference Include="Microsoft.CognitiveServices.Speech" Version="1.6.0" />
    </ItemGroup>
</Project>

А это мой run.csx:

using System;
using S22.Imap;
using System.Net.Mail;
using HtmlAgilityPack;
using System.Threading.Tasks;
using Microsoft.CognitiveServices.Speech;
using System.Diagnostics;

public static void Run(TimerInfo myTimer, ILogger log)
{
    var username = "sample@gmail.com";
    var password = "sample";
    var subsKey = "sample";

    using(ImapClient client = new ImapClient("imap.gmail.com", 993, username, password, AuthMethod.Login, true))
    {
        IEnumerable<uint> uids = client.Search(SearchCondition.From("sample@sample.com"));
        IEnumerable<MailMessage> messages = client.GetMessages(uids);
        log.LogInformation($"Count: {messages.Count()}.");

        var msg = messages.FirstOrDefault();
        if(msg != null)
        {
            var doc = new HtmlDocument();
            doc.LoadHtml(msg.Body);

            var paragraphs = doc.DocumentNode.Descendants()
                .Where(x => x.Name == "p" && !string.IsNullOrEmpty(x.InnerText.Trim()))
                .ToList();

            var mailText = string.Empty;
            foreach(var par in paragraphs)
                mailText += par.InnerText;

            if(!string.IsNullOrEmpty(mailText))
            {
                var config = SpeechConfig.FromSubscription(subsKey, "myregion");
                config.SetSpeechSynthesisOutputFormat(SpeechSynthesisOutputFormat.Audio24Khz160KBitRateMonoMp3);
                config.SpeechSynthesisLanguage = "pt-BR";

                using (var synthesizer = new SpeechSynthesizer(config))
                {
                    using (var result = synthesizer.SpeakTextAsync(mailText).Result)
                    {
                        if (result.Reason == ResultReason.SynthesizingAudioCompleted)
                        {
                            //Do something with it
                        }
                        else if (result.Reason == ResultReason.Canceled)
                        {
                            var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
                            log.LogError($"CANCELED: Reason={cancellation.Reason}");

                            if (cancellation.Reason == CancellationReason.Error)
                            {
                                log.LogError($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                                log.LogError($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
                            }
                        }
                    }
                }
            }
        }
    }
}

1 Ответ

1 голос
/ 12 июля 2019

Вы можете попытаться удалить function.proj, а затем воссоздать его и сначала добавить Microsoft.CognitiveServices.Speech.

Убедитесь, что Microsoft.CognitiveServices.Speech.core.dll установлен в win-x86 и win-x64. Пожалуйста, обратитесь к этому выпуск .

enter image description here

...