Skip to content

Commit

Permalink
Merge pull request #601 from alinerguio/master
Browse files Browse the repository at this point in the history
fix recognize_google_cloud
  • Loading branch information
Uberi authored Mar 27, 2022
2 parents d7b26b4 + 9c68e15 commit f149aff
Showing 1 changed file with 5 additions and 7 deletions.
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

0 comments on commit f149aff

Please sign in to comment.