Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix recognize_google_cloud #601

Merged
merged 2 commits into from
Mar 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions speech_recognition/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,8 +930,6 @@ def recognize_google_cloud(self, audio_data, credentials_json=None, language="en
try:
import socket
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
from google.api_core.exceptions import GoogleAPICallError
except ImportError:
raise RequestError('missing google-cloud-speech module: ensure that google-cloud-speech is set up correctly.')
Expand All @@ -945,15 +943,15 @@ def recognize_google_cloud(self, audio_data, credentials_json=None, language="en
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
)
audio = types.RecognitionAudio(content=flac_data)
audio = speech.RecognitionAudio(content=flac_data)

config = {
'encoding': enums.RecognitionConfig.AudioEncoding.FLAC,
'encoding': speech.RecognitionConfig.AudioEncoding.FLAC,
'sample_rate_hertz': audio_data.sample_rate,
'language_code': language
}
if preferred_phrases is not None:
config['speechContexts'] = [types.SpeechContext(
config['speechContexts'] = [speech.SpeechContext(
phrases=preferred_phrases
)]
if show_all:
Expand All @@ -963,10 +961,10 @@ def recognize_google_cloud(self, audio_data, credentials_json=None, language="en
if self.operation_timeout and socket.getdefaulttimeout() is None:
opts['timeout'] = self.operation_timeout

config = types.RecognitionConfig(**config)
config = speech.RecognitionConfig(**config)

try:
response = client.recognize(config, audio, **opts)
response = client.recognize(config=config, audio=audio)
except GoogleAPICallError as e:
raise RequestError(e)
except URLError as e:
Expand Down