【问题标题】:C#: transcribe WAV file to text (speech-to-text) with System.Speech namespacesC#:使用 System.Speech 命名空间将 WAV 文件转录为文本(语音到文本)
【发布时间】:2010-12-18 15:04:31
【问题描述】:

如何使用 .NET 语音命名空间类将 WAV 文件中的音频转换为可以在屏幕上显示或保存到文件的文本形式?

我正在寻找一些教程示例。

更新

找到一个代码示例here。但是当我尝试它时,它给出了不正确的结果。下面是我采用的 vb 代码示例。 (实际上我不介意语言,只要它是 vb/c#...)。它没有给我正确的结果。我假设如果我们使用正确的语法 - 即我们在录音中期望的单词 - 我们应该得到它的文本输出。首先,我尝试使用通话中的示例词。它有时只打印那个(一个)单词而没有其他任何东西。然后我尝试了我们在录音中完全没想到的单词......不幸的是它也打印出来了...... :(

Imports System
Imports System.Speech.Recognition

Public Class Form1

    Dim WithEvents sre As SpeechRecognitionEngine

    Private Sub btnLiterate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLiterate.Click
        If TextBox1.Text.Trim.Length = 0 Then Exit Sub
        sre.SetInputToWaveFile(TextBox1.Text)
        Dim r As RecognitionResult
        r = sre.Recognize()
        If r Is Nothing Then
            TextBox2.Text = "Could not fetch result"
            Return
        End If
        TextBox2.Text = r.Text
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = String.Empty
        Dim dr As DialogResult
        dr = OpenFileDialog1.ShowDialog()
        If dr = Windows.Forms.DialogResult.OK Then
            If Not OpenFileDialog1.FileName.Contains("wav") Then
                MessageBox.Show("Incorrect file")
            Else
                TextBox1.Text = OpenFileDialog1.FileName
            End If
        End If
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        sre = New SpeechRecognitionEngine()

    End Sub

    Private Sub sre_LoadGrammarCompleted(ByVal sender As Object, ByVal e As System.Speech.Recognition.LoadGrammarCompletedEventArgs) Handles sre.LoadGrammarCompleted

    End Sub

    Private Sub sre_SpeechHypothesized(ByVal sender As Object, ByVal e As System.Speech.Recognition.SpeechHypothesizedEventArgs) Handles sre.SpeechHypothesized
        System.Diagnostics.Debug.Print(e.Result.Text)
    End Sub

    Private Sub sre_SpeechRecognitionRejected(ByVal sender As Object, ByVal e As System.Speech.Recognition.SpeechRecognitionRejectedEventArgs) Handles sre.SpeechRecognitionRejected
        System.Diagnostics.Debug.Print("Rejected: " & e.Result.Text)
    End Sub

    Private Sub sre_SpeechRecognized(ByVal sender As Object, ByVal e As System.Speech.Recognition.SpeechRecognizedEventArgs) Handles sre.SpeechRecognized
        System.Diagnostics.Debug.Print(e.Result.Text)
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim words As String() = New String() {"triskaidekaphobia"}
        Dim c As New Choices(words)
        Dim grmb As New GrammarBuilder(c)
        Dim grm As Grammar = New Grammar(grmb)
        sre.LoadGrammar(grm)
    End Sub

End Class

更新(11 月 28 日之后)

找到了一种加载默认语法的方法。它是这样的:

sre.LoadGrammar(New DictationGrammar)

这里仍然存在问题。识别不准确。输出是垃圾。对于一个 6 分钟的文件,它可能会提供 5-6 个与语音文件完全无关的文字。

【问题讨论】:

    标签: c# namespaces speech-to-text


    【解决方案1】:

    System.Speech 中的类用于文本到语音(主要是辅助功能)。

    您正在寻找语音识别。自 .Net 3.0 起就有 System.Speech.Recognition 命名空间可用。它使用 Windows 桌面语音引擎。这可能会让你开始,但我想那里有更好的引擎。

    语音识别非常复杂,很难做到,也有一些商业产品可用。

    【讨论】:

    • 这需要实际“播放”语音文件吗?
    • 也许是这样,然后像某些 cd ripper 那样将音频输出植根到麦克风输入。否则,请尝试检查 SpeechRecognizer.EmulateRecognize 如何与反射器一起使用。
    • 也许也可以检查一下:msdn.microsoft.com/en-us/library/… 抱歉,我不是这方面的专家。希望对你有帮助。
    • EmulateRecognize 无济于事 - 它旨在通过提供已识别的输出来绕过识别。
    【解决方案2】:

    我意识到这是一个老问题,但在后面的问题和答案中提供了更好的信息。例如见What is the best option for transcribing speech-to-text in a asp.net web app?

    您可以调用 SetInputToWaveFile() 来读取音频文件,而不是调用 SetInputToDefaultAudioDevice()。

    Windows Vista 和 Windows 7 中的桌面识别引擎包括听写语法,如参考答案所示。

    【讨论】:

      【解决方案3】:
      【解决方案4】:

      您应该使用SpeechRecognitionEngine。要使用波形文件,请致电SetInputToWaveFile。我希望我能帮助你更多,但我不是专家。

      哦,如果你的话真的是triskaidekaphobia,我认为即使是人类语音识别引擎也不会识别...

      【讨论】:

        【解决方案5】:

        我已经测试了你的代码,但它没有正确抓取波形文件。它正在捕捉

        如果不是 OpenFileDialog1.FileName.Contains("wav") 那么 MessageBox.Show("不正确的文件") 别的 TextBox1.Text = OpenFileDialog1.FileName 结束如果

        不是 else 条件。我也尝试在字符串中使用 .wav 。

        我还需要一个示例代码,用于将 wav 文件转录为不是来自麦克风的文本。如果您找到了一个好的解决方案,请在此处发布。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多