Autoit en español
Aplicaciones con Autoit - Juan Antonio Villalpando
-- Tutorial de iniciación a Autoit --
Volver al índice del tutorial
____________________________
30.- Reconocedor de voz. Utter Speech Recognition.
- Hablamos delante de un micrófono una serie de palabras ya configuradas y el AutoIt las muestra.
- También podemos hablar normalmente a dictado y el AutoIt escribe esas palabras.
- https://www.autoitscript.com/forum/files/file/357-utter-speech-recognition-udf/
- Utter 3.0.0.1.zip
_______________________________________________
1.- Ejemplo de reconocimiento de palabra por voz.
- Bajamos y descomprimimos el archivo Utter 3.0.0.1.zip
- Ponemos un micrófono en nuestro ordenador
- Abrimos el ejemplo Example 2 (Word Recognition)
- Ponemos una serie de palabras en inglés o en español para probar: "Love|Car|Exit|Follow|Autoit|Uno|Dos|Tres|Izquierda|Derecha"
- Pronunciamos esas palabras y veremos en la parte de abajo de la ventana del SciTe (ConsoleWrite) las palabras que hemos pronunciado.
Example 2 (Word Recognition) |
#include "./Include/Utter.au3"
;here "|" is default GUIDataSeparatorChar delimiter so string will be split from the delimiter
$recognize = _Utter_Speech_StartEngine() ;initializes the engine
_Utter_Speech_CreateGrammar($recognize,"Love|Car|Exit|Follow|Autoit|Uno|Dos|Tres|Izquierda|Derecha") ;Creates a grammar with the words
_Utter_Speech_CreateTokens($recognize) ;Creates a token for registering speech recognition
_Utter_Speech_GrammarRecognize($recognize,"",0,"_spchin") ;Starts the recognition and calls the word recognized to the _spchin function
While 1
Sleep (50)
WEnd
_Utter_Speech_ShutdownEngine() ;shutdowns the function
Func _spchin($dummy)
ConsoleWrite($dummy)
Switch $dummy
Case "exit" ;Exit the script when the user says exit
Exit
EndSwitch
EndFunc
|
_______________________________________________
2.- Pronuncias un color y se muestra un círculo con ese color.
- Basado en el ejemplo anterior. Aparece un círculo relleno de un color, según pronunciemos las palabras rojo, azul, amarillo, verde, blanco o negro, el interior del círculo cambia de color.
Círculo de color |
#include "./Include/Utter.au3"
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <SendMessage.au3>
$Form1 = GUICreate("Colores - Juan A. Villalpando", 400, 400, 200, 200)
GUISetBkColor(0xffffff, $Form1)
; Juan A. Villalpando. KIO4.COM. juana1991@yahoo.com
$circulo = GUICtrlCreateGraphic(100, 50, 150, 150)
GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0x000000, 0xFF0000) ;== Rojo
GUICtrlSetGraphic(-1, $GUI_GR_ELLIPSE, 40, 50, 150, 150)
GUISetState()
$reconoce = _Utter_Speech_StartEngine()
_Utter_Speech_CreateGrammar($reconoce,"Rojo|Verde|Azul|Amarillo|Blanco|Negro")
_Utter_Speech_CreateTokens($reconoce)
_Utter_Speech_GrammarRecognize($reconoce,"",0,"_spchin")
While 1
$msg = GUIGetMsg()
Switch $msg
Case $GUI_EVENT_CLOSE
ExitLoop
EndSwitch
WEnd
_Utter_Speech_ShutdownEngine()
Func _spchin($color)
ConsoleWrite($color)
Switch $color
Case "Rojo"
GUICtrlSetGraphic($circulo, $GUI_GR_COLOR, 0x0000ff, 0xff0000)
GUICtrlSetGraphic(-1, $GUI_GR_ELLIPSE, 40, 50, 150, 150)
GUICtrlSetGraphic($circulo, $GUI_GR_REFRESH)
Case "Verde"
GUICtrlSetGraphic($circulo, $GUI_GR_COLOR, 0x0000ff, 0x00ff00)
GUICtrlSetGraphic(-1, $GUI_GR_ELLIPSE, 40, 50, 150, 150)
GUICtrlSetGraphic($circulo, $GUI_GR_REFRESH)
Case "Azul"
GUICtrlSetGraphic($circulo, $GUI_GR_COLOR, 0x0000ff, 0x0000ff)
GUICtrlSetGraphic(-1, $GUI_GR_ELLIPSE, 40, 50, 150, 150)
GUICtrlSetGraphic($circulo, $GUI_GR_REFRESH)
Case "Amarillo"
GUICtrlSetGraphic($circulo, $GUI_GR_COLOR, 0x0000ff, 0xffff00)
GUICtrlSetGraphic(-1, $GUI_GR_ELLIPSE, 40, 50, 150, 150)
GUICtrlSetGraphic($circulo, $GUI_GR_REFRESH)
Case "Blanco"
GUICtrlSetGraphic($circulo, $GUI_GR_COLOR, 0x0000ff, 0xffffff)
GUICtrlSetGraphic(-1, $GUI_GR_ELLIPSE, 40, 50, 150, 150)
GUICtrlSetGraphic($circulo, $GUI_GR_REFRESH)
Case "Negro"
GUICtrlSetGraphic($circulo, $GUI_GR_COLOR, 0x0000ff, 0x000000)
GUICtrlSetGraphic(-1, $GUI_GR_ELLIPSE, 40, 50, 150, 150)
GUICtrlSetGraphic($circulo, $GUI_GR_REFRESH)
EndSwitch
EndFunc
|
_______________________________________________
3.- Pronuncias el nombre de una aplicación y se ejecuta o se cierra.
- Basado en el ejemplo anterior. Pronuncia el nombre de una de las aplicaciones configuradas en el programa y esa aplicación se ejecuta.
- Cuando pronunciamos:
"Paint|Bloc de notas|Calculadora|Word|Símbolo de sistema|Reproductor de Windows|Cerrar Bloc de notas|Cerrar Paint|Cerrar Calculadora"
se abre o cierra uno de esos programas.
- Observa la dirección de las aplicaciones, tal vez debas cambiar alguna, por el ejemplo la del Word, para que te funcione.
- Fíjate en un detalle que he comentado en tutoriales anteriores, si el archivo Utter.au3 está en la carpeta Include del AutoIt, es decir en...
C:\Program Files (x86)\AutoIt3\Include pondríamos #include <Utter.au3>
- En cambio si está en una carpeta llamada Include que está en la carpeta donde esté la aplicación, es decir en...
C:\Mis ejemplos\AutoIt3\Include pondríamos #include <./Include/Utter.au3>
- Las palabras elegidas también las podría haber puesto así:
$palabras = "Paint|Bloc de notas|Calculadora|Word|Símbolo de sistema|Reproductor de Windows|"
$palabras = $palabras & "Cerrar Bloc de notas|Cerrar Paint|Cerrar Calculadora"
_Utter_Speech_CreateGrammar($reconoce, $palabras)
Ejecutar aplicación por voz |
#include "./Include/Utter.au3"
; #include <Utter.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <SendMessage.au3>
$Form1 = GUICreate("Abrir aplicaciones - Juan A. Villalpando", 400, 400, 200, 200)
GUISetBkColor(0xffffff, $Form1)
; Juan A. Villalpando. KIO4.COM. juana1991@yahoo.com
GUISetState()
$reconoce = _Utter_Speech_StartEngine()
_Utter_Speech_CreateGrammar($reconoce,"Paint|Bloc de notas|Calculadora|Word|Símbolo de sistema|Reproductor de Windows|Cerrar Bloc de notas|Cerrar Paint|Cerrar Calculadora")
_Utter_Speech_CreateTokens($reconoce)
_Utter_Speech_GrammarRecognize($reconoce,"",0,"_spchin")
While 1
$msg = GUIGetMsg()
Switch $msg
Case $GUI_EVENT_CLOSE
ExitLoop
EndSwitch
WEnd
_Utter_Speech_ShutdownEngine()
Func _spchin($color)
ConsoleWrite($color)
Switch $color
Case "Paint"
ShellExecute("mspaint.exe","","C:\Windows\System32\")
Case "Bloc de notas"
ShellExecute("notepad.exe","","C:\Windows\System32\")
Case "Calculadora"
ShellExecute("calc.exe","","C:\Windows\System32\")
Case "Word"
ShellExecute("winword.exe","","C:\Program Files (x86)\Microsoft Office\Office12\")
Case "Símbolo de sistema"
ShellExecute("cmd.exe","","C:\Windows\System32\")
Case "Reproductor de Windows"
ShellExecute("wmplayer.exe","","C:\Program Files\Windows Media Player\")
Case "Cerrar Bloc de notas"
WinClose("[CLASS:Notepad]")
Case "Cerrar Paint"
WinClose("[CLASS:MSPaintApp]")
Case "Cerrar Calculadora"
WinClose("[CLASS:CalcFrame]")
EndSwitch
EndFunc
|
- Mediante la utilidad AU3Info, arrastando el icono de Finder Tool a la barra superior de una aplicación podemos ver como se llama su Class, en este caso CalcFrame.
_______________________________________________
4.- Ejemplo de reconocimiento de dictado.
- Hablamos durante 10 segundos o el tiempo que establezcamos, observa el 10000 en el código.
Example 3 (Free Word Recognition) |
#include "./Include/Utter.au3"
_Utter_Speech_CreateFreeGrammar(10000,"_spchin") ; Create free grammar and load words from system dictionary lasting for 3 seconds
Func _spchin($dummy)
ConsoleWrite($dummy)
Switch $dummy
Case "exit" ;exit the script when the user says exit
Exit
EndSwitch
EndFunc
|
_______________________________________________
5.- Otro código con otra API.
- Hablamos delante del micrófono.
- https://www.autoitscript.com/forum/topic/115203-basic-speech-recognition/
|
Global $h_Context = ObjCreate("SAPI.SpInProcRecoContext")
Global $h_Recognizer = $h_Context.Recognizer
Global $h_Grammar = $h_Context.CreateGrammar(1)
$h_Grammar.Dictationload
$h_Grammar.DictationSetState(1)
Global $voice = ObjCreate("SAPI.SpVoice")
global $text = 0
Global $times = 0
Global $times2 = 1
Global $PF = @ProgramFilesDir
;Create a token for the default audio input device and set it
Global $h_Category = ObjCreate("SAPI.SpObjectTokenCategory")
$h_Category.SetId("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\AudioInput\TokenEnums\MMAudioIn\")
Global $h_Token = ObjCreate("SAPI.SpObjectToken")
$h_Token.SetId("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\AudioInput\TokenEnums\MMAudioIn\")
$h_Recognizer.AudioInput = $h_Token
Global $i_ObjInitialized = 0
Global $h_ObjectEvents = ObjEvent($h_Context, "SpRecEvent_")
If @error Then
ConsoleWrite("ObjEvent error: " & @error & @CRLF)
$i_ObjInitialized = 0
Else
ConsoleWrite("ObjEvent created Successfully!" & @CRLF)
$i_ObjInitialized = 1
EndIf
While $i_ObjInitialized
Sleep(5000) ;Allow the Audio In to finalize processing on the last 5 second capture
$h_Context.Pause ;Resume audio in processing
$h_Context.Resume ;Reset event function allocation (what is this? I think its garbage collection or something, needs clarification)
$h_ObjectEvents = ObjEvent($h_Context, "SpRecEvent_")
WEnd
Func SpRecEvent_Hypothesis($StreamNumber, $StreamPosition, $Result)
ConsoleWrite("Hypothesis(): Hypothized text is: " & $Result.PhraseInfo.GetText & @CRLF)
EndFunc ;==>SpRecEvent_Hypothesis
Func SpRecEvent_Recognition($StreamNumber, $StreamPosition, $RecognitionType, $Result)
ConsoleWrite($RecognitionType & "||" & $Result.PhraseInfo.GetText & @CRLF)
if $Result.PhraseInfo.GetText = "jarvis" and $times = not 1 Then
call("jarvis")
EndIf
if $times = 1 and $times2 = 0 Then
$q1 = $Result.PhraseInfo.GetText;InputBox("question for jarvis", "")
call("Aknowledge",$q1)
EndIf
EndFunc ;==>SpRecEvent_Recognition
Func SpRecEvent_SoundStart($StreamNumber, $StreamPosition)
ConsoleWrite("Sound Started" & @CRLF)
EndFunc ;==>SpRecEvent_SoundStart
Func SpRecEvent_SoundEnd($StreamNumber, $StreamPosition)
ConsoleWrite("Sound Ended" & @CRLF)
EndFunc ;==>SpRecEvent_SoundEnd
func jarvis()
$string = "Sir?"
$Voice.Speak($string,11)
Sleep(500)
$times = 1
$times2 = 0
;$q1 = $Result.PhraseInfo.GetText;InputBox("question for jarvis", "")
;call("Aknowledge",$q1)
EndFunc
func aknowledge($q1)
Select
case $q1 = "Open mail"
Call("open_mail")
case $q1 = "Close mail"
call("close_mail")
case $q1 = "get time"
$text = ", the time is " & @hour & " hours " & @min & " minutes"
$times2 = 1
case $q1 = "get date"
call("get_date")
case $q1 = "goodbye"
call("Good_bye")
case $q1 = "go to sleep"
$text = "going to sleep"
case $q1 = "start team speak"
call("open_teamspeak")
case $q1 = "close team speak"
call("close_teamspeak")
case $q1 = "lights on all"
Call("Lights_on_all")
case $q1 = "lights off all"
call("Lights_off_all")
case $q1 = ""
$text = "sorry you haven't specified what you wanted from me"
EndSelect
if $text = not 0 then
$string = "of course Sir " & $text
$Voice.Speak($string,11)
Sleep(2000)
$times = 0
endif
;select
;case $q1 = "good bye"
;exit
;case $q1 = "go to sleep"
;WinSetState("J.A.R.V.I.S 1.0 Beta","" ,@SW_MINIMIZE)
;EndSelect
$q1 = 0
$text = 0
EndFunc
func good_bye()
$t1 = @HOUR
select
case $t1 >= 12 and $t1 < 18
$text = "have a nice afternoon"
case $t1 < 12 and $t1 > 5
$text = "have a nice day"
case $t1 > 18 and $t1 < 23
$text = "have a pleasant night"
EndSelect
$times2 = 1
;MsgBox(0, "test", $text) ; encoutered problems with $text variable
; the variable doesn't get returned to the speech part
;made variable $text a global and this resolved the problem
endfunc
func open_mail()
$text = "opening your'e mail now"
run("C:\Program Files (x86)\Microsoft Office\Office14\OUTLOOK.exe")
winwaitactive("Inbox")
$times2 = 1
EndFunc
func close_mail()
$p1 =ProcessExists("OUTLOOK.EXE")
if $p1 = 0 Then
$text = ", you have no mail programm running"
else
$text = "closing your'e mail"
ProcessClose("OUTLOOK.EXE")
EndIf
$times2 = 1
EndFunc
func open_teamspeak()
$text = "starting Team Speak"
;run($PF &"\FalNET G19 Display Manager\FalNET G19 Display Manager.exe")
WinWaitActive("FalNET G19 Display Manager", "")
WinSetState("FalNET G19 Display Manager", "",@SW_MINIMIZE)
run($PF &"\TeamSpeak 3 Client\ts3client_win64.exe")
WinWaitActive("TeamSpeak 3", "")
Send("^s")
WinWaitActive("Connect", "")
send("{enter}")
$times2 = 1
EndFunc
func close_teamspeak()
$text = "closing Team Speak"
$p1 = ProcessExists("ts3client_win64.exe")
$p2 = ProcessExists("FalNET G19 Display Manager.exe")
if $p1 = 0 Then
$text = "no Team Speak running"
Else
ProcessClose("ts3client_win64.exe")
;WinSetState("TeamSpeak3", " ",@sw_show)
EndIf
if $p2 = 0 then
Else
ProcessClose("FalNET G19 Display Manager.exe")
EndIf
$times2 = 1
EndFunc
func get_date()
$year = @YEAR
$month1 =@MON
$day1 = @MDAY
$day2 = @WDAY
; converting day number to the name of that day
Select
Case $day2 = 1
$day3 = "Sunday"
Case $day2 = 2
$day3 = "monday"
case $day2 = 3
$day3 = "Tuesday"
case $day2 = 4
$day3 = "wensday"
Case $day2 = 5
$day3 = "Thursday"
Case $day2 = 6
$day3 = "Friday"
case $day2 = 7
$day3 = "Saturday"
EndSelect
;converting number of month to name of that month
Select
Case $month1 = 01
$month2 = "January"
Case $month1 = 02
$month2 = "February"
Case $month1 = 03
$month2 = "March"
Case $month1 = 04
$month2 = "April"
Case $month1 = 05
$month2 = "May"
Case $month1 = 06
$month2 = "June"
Case $month1 = 07
$month2 = "July"
Case $month1 = 08
$month2 = "August"
Case $month1 = 09
$month2 = "September"
Case $month1 = 10
$month2 = "October"
Case $month1 = 11
$month2 = "November"
Case $month1 = 12
$month2 = "December"
EndSelect
$text = "The date is: " & $day3 & " "& $day1 & " of " & $month2 & " " & $year
$times2 = 1
EndFunc
; the Lights functions will grow when i figured out how to
; combine this programm with my lighting scheme
func Lights_on_all()
$text = " Turning all lights on"
EndFunc
func Lights_off_all()
$text = " turning all lights off"
EndFunc
|
_______________________________________________
6.- Otro código con otra API.
https://www.autoitscript.com/forum/topic/114742-sapilistbox-speech-recognition-udf/
_________________
|