Permalink
Browse files

updated usage of Google Cloud Speech API from v1beta1 to v1

  • Loading branch information...
oort7 committed Oct 24, 2017
1 parent 0202212 commit 3379e4ae6aa7952417b34c3b4ce42e101addb60b
Showing with 5 additions and 5 deletions.
  1. +5 −5 speech_recognition/__init__.py
@@ -912,7 +912,7 @@ def recognize_google_cloud(self, audio_data, credentials_json=None, language="en
assert isinstance(language, str), "``language`` must be a string"
assert preferred_phrases is None or all(isinstance(preferred_phrases, (type(""), type(u""))) for preferred_phrases in preferred_phrases), "``preferred_phrases`` must be a list of strings"
# See https://cloud.google.com/speech/reference/rest/v1beta1/RecognitionConfig
# See https://cloud.google.com/speech/reference/rest/v1/RecognitionConfig
flac_data = audio_data.get_flac_data(
convert_rate=None if 8000 <= audio_data.sample_rate <= 48000 else max(8000, min(audio_data.sample_rate, 48000)), # audio sample rate must be between 8 kHz and 48 kHz inclusive - clamp sample rate into this range
convert_width=2 # audio samples must be 16-bit
@@ -940,15 +940,15 @@ def recognize_google_cloud(self, audio_data, credentials_json=None, language="en
f.flush()
api_credentials = GoogleCredentials.from_stream(f.name)
speech_service = build("speech", "v1beta1", credentials=api_credentials, cache_discovery=False)
speech_service = build("speech", "v1", credentials=api_credentials, cache_discovery=False)
except ImportError:
raise RequestError("missing google-api-python-client module: ensure that google-api-python-client is set up correctly.")
if preferred_phrases is None:
speech_config = {"encoding": "FLAC", "sampleRate": audio_data.sample_rate, "languageCode": language}
speech_config = {"encoding": "FLAC", "sampleRateHertz": audio_data.sample_rate, "languageCode": language}
else:
speech_config = {"encoding": "FLAC", "sampleRate": audio_data.sample_rate, "languageCode": language, "speechContext": {"phrases": preferred_phrases}}
request = speech_service.speech().syncrecognize(body={"audio": {"content": base64.b64encode(flac_data).decode("utf8")}, "config": speech_config})
speech_config = {"encoding": "FLAC", "sampleRateHertz": audio_data.sample_rate, "languageCode": language, "speechContext": {"phrases": preferred_phrases}}
request = speech_service.speech().recognize(body={"audio": {"content": base64.b64encode(flac_data).decode("utf8")}, "config": speech_config})
try:
response = request.execute()

0 comments on commit 3379e4a

Please sign in to comment.