C#WinForm App

时间:2016-12-02 13:45:39

标签: speech-recognition speech-synthesis google-speech-api microsoft-speech-platform microsoft-speech-api

我试图找到C#Windows窗体应用程序可执行文件的自由语音识别,它可以用作Google语音识别,识别并将绝对新词转换为文本。

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

所以我想尝试使用Microsoft Cognitive Services的Bing Speech API,但是找不到任何编码方法,一些基本的例子。如果有人处理过这个工具,你可以帮我搞清楚吗

1 个答案:

答案 0 :(得分: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;
            }));
        }
    }
}