使用System.Speech将音频文件转换为文本

时间:2011-05-10 07:27:24

标签: c# speech-recognition speech-to-text system.speech.recognition

我希望将通过16000的Android手机录制的.wav文件转换为使用C#的文本;即System.Speech命名空间。我的代码如下所述;

recognizer.SetInputToWaveFile(Server.MapPath("~/spoken.wav"));
recognizer.LoadGrammar(new DictationGrammar());
RecognitionResult result = recognizer.Recognize();
label1.Text = result.Text;

与样本.wav“Hello world”文件完美配合。然而,当我在手机上录制某些内容并尝试转换为电脑时,转换后的文字并没有接近我所记录的内容。有没有办法确保音频文件准确转录?

3 个答案:

答案 0 :(得分:3)

手机的音频文件录制的格式是什么?文件是否编码? Microsoft识别器支持PCM,ALaw和ULaw。确保以支持的格式录制。您可以查看RecognizerInfo.SupportedAudioFormats属性 - http://msdn.microsoft.com/en-us/library/system.speech.recognition.recognizerinfo.supportedaudioformats(v=VS.90).aspx并检查识别器版本支持的格式。

您是否收听了手机上录制的文件?吵闹吗?听起来很清楚吗?确保您为识别器提供最佳的声音。

由于您使用的是听写语法,我假设您使用的是Windows 7.您是否尝试过训练识别器?我的理解是,通过培训可以提高听写语法表现,并且标准的Windows 7语音识别培训将有助于其表现 - http://windows.microsoft.com/en-US/windows7/Set-up-Speech-Recognition

StackOverflow上的其他一些问题也可能会为您提供一些见解。请参阅good Speech recognition API开始。

答案 1 :(得分:-1)

您可以在此处找到您想要的完整实施细节:

Converting WAV audio to text using System.Speech

答案 2 :(得分:-2)

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 

或尝试此链接 Audio to text software free