C#: transcribe WAV file to text (speech-to-text) with System.Speech namespaces

梦想与她 提交于 2019-12-03 13:04:00

问题


How do you use the .NET speech namespace classes to convert audio in a WAV file to textual form which I can display on the screen or save to file?

I am looking for some tutorial samples.

UPDATE

Found a code sample here. But when I tried it it gives incorrect results. Below is the vb code sample I've adopted. (Actually I don't mind the lang as long as its either vb/c#...). It is not giving me proper results. I assume if we put the right grammar - i.e. the words we expect in the recording - we should get the textual output of that. First I've tried with sample words that are in the call. It sometimes printed only that (one) word and nothing else. Then I tried words which we totally do not expect in the recording...Unfortunately it printed out that too... :(

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

UPDATE(after Nov 28th)

Found a way to load a default grammar. It goes something like this:

sre.LoadGrammar(New DictationGrammar)

There are still problems here. The recognition is not exact. The output is rubbish. For a 6min file it gives probably 5-6 words of text totally irrelevant to the voice file.


回答1:


The classes in System.Speech are for text to speech (primarily an acessibility feature).

You are looking for voice recognition. There is the System.Speech.Recognition namespace available since .Net 3.0. It uses the Windows Desktop Speech engine. This might get you started, but I guess there are better engines out there.

Voice recognition is very complicated and hard to do right, there are also some commercial products available.




回答2:


I realize this is an old question, but there is better information available in later questions and answers. For example see What is the best option for transcribing speech-to-text in a asp.net web app?

Instead of calling SetInputToDefaultAudioDevice() you can call SetInputToWaveFile() to read from an audio file.

The desktop recognition engine that comes in Windows Vista and Windows 7 includes a dictation grammar as shown in the referenced answer.




回答3:


You actually need Natural Language toolkit. In python I have used NTLK http://www.nltk.org/

In .Net I have just found Antelope https://stackoverflow.com/questions/1762040/natural-language-toolkit-equivalent-in-c

see the article as well http://en.wikipedia.org/wiki/Speech_recognition




回答4:


You should use the SpeechRecognitionEngine. To use a wave file, call SetInputToWaveFile. I wish I could help you more, but I'm no expert.

Oh, and if your word is really triskaidekaphobia, I don't think even a human speech recognition engine would recognize that...




回答5:


I have tested your code , but it is not grabbing wave file properly. It is catching

If Not OpenFileDialog1.FileName.Contains("wav") Then MessageBox.Show("Incorrect file") Else TextBox1.Text = OpenFileDialog1.FileName End If

Not the else condition. I tried using .wav in the string also.

I am also in need of a sample code for transcribing wav file to text not from Microphone. Please if u came to a good solution so please post it here.



来源:https://stackoverflow.com/questions/1768679/c-transcribe-wav-file-to-text-speech-to-text-with-system-speech-namespaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!