1

我正在尝试为 C# Windows 窗体应用程序可执行文件找到免费的语音识别,它可以作为谷歌语音识别,识别并将绝对新词转换为文本。

我尝试使用 System.Speech.Recognition;不同的方式,但它适用于预先录制的命令,我无法获得这样的结果,例如它与 Python 的 Google Speech Recognition 一起使用,这至少是 95% 的正确结果,足以说,这很好,但显然,如果我没有密钥,它就不能免费使用,也不能在可执行文件中使用。

所以我想试试微软认知服务的 Bing Speech API,但找不到任何如何编码的例子,一些基本的例子。如果有人处理过这个工具,你能帮我弄清楚吗

4

1 回答 1

1

嗨,也许这可以帮助你一个简单的 bing 语音 api 示例,这不是 winform 这是用于 WPF 应用程序 C#

using Microsoft.CognitiveServices.SpeechRecognition;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.ProjectOxford.SpeechRecognition;
using System.Threading;
using System.Configuration;

namespace BingSpeech
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        AutoResetEvent _FinalResponceEvent;
        MicrophoneRecognitionClient _microphoneRecognitionClient;
        public MainWindow()
        {
            InitializeComponent();
            RecordButton.Content = "Start\nRecording";
            _FinalResponceEvent = new AutoResetEvent(false);
            OutputTextbox.Background = Brushes.White;
            OutputTextbox.Foreground = Brushes.Black;
        }

        private void RecordButton_Click(object sender, RoutedEventArgs e)
        {
            RecordButton.Content = "Listening ...";
            RecordButton.IsEnabled = false;
            OutputTextbox.Background = Brushes.Green;
            OutputTextbox.Foreground = Brushes.White;
            ConvertSpeechToText();
        }

        private void ConvertSpeechToText()
        {
            var speechRecognitionMode = SpeechRecognitionMode.ShortPhrase;
            string language = "en-us";
            string subscriptionKey = ConfigurationManager.AppSettings["MicrosoftSpeechApiKey"].ToString();
            _microphoneRecognitionClient = SpeechRecognitionServiceFactory.CreateMicrophoneClient(
                speechRecognitionMode,
                language,
                subscriptionKey
                 );
            _microphoneRecognitionClient.OnPartialResponseReceived += OnPartialResponseReceivedHandler;
            _microphoneRecognitionClient.OnResponseReceived += OnMicShortPhraseResponceReceivedHandler;
            _microphoneRecognitionClient.StartMicAndRecognition();
        }

        private void OnPartialResponseReceivedHandler(object sender, PartialSpeechResponseEventArgs e)
        {
            string result = e.PartialResult;
            jarvis.SpeakAsync(e.PartialResult);
            Dispatcher.Invoke(() =>
            {
                OutputTextbox.Text = (e.PartialResult);
                OutputTextbox.Text += ("\n");

            });
        }
        private void OnMicShortPhraseResponceReceivedHandler(object sender, SpeechResponseEventArgs e)
        {
            Dispatcher.Invoke((Action)(() =>
            {
                _FinalResponceEvent.Set();
                _microphoneRecognitionClient.EndMicAndRecognition();
                _microphoneRecognitionClient.Dispose();
                _microphoneRecognitionClient = null;
                RecordButton.Content = "Start\nRecording";
                RecordButton.IsEnabled = true;
                OutputTextbox.Background = Brushes.White;
                OutputTextbox.Foreground = Brushes.Black;
            }));
        }
    }
}
于 2016-12-09T07:37:11.593 回答