gpt4 book ai didi

c# - 如何以编程方式训练 SpeechRecognitionEngine 并将音频文件转换为 C# 或 vb.net 中的文本

转载 作者:可可西里 更新时间:2023-11-01 08:46:46 26 4
gpt4 key购买 nike

是否可以通过编程方式训练识别器提供 .wavs 而不是对着麦克风说话?

如果是这样,该怎么做?目前我有对 0.wav 文件中的音频执行识别并将识别的文本写入控制台的代码。

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

编辑

我了解使用培训向导对此很有用

accomplished by Opening Speech Recognition,clicking Start button->Control Panel->Ease of Access->Speech Recognition

.

如何使用自定义 wav 甚至 mp3 文件自定义训练语音识别?

使用培训向导(控制面板培训用户界面)时,培训文件存储在{AppData}\Local\Microsoft\Speech\Files\TrainingAudio

我如何使用或进行自定义培训而不是使用培训向导?

语音控制面板在 key HKCU\Software\Microsoft\Speech\RecoProfiles\Tokens{ProfileGUID}{00000000-0000-0000-0000-0000000000000000}\Files 中为培训音频文件创建注册表项强>

代码创建的注册表项必须放在那里吗?

这样做的原因是我想用我自己的 wav 文件和单词和短语列表进行自定义训练,然后将它们全部传输到其他系统。

最佳答案

当然可以使用 C# 训练 SAPI。您可以使用围绕 SAPI 的 speechlib 包装器从 C# 访问训练模式 API。此处 @Eric Brown answered the procedure

  • 创建一个 inproc 识别器并绑定(bind)适当的音频输入。
  • 确保您保留了用于识别的音频;稍后您会需要它。
  • 创建包含要训练的文本的语法。
  • 设置语法的状态以在识别发生时暂停识别器。 (这也有助于通过音频文件进行训练。)

    识别发生时:

  • 获取识别的文本和保留的音频。

  • 使用 CoCreateInstance(CLSID_SpStream) 创建流对象。
  • 使用 ISpRecognizer::GetObjectToken 和 ISpObjectToken::GetStorageFileName 创建训练音频文件,并将其绑定(bind)到流(使用 ISpStream::BindToFile)。
  • 将保留的音频复制到流对象中。
  • QI ISpTranscript 接口(interface)的流对象,并使用 ISpTranscript::AppendTranscript 将识别的文本添加到流中。
  • 更新下一个话语的语法,恢复识别器,并重复直到你用完训练文本。

其他选项可以是用所需的输出训练 sapi 一次,然后使用代码获取配置文件并将其传输到其他系统,以下代码返回一个 ISpeechObjectTokens 对象。:

The GetProfiles method returns a selection of the available user speech profiles. Profiles are stored in the speech configuration database as a series of tokens, with each token representing one profile. GetProfiles retrieves all available profile tokens. The returned list is an ISpeechObjectTokens object. Additional or more detailed information about the tokens is available in methods associated with ISpeechObjectTokens. The token search may be further refined using the RequiredAttributes and OptionalAttributes search attributes. Only tokens matching the specified RequiredAttributes search attributes are returned. Of those tokens matching the RequiredAttributes key, OptionalAttributes lists devices in the order matching OptionalAttributes. If no search attributes are offered, all tokens are returned. If no audio devices match the criteria, GetAudioInputs returns an empty selection, that is, an ISpeechObjectTokens collection with an ISpeechObjectTokens::Count property of zero. See Object Tokens and Registry Settings White Paper for a list of SAPI 5-defined attributes.

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

关于c# - 如何以编程方式训练 SpeechRecognitionEngine 并将音频文件转换为 C# 或 vb.net 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14865623/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com