Fixed the voice to text

fetchIP
E. Almqvist 4 years ago
parent 1e0350d356
commit 2ffdb41cab
  1. 21
      speech/speech.py

@ -5,25 +5,28 @@ class voiceInput(object):
muted = True muted = True
def voiceToText( self, deviceIndex=30 ): # "Error codes", can be used to check stuff
what = "??"
error = "ERROR"
def start( self, deviceIndex=30 ): # a generator for everything that is said
while( True ): # loop
try: try:
if( not self.muted ): # this thing is not the NSA
with sr.Microphone( deviceIndex ) as src: with sr.Microphone( deviceIndex ) as src:
self.recognizer.adjust_for_ambient_noise( src, 0.2 ) self.recognizer.adjust_for_ambient_noise( src, 0.2 )
print("Listening...") print("Listening...")
audio = self.recognizer.listen( src, phrase_time_limit=5 ) audio = self.recognizer.listen( src, phrase_time_limit=5 )
print("Thinking...") print("Thinking...")
text = self.recognizer.recognize_google(audio) text = self.recognizer.recognize_google(audio)
print(text) yield text
return self.voiceToText(deviceIndex)
except sr.RequestError as err: except sr.RequestError as err:
print("Unable to request results: {0}".format(err)) print("Unable to request results: {0}".format(err))
return self.voiceToText(deviceIndex) yield self.error
except sr.UnknownValueError: except sr.UnknownValueError:
print("????") yield self.what
return self.voiceToText(deviceIndex)
def setMuted( self, setm: bool=True ): def setMuted( self, setm: bool=True ):
@ -32,7 +35,3 @@ class voiceInput(object):
def switchMute( self ): def switchMute( self ):
self.setMuted( not self.muted ) self.setMuted( not self.muted )
voice = voiceInput()
voice.setMuted(False)
print( "out:", voice.voiceToText() )

Loading…
Cancel
Save