问题描述
你如何使用.NET讲话命名空间的类音频转换在文件,以文本的形式,我可以显示在屏幕上或保存到文件?
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.
找到一个代码示例的。但是,当我尝试了它给不正确的结果。下面是我通过VB代码示例。 (其实我不只要介意郎作为其无论是VB / C#...)。它不给我正确的结果。我认为,如果我们把正确的语法 - 即我们在记录期望的话 - 我们应该得到的文本输出。首先,我已经试过与在呼叫样的话。有时候只印了(一个)字,没有别的。然后我尝试这是我们完全的记录不希望的话...不幸的是打印出来的太...:(
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
更新(11月28日以后)
找到了一种方法来加载默认的语法。它是这样的:
UPDATE(after Nov 28th)
Found a way to load a default grammar. It goes something like this:
sre.LoadGrammar(New DictationGrammar)
还有问题在这里。识别不准确。输出是垃圾。对于一个6分钟的文件它给了大概5-6文本完全无关的语音文件的话。
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.
推荐答案
在系统中的类。语音是文本到语音(主要是一个acessibility功能)。
The classes in System.Speech are for text to speech (primarily an acessibility feature).
您正在寻找语音识别。还有,因为命名空间中可用.NET 3.0。它采用了Windows桌面语音引擎。这可能让你开始,但我想有更好的引擎在那里。
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.
这篇关于C#中:录制WAV文件为文本(语音到文本)与System.Speech命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!