Skip to content

Instantly share code, notes, and snippets.

@J0hnL0cke
Last active June 8, 2020 18:25
Show Gist options
  • Save J0hnL0cke/44d101c1b6e46b9e75718c8b1f67406d to your computer and use it in GitHub Desktop.
Save J0hnL0cke/44d101c1b6e46b9e75718c8b1f67406d to your computer and use it in GitHub Desktop.
AIY speech-to-text program
#!/usr/bin/env python3
#This is a program for the Google Voice Kit. It uses the Speech Recognition library to send audio snippets to Houndify for interpretation,
#and it uses pyttsx3 to respond to the user
#This program uses code snippets from this example file: https://github.com/Uberi/speech_recognition/blob/master/examples/audio_transcribe.py
#As well as from Google's voice_recorder file: https://github.com/google/aiyprojects-raspbian/blob/aiyprojects/src/examples/voice/voice_er.py
print("Importing libraries...")
import argparse
import time
import threading
import pyttsx3
from os import path
from aiy.board import Board
from aiy.voice.audio import AudioFormat, play_wav, record_file, Recorder
import speech_recognition as sr
class record:
was_set_up=False
callback=None
@classmethod
def setup(cls,fname):
if not cls.was_set_up:
parser = argparse.ArgumentParser()
parser.add_argument('--filename', '-f', default=fname)
cls.args = parser.parse_args()
btn.set_event()
cls.was_set_up=True
@classmethod
def new(cls,fname='recording.wav'):
cls.setup(fname)
print("Recording started, press button to stop recording")
record_file(AudioFormat.CD, filename=cls.args.filename, wait=cls.interval, filetype='wav')
print("Recording stopped")
return fname
@classmethod
def interval(cls):
if cls.callback==None:
cls.default_cb()
else:
cls.callback()
@classmethod
def default_cb(cls):
start = time.monotonic()
finished=False
while not finished:
if btn.was_pressed():
finished=True
else:
duration = time.monotonic() - start
print('Recording: %.02f seconds [Press button to stop]' % duration)
time.sleep(0.5)
class file:
@classmethod
def get_file(cls,fname):
file=open(fname, "r")
res=file.readlines()
file.close()
return res
class btn:
board=Board()
event_was_set=False
@classmethod
def wait(cls):
cls.board.button.wait_for_press()
@classmethod
def set_event(cls):
cls.done = threading.Event()
cls.board.button.when_pressed=cls.done.set
cls.event_was_set=True
print("Set up button handler")
@classmethod
def was_pressed(cls):
assert cls.event_was_set
return cls.done.is_set()
class tts:
was_set_up=False
@classmethod
def setup(cls):
cls.engine = pyttsx3.init()
cls.was_set_up=True
@classmethod
def say(cls,text):
if not cls.was_set_up:
cls.setup()
print(text)
cls.engine.say(text)
cls.engine.runAndWait()
class recognize:
#AIY voice recognition
has_ids=False
HOUNDIFY_CLIENT_ID = "" # Houndify client IDs are Base64-encoded strings
HOUNDIFY_CLIENT_KEY = "" # Houndify client keys are Base64-encoded
@classmethod
def main(cls):
while True:
print("Waiting for button press to start recording audio")
btn.wait()
file_name=record.new()
print("Interpreting speech...")
audio=cls.parse_audio(file_name)
text=cls.get_text(audio)
assert type(text)==str
tts.say("You said "+text)
@classmethod
def parse_audio(cls,fname):
# obtain path to "english.wav" in the same folder as this script
AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), fname)
# use the audio file as the audio source
cls.r = sr.Recognizer()
with sr.AudioFile(AUDIO_FILE) as source:
audio = cls.r.record(source) # read the entire audio file
return audio
@classmethod
def get_text(cls,audio):
# recognize speech using Houndify
if not cls.has_ids:
cls.get_houndify_credentials()
try:
resp=cls.r.recognize_houndify(audio, client_id=cls.HOUNDIFY_CLIENT_ID, client_key=cls.HOUNDIFY_CLIENT_KEY)
print("Result: client said", resp)
except sr.UnknownValueError:
resp="Houndify could not understand audio"
print(resp)
except sr.RequestError as e:
resp="Could not request results from Houndify service; {0}".format(e)
print(resp)
return resp
@classmethod
def get_houndify_credentials(cls):
file_data=file.get_file('../houndify.txt') #credential location, should be stored as 2 lines: client ID and client key
cls.HOUNDIFY_CLIENT_ID = file_data[0].replace("\n","")
cls.HOUNDIFY_CLIENT_KEY = file_data[1].replace("\n","")
cls.has_ids=True
recognize.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment