http://www.cyberforum.ru/vb-net/thread2489130.html#post13758454
В VS 2010, к проекту подключил Microsoft.Speech и Interop.SpeechLib
моя форма слышит, распознаёт и повторяет слова из моего словаря
Public Class Form1
Private MSTTS As New SpeechLib.SpVoice
Private CI As New System.Globalization.CultureInfo("ru-RU")
Private WithEvents SRE As New Microsoft.Speech.Recognition.SpeechRecognitionEngine(CI)
Private MyWordCollection As New Microsoft.Speech.Recognition.Choices
Private GB As New Microsoft.Speech.Recognition.GrammarBuilder
Private GR As Microsoft.Speech.Recognition.Grammar
Private MyWords() As String
Private ExitCommand As Boolean
Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
ReDim MyWords(14)
MyWords(0) = "адам"
MyWords(1) = "салим"
MyWords(2) = "артур"
MyWords(3) = "николай"
MyWords(4) = "тимур"
MyWords(5) = "вика"
MyWords(6) = "слушай"
MyWords(7) = "привет"
MyWords(8) = "заткнись"
MyWords(9) = "открой"
MyWords(10) = "калькулятор"
MyWords(11) = "интернет"
MyWords(12) = "ютуб"
MyWords(13) = "закройся"
MyWords(14) = "отвечай"
SRE.SetInputToDefaultAudioDevice()
MyWordCollection.Add(MyWords)
GB.Culture = CI
GB.Append(MyWordCollection)
GR = New Microsoft.Speech.Recognition.Grammar(GB)
SRE.LoadGrammar(GR)
SRE.RecognizeAsync(Microsoft.Speech.Recognition.RecognizeMode.Multiple)
MSTTS.Speak("<PITCH MIDDLE=""+10"">Слушаю</PITCH>")
End Sub
Private Sub SRE_AudioStateChanged(ByVal sender As Object, ByVal e As Microsoft.Speech.Recognition.AudioStateChangedEventArgs) Handles SRE.AudioStateChanged
Label1.Text = "Слушаю"
End Sub
Private Sub SRE_SpeechHypothesized(ByVal sender As Object, ByVal e As Microsoft.Speech.Recognition.SpeechHypothesizedEventArgs) Handles SRE.SpeechHypothesized
Dim result As Microsoft.Speech.Recognition.RecognitionResult = e.Result
Label1.Text = "возможно " & result.Text
End Sub
Private Sub SRE_SpeechRecognitionRejected(ByVal sender As Object, ByVal e As Microsoft.Speech.Recognition.SpeechRecognitionRejectedEventArgs) Handles SRE.SpeechRecognitionRejected
MSTTS.Speak("<PITCH MIDDLE=""+10"">Не поняла</PITCH>")
End Sub
Private Sub SRE_SpeechRecognized(ByVal sender As Object, ByVal e As Microsoft.Speech.Recognition.SpeechRecognizedEventArgs) Handles SRE.SpeechRecognized
Dim result As Microsoft.Speech.Recognition.RecognitionResult = e.Result
MSTTS.Speak("<PITCH MIDDLE=""+10"">" & result.Text & "</PITCH>")
Label1.Text = "точно " & result.Text
End Sub
End Class
Когда программа была ещё на VBScript'е и распознавались только английские слова, работал режим 'диктанта'.
Dim c_SGDSActive '23, c_SGDSInactive, c_SGDSActiveWithAutoPause, c_SGDSActiveUserDelimited
c_SGDSActive = 1 ' c_SGDSInactive = 0 c_SGDSActiveWithAutoPause = 3 c_SGDSActiveUserDelimited = 4
Dim AudioInput_Tokens '25 - 27
AudioInput_Tokens = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\AudioInput\TokenEnums\MMAudioIn\"
'8 all objects here
Dim SpVoice, h_Context, h_Recognizer, h_Grammar, h_Category, h_Token, gram, colorRule, colorslist
Set SpVoice = WScript.CreateObject("SAPI.SpVoice")
Set SpVoice.Voice = SpVoice.GetVoices.Item(0) 'msgbox SpVoice.Voice.GetDescription
SpVoice.Volume = 100
'13
AtoT
WScript.Quit
Private Function AtoT()
'SpVoice.Speak "Start"
'18
Set h_Context = WScript.CreateObject("SAPI.SpInProcRecoContext", "SpRecEvent_")
Set h_Recognizer = h_Context.Recognizer
Set h_Grammar = h_Context.CreateGrammar()
h_Grammar.Dictationload
h_Grammar.DictationSetState(c_SGDSActive)
Set h_Category = WScript.CreateObject("SAPI.SpObjectTokenCategory")
h_Category.SetId(AudioInput_Tokens)
Set h_Token = WScript.CreateObject("SAPI.SpObjectToken")
h_Token.SetId(AudioInput_Tokens)
Set h_Recognizer.AudioInput = h_Token '28 Set h_Recognizer.AudioInput = h_Recognizer.GetAudioInputs().Item(1)
'29
Set colorslist = WScript.CreateObject("SAPI.SpInProcRecognizer")
For Each gram in colorslist.GetRecognizers("Telephony")
WScript.Echo gram.GetDescription
Next
wscript.sleep 9000
SpVoice.Speak "Green"
wscript.sleep 10000000
'Set h_ObjectEvents = h_Context.Events("SpRecEvent_")
End Function
Function SpRecEvent_Recognition(StreamNumber, StreamPosition, RecognitionType, Result)
WScript.Echo "= " & Result.PhraseInfo.GetText
SpVoice.Speak "<PITCH MIDDLE=""+10""><RATE SPEED=""1"">" & Result.PhraseInfo.GetText & "</RATE></PITCH>"
End Function
Function SpRecEvent_Hypothesis(StreamNumber, StreamPosition, Result)
WScript.Echo "~ " & Result.PhraseInfo.GetText
End Function
Я пока не понял, возможна ли такая же диктовка, но на русском. Подскажите пожалуйста.
Может знатоки даже готовый пример покажут? Было бы круто.
Ответить
|