質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Google Cloud Platform

Google Cloud Platformは、Google社がクラウド上で提供しているサービス郡の総称です。エンドユーザー向けサービスと同様のインフラストラクチャーで運営されており、Webサイト開発から複雑なアプリ開発まで対応可能です。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

628閲覧

python gcpでリアルタイムストリーミングしたい

gaza115

総合スコア10

Google Cloud Platform

Google Cloud Platformは、Google社がクラウド上で提供しているサービス郡の総称です。エンドユーザー向けサービスと同様のインフラストラクチャーで運営されており、Webサイト開発から複雑なアプリ開発まで対応可能です。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2022/06/17 09:20

実現したいこと

参考サイト通りに設定しているのですが、エラーが出ます。
公式のコピペなので何が原因かわかりません。
すみません、どなたかご教示いただけないでしょうか。!

参考サイト
公式設定

エラー内容

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

gaza115

2022/06/18 13:33

すみません、ありがとうございます。 from google.cloud.speech_v1 import enums from google.cloud.speech_v1 import types 上記に惑わされて from google.cloud import speech_v1 as speech もエラーかと思ってました。
guest

回答1

0

自己解決

投稿2022/06/18 13:34

gaza115

総合スコア10

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問