2013-02-14 63 views
10

是否有可能以編程方式訓練識別器給出.wavs而不是與麥克風對話?如何以編程方式訓練SpeechRecognitionEngine並將音頻文件轉換爲c#或vb.net中的文本

如果是這樣,怎麼做呢?,目前我的代碼在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 

編輯

我瞭解使用學習嚮導是要做到這一點

通過打開語音識別來完成,單擊開始 有用的按鈕 - >控制面板 - >訪問 - 易於>語音識別

如何自定義訓練自定義wav甚至mp3文件的語音識別?

當使用訓練嚮導(控制面板訓練UI)培訓文件存儲在 {AppData的} \本地\微軟\語音\文件\ TrainingAudio

如何使用或進行自定義培訓而不是使用培訓嚮導?

的語音控制面板中的關鍵HKCU \軟件\微軟\語音\ RecoProfiles \ {令牌} ProfileGUID培訓音頻文件{00000000-0000-0000-0000-0000000000000000} \文件創建註冊表項

由代碼創建的註冊表項必須放在那裏嗎?

這樣做的原因是我想用我自己的wav文件和單詞和短語列表自定義訓練,然後將所有文件轉移到其他系統。

回答

2

可以使用SAPI引擎(非託管API)

下面是關於如何做一個link生成定製的訓練它(雖然有點模糊)

4

這當然可以使用C#來訓練SAPI。您可以使用speechlib包裝器SAPI從C#進入訓練模式的API。在這裏@Eric Brown answered the procedure

  • 創建一個進程內識別&綁定相應的音頻輸入。
  • 確保您保留音頻以獲得您的認可;你稍後需要它。
  • 創建一個包含要訓練的文本的語法。
  • 設置語法狀態以在發生識別時暫停識別器。 (這有助於從一個音頻文件的訓練,也是如此。)

    當識別發生:

  • 獲取識別的文本和保留音頻。

  • 使用CoCreateInstance(CLSID_SpStream)創建一個流對象。
  • 使用ISpRecognizer :: GetObjectToken和ISpObjectToken :: GetStorageFileName創建培訓音頻文件,並將其綁定到流(使用ISpStream :: BindToFile)。
  • 將保留的音頻複製到流對象中。
  • QI ISpTranscript接口的流對象,並使用ISpTranscript :: AppendTranscript將已識別的文本添加到流中。
  • 更新下一個話語的語法,恢復識別器,並重復,直到你沒有訓練的文本。

其他選項可以與期望輸出一次訓練SAPI,然後得到型材代碼和交通運輸到其他系統,下面的代碼返回一個對象ISpeechObjectTokens:

的GetProfiles方法返回一個選擇可用用戶 語音配置文件。配置文件作爲一系列令牌存儲在語音配置數據庫 中,每個令牌代表一個 配置文件。 GetProfiles檢索所有可用的配置文件標記。 返回的列表是一個ISpeechObjectTokens對象。額外或更多 有關標記的詳細信息可在與ISpeechObjectTokens關聯的方法 中獲得。令牌搜索可以進一步 使用RequiredAttributes和OptionalAttributes搜索 屬性進行細化。僅返回與指定的RequiredAttributes 搜索屬性匹配的令牌。在那些與 RequiredAttributes密鑰匹配的令牌中,OptionalAttributes按照 與OptionalAttributes匹配的順序列出設備。如果沒有提供搜索屬性,則返回所有 令牌。如果沒有音頻設備符合標準,則 GetAudioInputs將返回一個空選擇,即具有零聲級ISpeechObjectTokens :: Count 屬性的ISpeechObjectTokens集合。 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 
相關問題