実現したいこと
参考サイト通りに設定しているのですが、エラーが出ます。
公式のコピペなので何が原因かわかりません。
すみません、どなたかご教示いただけないでしょうか。!
エラー内容
client = speech.SpeechClient() AttributeError: module 'google.cloud.speech' has no attribute 'SpeechClient'
ソース
python
1from __future__ import division 2import os 3import re 4import sys 5from google.cloud import speech 6import pyaudio 7from six.moves import queue 8 9os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'new.json' 10 11# Audio recording parameters 12RATE = 16000 13CHUNK = int(RATE / 10) # 100ms 14 15class MicrophoneStream(object): 16 """Opens a recording stream as a generator yielding the audio chunks.""" 17 18 def __init__(self, rate, chunk): 19 self._rate = rate 20 self._chunk = chunk 21 22 # Create a thread-safe buffer of audio data 23 self._buff = queue.Queue() 24 self.closed = True 25 26 def __enter__(self): 27 self._audio_interface = pyaudio.PyAudio() 28 self._audio_stream = self._audio_interface.open( 29 format=pyaudio.paInt16, 30 # The API currently only supports 1-channel (mono) audio 31 # https://goo.gl/z757pE 32 channels=1, 33 rate=self._rate, 34 input=True, 35 frames_per_buffer=self._chunk, 36 # Run the audio stream asynchronously to fill the buffer object. 37 # This is necessary so that the input device's buffer doesn't 38 # overflow while the calling thread makes network requests, etc. 39 stream_callback=self._fill_buffer, 40 ) 41 42 self.closed = False 43 44 return self 45 46 def __exit__(self, type, value, traceback): 47 self._audio_stream.stop_stream() 48 self._audio_stream.close() 49 self.closed = True 50 # Signal the generator to terminate so that the client's 51 # streaming_recognize method will not block the process termination. 52 self._buff.put(None) 53 self._audio_interface.terminate() 54 55 def _fill_buffer(self, in_data, frame_count, time_info, status_flags): 56 """Continuously collect data from the audio stream, into the buffer.""" 57 self._buff.put(in_data) 58 return None, pyaudio.paContinue 59 60 def generator(self): 61 while not self.closed: 62 # Use a blocking get() to ensure there's at least one chunk of 63 # data, and stop iteration if the chunk is None, indicating the 64 # end of the audio stream. 65 chunk = self._buff.get() 66 if chunk is None: 67 return 68 data = [chunk] 69 70 # Now consume whatever other data's still buffered. 71 while True: 72 try: 73 chunk = self._buff.get(block=False) 74 if chunk is None: 75 return 76 data.append(chunk) 77 except queue.Empty: 78 break 79 80 yield b"".join(data) 81 82def listen_print_loop(responses): 83 """Iterates through server responses and prints them. 84 85 The responses passed is a generator that will block until a response 86 is provided by the server. 87 88 Each response may contain multiple results, and each result may contain 89 multiple alternatives; for details, see https://goo.gl/tjCPAU. Here we 90 print only the transcription for the top alternative of the top result. 91 92 In this case, responses are provided for interim results as well. If the 93 response is an interim one, print a line feed at the end of it, to allow 94 the next result to overwrite it, until the response is a final one. For the 95 final one, print a newline to preserve the finalized transcription. 96 """ 97 num_chars_printed = 0 98 for response in responses: 99 if not response.results: 100 continue 101 102 # The `results` list is consecutive. For streaming, we only care about 103 # the first result being considered, since once it's `is_final`, it 104 # moves on to considering the next utterance. 105 result = response.results[0] 106 if not result.alternatives: 107 continue 108 109 # Display the transcription of the top alternative. 110 transcript = result.alternatives[0].transcript 111 112 # Display interim results, but with a carriage return at the end of the 113 # line, so subsequent lines will overwrite them. 114 # 115 # If the previous result was longer than this one, we need to print 116 # some extra spaces to overwrite the previous result 117 overwrite_chars = " " * (num_chars_printed - len(transcript)) 118 119 if not result.is_final: 120 sys.stdout.write(transcript + overwrite_chars + "\r") 121 sys.stdout.flush() 122 123 num_chars_printed = len(transcript) 124 125 else: 126 print(transcript + overwrite_chars) 127 128 # Exit recognition if any of the transcribed phrases could be 129 # one of our keywords. 130 if re.search(r"\b(exit|quit)\b", transcript, re.I): 131 print("Exiting..") 132 break 133 134 num_chars_printed = 0 135 136def main(): 137 # See http://g.co/cloud/speech/docs/languages 138 # for a list of supported languages. 139 language_code = "ja-JP" # a BCP-47 language tag 140 141 client = speech.SpeechClient() 142 config = speech.RecognitionConfig( 143 encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16, 144 sample_rate_hertz=RATE, 145 language_code=language_code, 146 ) 147 148 streaming_config = speech.StreamingRecognitionConfig( 149 config=config, interim_results=True 150 ) 151 152 with MicrophoneStream(RATE, CHUNK) as stream: 153 audio_generator = stream.generator() 154 requests = ( 155 speech.StreamingRecognizeRequest(audio_content=content) 156 for content in audio_generator 157 ) 158 159 responses = client.streaming_recognize(streaming_config, requests) 160 161 # Now, put the transcription responses to use. 162 listen_print_loop(responses) 163 164if __name__ == "__main__": 165 main()
環境
Windows10
python3.9
回答1件