前提・実現したいこと
初心者です。お世話になります。
python3.7.4
windows 10
google speech to text というapiのstreaming音声文字お越しをしたいです。
正確にはマイクからの入力を文字にしたいです。
公式ドキュメントの
(https://cloud.google.com/speech-to-text/docs/streaming-recognize?hl=ja#speech-streaming-mic-recognize-python)
「音声ストリームでのストリーミング音声認識の実行」の最中です。
こちら↓
(https://qiita.com/hamham/items/3733ac8cd9e3d7b9ccae)
を参考にしながら、C直下に公式ドキュメントをコピペしたtranscribe_streaming_mic.pyを作成し、streamを付けてコマンドラインから実行しました。
どうぞよろしくお願いいたします
発生している問題・エラーメッセージ
C:\>transcribe_streaming_mic.py stream Traceback (most recent call last): File "C:\transcribe_streaming_mic.py", line 6, in <module> from google.cloud import speech ImportError: No module named google.cloud
似た質問の回答のpip(sudoなし)をおこないインストールは成功しました
(https://teratail.com/questions/123983)
「Successfully installed cachetools-3.1.1 certifi-2019.9.11 chardet-3.0.4 google-api-core-1.14.3 google-auth-1.7.0 google-cloud-speech-1.2.0 googleapis-common-protos-1.6.0 grpcio-1.25.0 idna-2.8 protobuf-3.10.0 pyasn1-0.4.7 pyasn1-modules-0.2.7 requests-2.22.0 rsa-4.0 six-1.13.0 urllib3-1.25.6」
コマンド:(sudo) pip install google-cloud-speech
しかしもう一度C:>transcribe_streaming_mic.py stream コマンドを実行してもまったく同じエラーがでます。
該当のソースコード
python
1from __future__ import division 2 3import re 4import sys 5 6from google.cloud import speech 7from google.cloud.speech import enums 8from google.cloud.speech import types 9import pyaudio 10from six.moves import queue 11 12# Audio recording parameters 13RATE = 16000 14CHUNK = int(RATE / 10) # 100ms 15 16class MicrophoneStream(object): 17 """Opens a recording stream as a generator yielding the audio chunks.""" 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, rate=self._rate, 33 input=True, frames_per_buffer=self._chunk, 34 # Run the audio stream asynchronously to fill the buffer object. 35 # This is necessary so that the input device's buffer doesn't 36 # overflow while the calling thread makes network requests, etc. 37 stream_callback=self._fill_buffer, 38 ) 39 40 self.closed = False 41 42 return self 43 44 def __exit__(self, type, value, traceback): 45 self._audio_stream.stop_stream() 46 self._audio_stream.close() 47 self.closed = True 48 # Signal the generator to terminate so that the client's 49 # streaming_recognize method will not block the process termination. 50 self._buff.put(None) 51 self._audio_interface.terminate() 52 53 def _fill_buffer(self, in_data, frame_count, time_info, status_flags): 54 """Continuously collect data from the audio stream, into the buffer.""" 55 self._buff.put(in_data) 56 return None, pyaudio.paContinue 57 58 def generator(self): 59 while not self.closed: 60 # Use a blocking get() to ensure there's at least one chunk of 61 # data, and stop iteration if the chunk is None, indicating the 62 # end of the audio stream. 63 chunk = self._buff.get() 64 if chunk is None: 65 return 66 data = [chunk] 67 68 # Now consume whatever other data's still buffered. 69 while True: 70 try: 71 chunk = self._buff.get(block=False) 72 if chunk is None: 73 return 74 data.append(chunk) 75 except queue.Empty: 76 break 77 78 yield b''.join(data) 79 80def listen_print_loop(responses): 81 """Iterates through server responses and prints them. 82 83 The responses passed is a generator that will block until a response 84 is provided by the server. 85 86 Each response may contain multiple results, and each result may contain 87 multiple alternatives; for details, see https://goo.gl/tjCPAU. Here we 88 print only the transcription for the top alternative of the top result. 89 90 In this case, responses are provided for interim results as well. If the 91 response is an interim one, print a line feed at the end of it, to allow 92 the next result to overwrite it, until the response is a final one. For the 93 final one, print a newline to preserve the finalized transcription. 94 """ 95 num_chars_printed = 0 96 for response in responses: 97 if not response.results: 98 continue 99 100 # The `results` list is consecutive. For streaming, we only care about 101 # the first result being considered, since once it's `is_final`, it 102 # moves on to considering the next utterance. 103 result = response.results[0] 104 if not result.alternatives: 105 continue 106 107 # Display the transcription of the top alternative. 108 transcript = result.alternatives[0].transcript 109 110 # Display interim results, but with a carriage return at the end of the 111 # line, so subsequent lines will overwrite them. 112 # 113 # If the previous result was longer than this one, we need to print 114 # some extra spaces to overwrite the previous result 115 overwrite_chars = ' ' * (num_chars_printed - len(transcript)) 116 117 if not result.is_final: 118 sys.stdout.write(transcript + overwrite_chars + '\r') 119 sys.stdout.flush() 120 121 num_chars_printed = len(transcript) 122 123 else: 124 print(transcript + overwrite_chars) 125 126 # Exit recognition if any of the transcribed phrases could be 127 # one of our keywords. 128 if re.search(r'\b(exit|quit)\b', transcript, re.I): 129 print('Exiting..') 130 break 131 132 num_chars_printed = 0 133 134def main(): 135 # See http://g.co/cloud/speech/docs/languages 136 # for a list of supported languages. 137 language_code = 'en-US' # a BCP-47 language tag 138 139 client = speech.SpeechClient() 140 config = types.RecognitionConfig( 141 encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16, 142 sample_rate_hertz=RATE, 143 language_code=language_code) 144 streaming_config = types.StreamingRecognitionConfig( 145 config=config, 146 interim_results=True) 147 148 with MicrophoneStream(RATE, CHUNK) as stream: 149 audio_generator = stream.generator() 150 requests = (types.StreamingRecognizeRequest(audio_content=content) 151 for content in audio_generator) 152 153 responses = client.streaming_recognize(streaming_config, requests) 154 155 # Now, put the transcription responses to use. 156 listen_print_loop(responses) 157 158if __name__ == '__main__': 159 main() 160 161
試したこと
pip install google-cloud-speech
補足情報(FW/ツールのバージョンなど)
公式のpythonコードが書いてあるページ
https://cloud.google.com/speech-to-text/docs/streaming-recognize?hl=ja#speech-streaming-mic-recognize-python
import errorで解決した?とされるサイト
https://qiita.com/hanlio/items/875b91e0d4931a57e86b
参考にしていたサイト
https://qiita.com/hamham/items/3733ac8cd9e3d7b9ccae
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/12/15 13:28