diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2014-07-29 19:44:10 -0700 |
|---|---|---|
| committer | schneefux <schneefux+commit@schneefux.xyz> | 2014-07-29 19:44:10 -0700 |
| commit | 577d630ad2110fbde73008c2ba96f38319024334 (patch) | |
| tree | 8d231328bc461572e31572de3cf0a954199e9611 /client | |
| parent | 526f86a138a2145c4342e76e2d7dd795f60013a7 (diff) | |
| download | jasper-client-577d630ad2110fbde73008c2ba96f38319024334.tar.gz jasper-client-577d630ad2110fbde73008c2ba96f38319024334.zip | |
Addressed CR comments.
Diffstat (limited to 'client')
| -rw-r--r-- | client/main.py | 14 | ||||
| -rw-r--r-- | client/mic.py | 7 | ||||
| -rw-r--r-- | client/populate.py | 3 | ||||
| -rw-r--r-- | client/stt.py | 70 | ||||
| -rw-r--r-- | client/test.py | 3 |
5 files changed, 45 insertions, 52 deletions
diff --git a/client/main.py b/client/main.py index ccafdb9..3fc728f 100644 --- a/client/main.py +++ b/client/main.py @@ -2,7 +2,6 @@ import yaml import sys import speaker import stt -from stt import PocketSphinxSTT from conversation import Conversation @@ -24,12 +23,17 @@ if __name__ == "__main__": profile = yaml.safe_load(open("profile.yml", "r")) try: - google_api_key = profile['google_api_key'] + api_key = profile['keys']['GOOGLE_SPEECH'] except KeyError: - print "Google STT API Key not present in profile - defaulting to PocketSphinx..." - google_api_key = None + api_key = None - mic = Mic(speaker.newSpeaker(), PocketSphinxSTT(), stt.newSTTEngine(google_api_key)) + try: + stt_engine_type = profile['stt_engine'] + except KeyError: + print "stt_engine not specified in profile, defaulting to PocketSphinx" + stt_engine_type = "sphinx" + + mic = Mic(speaker.newSpeaker(), stt.PocketSphinxSTT(), stt.newSTTEngine(stt_engine_type, api_key=api_key)) addendum = "" if 'first_name' in profile: diff --git a/client/mic.py b/client/mic.py index 8d66053..b4ef668 100644 --- a/client/mic.py +++ b/client/mic.py @@ -180,7 +180,7 @@ class Mic: """ AUDIO_FILE = "active.wav" - RATE = 44100 + RATE = 16000 CHUNK = 1024 LISTEN_TIME = 12 @@ -241,10 +241,7 @@ class Mic: # DO SOME AMPLIFICATION # os.system("sox "+AUDIO_FILE+" temp.wav vol 20dB") - if MUSIC: - return self.active_stt_engine.transcribe(AUDIO_FILE, MUSIC=True) - - return self.active_stt_engine.transcribe(AUDIO_FILE) + return self.active_stt_engine.transcribe(AUDIO_FILE, MUSIC) def say(self, phrase, OPTIONS=" -vdefault+m3 -p 40 -s 160 --stdout > say.wav"): # alter phrase before speaking diff --git a/client/populate.py b/client/populate.py index 5e9fa4a..48b8670 100644 --- a/client/populate.py +++ b/client/populate.py @@ -84,9 +84,6 @@ def run(): response = raw_input("Please choose email (E) or text message (T): ") profile['prefers_email'] = (response == 'E') - print ("\nIf you wish to depend on the Google Speech To Text API, please enter your API key, or leave blank to use Jasper's default speech to text implementation.") - simple_request('google_api_key', 'API Key') - # write to profile print("Writing to profile...") outputFile = open("profile.yml", "w") diff --git a/client/stt.py b/client/stt.py index 3b3b81d..d261464 100644 --- a/client/stt.py +++ b/client/stt.py @@ -10,7 +10,7 @@ class PocketSphinxSTT(object): def __init__(self, lmd = "languagemodel.lm", dictd = "dictionary.dic", lmd_persona = "languagemodel_persona.lm", dictd_persona = "dictionary_persona.dic", - lmd_music=None, dictd_music=None): + lmd_music=None, dictd_music=None, **kwargs): """ Initiates the pocketsphinx instance. @@ -75,49 +75,50 @@ To obtain an API key: 2. Create a project through the Google Developers console: https://console.developers.google.com/project 3. Select your project. In the sidebar, navigate to "APIs & Auth." Activate the Speech API. 4. Under "APIs & Auth," navigate to "Credentials." Create a new key for public API access. -5. Copy your API key and run client/populate.py. When prompted, paste this key for access to the Speech API. +5. Add your credentials to your profile.yml. Add an entry to the 'keys' section using the key name 'GOOGLE_SPEECH.' Sample configuration: +6. Set the value of the 'stt_engine' key in your profile.yml to 'google' + + +Excerpt from sample profile.yml: + + ... + timezone: US/Pacific + stt_engine: google + keys: + GOOGLE_SPEECH: $YOUR_KEY_HERE -This implementation also requires that the avconv audio utility be present on your $PATH. On RPi, simply run: - sudo apt-get install avconv """ class GoogleSTT(object): - RATE = 44100 + RATE = 16000 - def __init__(self, api_key): + def __init__(self, api_key, **kwargs): """ Arguments: api_key - the public api key which allows access to Google APIs """ - self.api_key = api_key - for tool in ("avconv", "ffmpeg"): - if os.system("which %s" % tool) == 0: - self.audio_tool = tool - break - if not self.audio_tool: - raise Exception("Could not find an audio tool to convert .wav files to .flac") - def transcribe(self, audio_file_path): + def transcribe(self, audio_file_path, PERSONA_ONLY=False, MUSIC=False): """ Performs STT via the Google Speech API, transcribing an audio file and returning an English string. - audio_file_path -- the path to the audio file to-be transcribed + Arguments: + audio_file_path -- the path to the .wav file to be transcribed """ - AUDIO_FILE_FLAC = "active.flac" - os.system("%s -y -i %s -f flac -b:a 44100 %s" % (self.audio_tool, audio_file_path, AUDIO_FILE_FLAC)) - url = "https://www.google.com/speech-api/v2/recognize?output=json&client=chromium&key=%s&lang=%s&maxresults=6&pfilter=2" % (self.api_key, "en-us") - flac = open(AUDIO_FILE_FLAC, 'rb') - data = flac.read() - flac.close() + + wav = open(audio_file_path, 'rb') + data = wav.read() + wav.close() + try: req = urllib2.Request( url, data=data, headers={ - 'Content-type': 'audio/x-flac; rate=%s' % GoogleSTT.RATE}) + 'Content-type': 'audio/l16; rate=%s' % GoogleSTT.RATE}) response_url = urllib2.urlopen(req) response_read = response_url.read() response_read = response_read.decode('utf-8') @@ -135,21 +136,18 @@ class GoogleSTT(object): """ Returns a Speech-To-Text engine. -If api_key is not supplied, Jasper will rely on the PocketSphinx STT engine for -audio transcription. - -If api_key is supplied, Jasper will use the Google Speech API for transcribing -audio while in the active listen phase. Jasper will continue to rely on the -PocketSphinx engine during the passive listen phase, as the Google Speech API -is rate limited to 50 requests/day. +Currently, the supported implementations are the default Pocket Sphinx and +the Google Speech API Arguments: -api_key - if supplied, Jasper will use the Google Speech API for transcribing -audio in the active listen phase. - + engine_type - one of "sphinx" or "google" + kwargs - keyword arguments passed to the constructor of the STT engine """ -def newSTTEngine(api_key = None): - if api_key: - return GoogleSTT(api_key) +def newSTTEngine(engine_type, **kwargs): + t = engine_type.lower() + if t == "sphinx": + return PocketSphinxSTT(**kwargs) + elif t == "google": + return GoogleSTT(**kwargs) else: - return PocketSphinxSTT() + raise ValueError("Unsupported STT engine type: " + engine_type) diff --git a/client/test.py b/client/test.py index 69567bb..6186642 100644 --- a/client/test.py +++ b/client/test.py @@ -28,9 +28,7 @@ class TestMic(unittest.TestCase): self.jasper_clip = "../static/audio/jasper.wav" self.time_clip = "../static/audio/time.wav" - from mic import Mic from stt import PocketSphinxSTT - self.stt = PocketSphinxSTT() def testTranscribeJasper(self): @@ -41,7 +39,6 @@ class TestMic(unittest.TestCase): def testTranscribe(self): """Does Jasper recognize 'time' (i.e., active listen)?""" transcription = self.stt.transcribe(self.time_clip) - print transcription self.assertTrue("TIME" in transcription) |
