I am trying to use the Google speech to text API from Python 3.5 on a RPI 3B+ running Raspbian. But when I try to run the speech_client.recognize function I get the following error message:
Traceback (most recent call last):
File , line 135, in <module>
response = speech_client.recognize(config, audio)
File "/usr/local/lib/python3.5/dist-packages/google/cloud/speech_v1/gapic/speech_client.py", line 231, in recognize
self.transport.recognize,
AttributeError: 'str' object has no attribute 'recognize'
I use the following code:
from google.cloud import speech
from google.cloud.speech_v1 import enums
from google.cloud.speech_v1 import types
speech_client = speech.SpeechClient()
with io.open(chunk_file, 'rb') as audio_file:
content = audio_file.read()
audio = types.RecognitionAudio(content=content)
with wave.open(chunk_file, 'rb') as wave_file:
frame_rate = wave_file.getframerate()
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=frame_rate,
language_code='nl-NL')
response = speech_client.recognize(config, audio)
The (google packages) output of pip3 freeze:
google-api-core==1.10.0
google-api-python-client==1.7.8
google-auth==1.6.3
google-auth-httplib2==0.0.3
google-auth-oauthlib==0.3.0
google-cloud==0.34.0
google-cloud-bigquery==1.11.2
google-cloud-core==0.29.1
google-cloud-speech==1.0.0
google-cloud-storage==1.15.0
google-cloud-vision==0.36.0
google-resumable-media==0.3.2
googleapis-common-protos==1.5.10
I have absolutely no clue (anymore) on how to solve this. Could this be a bug in one of the packages and should I downgrade, or could it be that my API authentications aren's set correctly?
The installation of the RPI has happened in the past few days, and I've tried upgrading also.
Hope anybody can help. Thanks!
Trying to reproduce in a fresh virtual environment:
$ python3.6 -m venv /tmp/gcp/7911
$ /tmp/gcp/7911/bin/pip install --upgrade setuptools pip
...
Successfully installed pip-19.1.1 setuptools-41.0.1
$ /tmp/gcp/7911/bin/pip install google-cloud-speech
...
Successfully installed cachetools-3.1.0 certifi-2019.3.9 chardet-3.0.4 google-api-core-1.10.0 google-auth-1.6.3 google-cloud-speech-1.0.0 googleapis-common-protos-1.5.10 grpcio-1.20.1 idna-2.8 protobuf-3.7.1 pyasn1-0.4.5 pyasn1-modules-0.2.5 pytz-2019.1 requests-2.21.0 rsa-4.0 six-1.12.0 urllib3-1.24.3
I've modified your script to make it run (adding imports, changing the language code to match my sample):
# Repro https://github.com/googleapis/google-cloud-python/issues/7911
import io
import sys
import wave
from google.cloud import speech
from google.cloud.speech_v1 import enums
from google.cloud.speech_v1 import types
speech_client = speech.SpeechClient()
def process_audio(chunk_file):
with io.open(chunk_file, 'rb') as audio_file:
content = audio_file.read()
audio = types.RecognitionAudio(content=content)
with wave.open(chunk_file, 'rb') as wave_file:
frame_rate = wave_file.getframerate()
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=frame_rate,
language_code='en',
)
response = speech_client.recognize(config, audio)
first = response.results[0].alternatives[0]
print("{}\n{}".format(first.transcript, first.confidence))
if __name__ == '__main__':
for chunk_file in sys.argv[1:]:
process_audio(chunk_file)
Running the script:
$ bin/python repro_gcp_7911.py test_audio.wav
testing testing one two three testing this is just a test
0.9797341823577881
In the traceback you reported above, the error appears to be that the client's transport attribute is a string: is it possible you passed a string value inadvertently when constructing the client? E.g.: client = speech.SpeechClient("something")
Thank you so much @tseaver, that was exactly what I did! My SpeechClient won't initialize without me giving the location of the credentials file explicitly in the function. Otherwise I get the errormessage that the credentials are unknown. But I forgot to state the parametername when passing it to the function. I changed this now to this:
speech_client = speech.SpeechClient(credentials="my_creds_file.json")
I don't get any error messages anymore, but now the code seems to hang on this line:
response = speech_client.recognize(config, audio)
Even when I'm just trying to run the standard example code:
`from google.cloud import speech_v1
from google.cloud.speech_v1 import enums
client = speech_v1.SpeechClient(credentials="my_creds_file.json")
encoding = enums.RecognitionConfig.AudioEncoding.FLAC
sample_rate_hertz = 44100
language_code = 'en-US'
config = {'encoding': encoding, 'sample_rate_hertz': sample_rate_hertz, 'language_code': language_code}
uri = 'gs://bucket_name/file_name.flac'
audio = {'uri': uri}
response = client.recognize(config, audio)`
Any idea what could be wrong here?
Thank you so much for your help, it is very much appreciated!
Update: there seems to be a problem with the credentials. I try to pass them to the SpeechClient function, and it seems to work fine. Until I try to run the recognize function, then nothing happens. I tried to fill in some random stuff instead of my credentials and the same happens without any error message. Any clue on how to pass the credentials to the SpeechClient initialization?
I found the solution!
Added a line to my code to pass the credentials and changed the way I passed the credentials to SpeechClient:
creds = service_account.Credentials.from_service_account_file("./path/credentials.json")
speech_client = speech_v1.SpeechClient(credentials=creds)
@Cmotions-reader I'm glad you worked that out. An alternate spelling would be:
speech_client = speech_v1.from_service_account_file("./path/credentials.json")