Google API test

fetchIP
E. Almqvist 4 years ago
parent ec4f6200b9
commit 57af43c90c
  1. 44
      speech/speech.py

@ -1,27 +1,37 @@
import speech_recognition as sr import speech_recognition as sr
import io
from google.cloud import speech as sp
class sr_microphone(object): class voiceInput(object):
recognizer = sr.Recognizer() recognizer = sr.Recognizer()
commandFunc = None
muted = True muted = True
def getInput(self): # use the object as a generator def transcribe_voice( self, streamFile ):
print("Awaiting input") cl = sp.SpeechClient()
if( not self.muted ):
try:
with sr.Microphone() as src:
self.recognizer.adjust_for_ambient_noise( src, duration=0.2 ) # adjust for ambient noise
audio = self.recognizer.listen(src) with io.open( streamFile, "rb" ) as audioFile:
cont = audioFile.read()
# Make audio -> text stream = [cont]
return (self.recognizer.recognize_google( audio )).lower() # use googles recognizer and lower its output req = ( sp.StreamingRecognizeRequest(audio_content=chunk) for chunk in stream )
except sr.RequestError as err: conf = sp.RecognitionConfig(
print("Unable to request results: {0}".format(err)) encoding = sp.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz = 16000,
language_code = "en-US"
)
except sr.UnknownValueError as err:
print("Unknown Error: {0}".format(err)) streamConf = sp.StreamingRecognitionConfig(config=conf)
responses = cl.streaming_recognize( steamConf, req )
for res in responses:
for result in res.results:
for alt in result.alternatives:
print(alt.transcript)
def setMuted( self, setm: bool=True ): def setMuted( self, setm: bool=True ):
self.muted = setm self.muted = setm
@ -30,7 +40,5 @@ class sr_microphone(object):
self.setMuted( not self.muted ) self.setMuted( not self.muted )
# Small test vc = voiceInput()
voice = sr_microphone() vc.transcribe_voice( "./stream.txt" )
voice.setMuted(False)
print( voice.getInput() )

Loading…
Cancel
Save