aiyprojects-raspbian icon indicating copy to clipboard operation
aiyprojects-raspbian copied to clipboard

cloudspeech_demo.py fails: AttributeError: module 'google.cloud.speech' has no attribute 'types'

Open bbrendon opened this issue 4 years ago • 9 comments

I tried doing a git pull to get possibly updated but that didn't change anything. Any ideas?

pi@raspberrypi:~/AIY-projects-python/src/examples/voice $ ./cloudspeech_demo.py
Traceback (most recent call last):
  File "./cloudspeech_demo.py", line 22, in <module>
    from aiy.cloudspeech import CloudSpeechClient
  File "/home/pi/AIY-projects-python/src/aiy/cloudspeech.py", line 40, in <module>
    END_OF_SINGLE_UTTERANCE = speech.types.StreamingRecognizeResponse.END_OF_SINGLE_UTTERANCE
AttributeError: module 'google.cloud.speech' has no attribute 'types'
pi@raspberrypi:~/AIY-projects-python/src/examples/voice $

bbrendon avatar Nov 26 '20 04:11 bbrendon

Solved it with the following three changes:

from google.cloud import speech_v1 as speech
...
END_OF_SINGLE_UTTERANCE = speech.types.StreamingRecognizeResponse.SpeechEventType.END_OF_SINGLE_UTTERANCE
...
encoding=speech.types.RecognitionConfig.AudioEncoding.LINEAR16,
...

rabbitmcrabbit avatar Dec 05 '20 21:12 rabbitmcrabbit

@rabbitmcrabbit what file did you modify? Do you have a diff?

bbrendon avatar Dec 06 '20 00:12 bbrendon

This was an edit to ~/AIY-projects-python/src/aiy/cloudspeech.py. My apologies, I'm writing from a machine where it's not straightforward to attach a diff, but the start of each line remains unchanged, so you should be able to find the corresponding places.

rabbitmcrabbit avatar Dec 06 '20 10:12 rabbitmcrabbit

Thanks. I got past this. I would have never figured that out.

diff --git a/src/aiy/cloudspeech.py b/src/aiy/cloudspeech.py
index d8bcd57..ddd8225 100644
--- a/src/aiy/cloudspeech.py
+++ b/src/aiy/cloudspeech.py
@@ -32,12 +32,15 @@ import os
 import logging

 os.environ['GRPC_POLL_STRATEGY'] = 'epoll1'
-from google.cloud import speech
+#from google.cloud import speech
+from google.cloud import speech_v1 as speech
 from google.oauth2 import service_account

 from aiy.voice.audio import AudioFormat, Recorder

-END_OF_SINGLE_UTTERANCE = speech.types.StreamingRecognizeResponse.END_OF_SINGLE_UTTERANCE
+#END_OF_SINGLE_UTTERANCE = speech.types.StreamingRecognizeResponse.END_OF_SINGLE_UTTERANCE
+END_OF_SINGLE_UTTERANCE = speech.types.StreamingRecognizeResponse.SpeechEventType.END_OF_SINGLE_UTTERANCE
+
 AUDIO_SAMPLE_RATE_HZ = 16000
 AUDIO_FORMAT=AudioFormat(sample_rate_hz=AUDIO_SAMPLE_RATE_HZ,
                          num_channels=1,
@@ -64,7 +67,8 @@ class CloudSpeechClient:

     def _make_config(self, language_code, hint_phrases):
         return speech.types.RecognitionConfig(
-            encoding=speech.types.RecognitionConfig.LINEAR16,
+            #encoding=speech.types.RecognitionConfig.LINEAR16,
+            encoding=speech.types.RecognitionConfig.AudioEncoding.LINEAR16,
             sample_rate_hertz=AUDIO_SAMPLE_RATE_HZ,
             language_code=language_code,
             speech_contexts=[speech.types.SpeechContext(phrases=hint_phrases)])

bbrendon avatar Dec 21 '20 04:12 bbrendon

It works, thank you.

josecarlosphp avatar Feb 15 '21 02:02 josecarlosphp

Thank you! That was super useful.

nine86 avatar Feb 18 '21 16:02 nine86

Thank you so much! I was banging my head against that for hours with a fresh install of the 11/20/2020 AIY SD image (hardware is an RPi 3B and I believe a v1 AIY Voice Hat). Worked perfectly.

AlpenglowInd avatar Mar 03 '21 05:03 AlpenglowInd

Solved for me as well. Is there a PR for this? If not, I'd be happy to make one - this is a rather important issue, since it breaks Cloud Speech functionality.

LRitzdorf avatar May 29 '21 17:05 LRitzdorf

I had the same problem with a fresh recent VoiceKit. This solves the problem. Thank you!

petermeester avatar Sep 10 '21 22:09 petermeester