如何以编程方式培训SpeechRecognitionEngine并在C#或vb.net中将音频文件转换为文本
有没有可能通过编程方式训练识别器给出.wavs而不是对着麦克风说话 如果是,怎么做?目前我有一个代码,可以在0.wav文件中对音频执行识别,并将识别的文本写入控制台如何以编程方式培训SpeechRecognitionEngine并在C#或vb.net中将音频文件转换为文本,c#,.net,vb.net,speech-recognition,C#,.net,Vb.net,Speech Recognition,有没有可能通过编程方式训练识别器给出.wavs而不是对着麦克风说话 如果是,怎么做?目前我有一个代码,可以在0.wav文件中对音频执行识别,并将识别的文本写入控制台 Imports System.IO Imports System.Speech.Recognition Imports System.Speech.AudioFormat Namespace SampleRecognition Class Program Shared completed As Boolea
Imports System.IO
Imports System.Speech.Recognition
Imports System.Speech.AudioFormat
Namespace SampleRecognition
Class Program
Shared completed As Boolean
Public Shared Sub Main(ByVal args As String())
Using recognizer As New SpeechRecognitionEngine()
Dim dictation As Grammar = New DictationGrammar()
dictation.Name = "Dictation Grammar"
recognizer.LoadGrammar(dictation)
' Configure the input to the recognizer.
recognizer.SetInputToWaveFile("C:\Users\ME\v02\0.wav")
' Attach event handlers for the results of recognition.
AddHandler recognizer.SpeechRecognized, AddressOf recognizer_SpeechRecognized
AddHandler recognizer.RecognizeCompleted, AddressOf recognizer_RecognizeCompleted
' Perform recognition on the entire file.
Console.WriteLine("Starting asynchronous recognition...")
completed = False
recognizer.RecognizeAsync()
' Keep the console window open.
While Not completed
Console.ReadLine()
End While
Console.WriteLine("Done.")
End Using
Console.WriteLine()
Console.WriteLine("Press any key to exit...")
Console.ReadKey()
End Sub
' Handle the SpeechRecognized event.
Private Shared Sub recognizer_SpeechRecognized(ByVal sender As Object, ByVal e As SpeechRecognizedEventArgs)
If e.Result IsNot Nothing AndAlso e.Result.Text IsNot Nothing Then
Console.WriteLine(" Recognized text = {0}", e.Result.Text)
Else
Console.WriteLine(" Recognized text not available.")
End If
End Sub
' Handle the RecognizeCompleted event.
Private Shared Sub recognizer_RecognizeCompleted(ByVal sender As Object, ByVal e As RecognizeCompletedEventArgs)
If e.[Error] IsNot Nothing Then
Console.WriteLine(" Error encountered, {0}: {1}", e.[Error].[GetType]().Name, e.[Error].Message)
End If
If e.Cancelled Then
Console.WriteLine(" Operation cancelled.")
End If
If e.InputStreamEnded Then
Console.WriteLine(" End of stream encountered.")
End If
completed = True
End Sub
End Class
End Namespace
编辑
我知道使用培训向导对此很有用
通过打开语音识别,单击开始来完成
按钮->控制面板->易于访问->语音识别
如何使用自定义wav甚至mp3文件自定义训练语音识别
使用培训向导(控制面板培训UI)时,培训文件存储在
{AppData}\Local\Microsoft\Speech\Files\TrainingAudio
如何使用或进行自定义培训而不是使用培训向导
语音控制面板在注册表项HKCU\Software\Microsoft\Speech\RecoProfiles\Tokens{ProfileGUID}{00000000-0000-0000-0000-0000000000000000}\files
由代码创建的注册表项是否必须放在其中
这样做的原因是我想使用自己的wav文件和单词和短语列表进行自定义培训,然后将所有内容传输到其他系统。您可以使用SAPI引擎(而不是托管api)生成自定义培训
这里有一个关于如何做到这一点(虽然有点含糊)当然可以使用C#来训练SAPI。您可以使用SAPI周围的speechlib包装器从C#访问培训模式API
- 创建inproc识别器并绑定适当的音频输入
- 确保保留音频以供识别;你以后会需要它的
- 创建包含要训练的文本的语法
- 将语法的状态设置为在识别发生时暂停识别器。(这也有助于从音频文件进行培训。) 识别发生时:
- 获取识别的文本和保留的音频
- 使用CoCreateInstance(CLSID_SpStream)创建流对象
- 使用ISpRecognizer::GetObjectToken和ISpObjectToken::GetStorageFileName创建培训音频文件,并将其绑定到流(使用ISpStream::BindToFile)
- 将保留的音频复制到流对象中
- 为ISpTranscript接口设置流对象,并使用ISpTranscript::AppendTranscript将识别的文本添加到流中
- 更新下一次讲话的语法,恢复识别器,然后重复,直到你没有训练文本
Public SharedRecognizer作为SpSharedRecognizer
作为iPeechObjectToken的公共识别器
专用子命令1_Click()
关于错误转到EH
将currentProfile设置为SpObjectToken
作为整数的Dim i
调暗T作为字符串
Dim TokenObject作为ISpeechObjectToken
设置currentProfile=SharedRecognizer.Profile
对于识别器,i=0。计数-1
Set-TokenObject=TheReceigners.Item(i)
如果tokenObject.Id currentProfile.Id,则
Set SharedRecognizer.Profile=TokenObject
T=“已安装新配置文件:”
T=T&SharedRecognizer.Profile.GetDescription
退出
其他的
T=“没有安装新的配置文件。”
如果结束
接下来我
MsgBox T,vb信息
呃,
如果错误编号,则为RMSG
端接头
专用子表单_加载()
关于错误转到EH
常量NL=vbNewLine
昏暗的i,我的位置一样长
调暗T作为字符串
Dim TokenObject作为SpObjectToken
设置SharedRecognizer=CreateObject(“SAPI.SpSharedRecognizer”)
设置theRecognizers=SharedRecognizer.GetProfiles
对于识别器,i=0。计数-1
Set-TokenObject=TheReceigners.Item(i)
T=T&TokenObject.GetDescription&“--”&NL&NL
idPosition=InStrRev(TokenObject.Id,“\”)
T=T&Mid(TokenObject.Id,idPosition+1)&NL
接下来我
MsgBox T,vb信息
呃,
如果错误编号,则为RMSG
端接头
私人分包商RMSG()
'声明标识符:
调暗T作为字符串
T=“Desc:”&错误描述&vbNewLine
T=T&“错误号:”&错误号
MsgBox T,vb感叹号,“运行时错误”
终点
端接头
Public SharedRecognizer As SpSharedRecognizer
Public theRecognizers As ISpeechObjectTokens
Private Sub Command1_Click()
On Error GoTo EH
Dim currentProfile As SpObjectToken
Dim i As Integer
Dim T As String
Dim TokenObject As ISpeechObjectToken
Set currentProfile = SharedRecognizer.Profile
For i = 0 To theRecognizers.Count - 1
Set TokenObject = theRecognizers.Item(i)
If tokenObject.Id <> currentProfile.Id Then
Set SharedRecognizer.Profile = TokenObject
T = "New Profile installed: "
T = T & SharedRecognizer.Profile.GetDescription
Exit For
Else
T = "No new profile has been installed."
End If
Next i
MsgBox T, vbInformation
EH:
If Err.Number Then ShowErrMsg
End Sub
Private Sub Form_Load()
On Error GoTo EH
Const NL = vbNewLine
Dim i, idPosition As Long
Dim T As String
Dim TokenObject As SpObjectToken
Set SharedRecognizer = CreateObject("SAPI.SpSharedRecognizer")
Set theRecognizers = SharedRecognizer.GetProfiles
For i = 0 To theRecognizers.Count - 1
Set TokenObject = theRecognizers.Item(i)
T = T & TokenObject.GetDescription & "--" & NL & NL
idPosition = InStrRev(TokenObject.Id, "\")
T = T & Mid(TokenObject.Id, idPosition + 1) & NL
Next i
MsgBox T, vbInformation
EH:
If Err.Number Then ShowErrMsg
End Sub
Private Sub ShowErrMsg()
' Declare identifiers:
Dim T As String
T = "Desc: " & Err.Description & vbNewLine
T = T & "Err #: " & Err.Number
MsgBox T, vbExclamation, "Run-Time Error"
End
End Sub