CLI tool to control your IoT gadgets.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
roomcomputer/speech/speech.py

45 lines
984 B

import speech_recognition as sr
4 years ago
import io
from google.cloud import speech as sp
4 years ago
class voiceInput(object):
recognizer = sr.Recognizer()
4 years ago
commandFunc = None
4 years ago
muted = True
4 years ago
def transcribe_voice( self, streamFile ):
cl = sp.SpeechClient()
4 years ago
with io.open( streamFile, "rb" ) as audioFile:
cont = audioFile.read()
4 years ago
stream = [cont]
req = ( sp.StreamingRecognizeRequest(audio_content=chunk) for chunk in stream )
4 years ago
conf = sp.RecognitionConfig(
encoding = sp.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz = 16000,
language_code = "en-US"
)
4 years ago
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 ):
self.muted = setm
def switchMute( self ):
self.setMuted( not self.muted )
4 years ago
4 years ago
vc = voiceInput()
vc.transcribe_voice( "./stream.txt" )