summaryrefslogtreecommitdiff
path: root/client/stt.py
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2014-07-26 23:31:25 +0000
committerschneefux <schneefux+commit@schneefux.xyz>2014-07-26 23:31:25 +0000
commit78389f252a021d642a7a0146b658f0465da6e264 (patch)
treeb740c6339ceba381004d3441f2459f65d38d02a6 /client/stt.py
parent2c55f4da462383ee6c7b89ea44e5c40cd5badf0c (diff)
downloadjasper-client-78389f252a021d642a7a0146b658f0465da6e264.tar.gz
jasper-client-78389f252a021d642a7a0146b658f0465da6e264.zip
Work on Google STT on RPi.
Diffstat (limited to 'client/stt.py')
-rw-r--r--client/stt.py58
1 files changed, 54 insertions, 4 deletions
diff --git a/client/stt.py b/client/stt.py
index a62cf38..5da0889 100644
--- a/client/stt.py
+++ b/client/stt.py
@@ -3,9 +3,14 @@ import traceback
import json
import urllib2
+"""
+The default Speech-To-Text implementation which relies on PocketSphinx.
+"""
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):
+ 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):
"""
Initiates the pocketsphinx instance.
@@ -60,18 +65,48 @@ class PocketSphinxSTT(object):
return result[0]
+"""
+Speech-To-Text implementation which relies on the Google Speech API.
+This implementation requires a Google API key to be present in profile.yml
+
+To obtain an API key:
+1. Join the Chromium Dev group: https://groups.google.com/a/chromium.org/forum/?fromgroups#!forum/chromium-dev
+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.
+
+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
def __init__(self, api_key):
+ """
+ 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):
+ def transcribe(self, audio_file_path):
+ """
+ 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
+ """
AUDIO_FILE_FLAC = "active.flac"
- os.system("ffmpeg -y -i %s -c:a flac -ab 44100 %s" % (audio_file, AUDIO_FILE_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')
@@ -89,11 +124,26 @@ class GoogleSTT(object):
decoded = json.loads(response_read.split("\n")[1])
print response_read
text = decoded['result'][0]['alternative'][0]['transcript']
- print text
return text
except Exception:
traceback.print_exc()
+"""
+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.
+
+Arguments:
+api_key - if supplied, Jasper will use the Google Speech API for transcribing
+audio in the active listen phase.
+
+"""
def newSTTEngine(api_key = None):
if api_key:
return GoogleSTT(api_key)