実現したいこと
マイクから発言した言葉をスプレッドシートに書き込みたいです。
参考にしたサイト
エラーはなく、音声はcmdで文字化は出来ていますが、スプレッドシートに書き込みできません・・・
大変申し訳ないですが、助言・アドバイスいただけると幸いです。
発生している問題・エラーメッセージ
スプレッドシートに書き込みできない 別のソースでスプレッドシートに書き込みはできています。
該当のソースコード
python
1from __future__ import division 2import os 3import re 4import sys 5from google.cloud import speech_v1 as speech 6import pyaudio 7from six.moves import queue 8 9os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '自分のjson' 10 11from oauth2client.service_account import ServiceAccountCredentials 12from httplib2 import Http 13import gspread 14import json 15import time 16from datetime import datetime 17 18 19scopes = ['https://www.googleapis.com/auth/spreadsheets'] 20json_file = '自分のjson' 21credentials = ServiceAccountCredentials.from_json_keyfile_name(json_file, scopes=scopes) 22http_auth = credentials.authorize(Http()) 23 24 25doc_id = '自分のスプレッドシートID' 26client = gspread.authorize(credentials) 27googlefile = client.open_by_key(doc_id) 28worksheet = googlefile.sheet1 29def gSheetEdit(text): 30 worksheet.append_row([text]) 31 32 33# Audio recording parameters 34RATE = 16000 35CHUNK = int(RATE / 10) # 100ms 36 37class MicrophoneStream(object): 38 """Opens a recording stream as a generator yielding the audio chunks.""" 39 40 def __init__(self, rate, chunk): 41 self._rate = rate 42 self._chunk = chunk 43 44 # Create a thread-safe buffer of audio data 45 self._buff = queue.Queue() 46 self.closed = True 47 48 def __enter__(self): 49 self._audio_interface = pyaudio.PyAudio() 50 self._audio_stream = self._audio_interface.open( 51 format=pyaudio.paInt16, 52 # The API currently only supports 1-channel (mono) audio 53 # https://goo.gl/z757pE 54 channels=1, 55 rate=self._rate, 56 input=True, 57 frames_per_buffer=self._chunk, 58 # Run the audio stream asynchronously to fill the buffer object. 59 # This is necessary so that the input device's buffer doesn't 60 # overflow while the calling thread makes network requests, etc. 61 stream_callback=self._fill_buffer, 62 ) 63 64 self.closed = False 65 66 return self 67 68 def __exit__(self, type, value, traceback): 69 self._audio_stream.stop_stream() 70 self._audio_stream.close() 71 self.closed = True 72 # Signal the generator to terminate so that the client's 73 # streaming_recognize method will not block the process termination. 74 self._buff.put(None) 75 self._audio_interface.terminate() 76 77 def _fill_buffer(self, in_data, frame_count, time_info, status_flags): 78 """Continuously collect data from the audio stream, into the buffer.""" 79 self._buff.put(in_data) 80 return None, pyaudio.paContinue 81 82 def generator(self): 83 while not self.closed: 84 # Use a blocking get() to ensure there's at least one chunk of 85 # data, and stop iteration if the chunk is None, indicating the 86 # end of the audio stream. 87 chunk = self._buff.get() 88 if chunk is None: 89 return 90 data = [chunk] 91 92 # Now consume whatever other data's still buffered. 93 while True: 94 try: 95 chunk = self._buff.get(block=False) 96 if chunk is None: 97 return 98 data.append(chunk) 99 except queue.Empty: 100 break 101 102 yield b"".join(data) 103 104def listen_print_loop(responses): 105 """Iterates through server responses and prints them. 106 107 The responses passed is a generator that will block until a response 108 is provided by the server. 109 110 Each response may contain multiple results, and each result may contain 111 multiple alternatives; for details, see https://goo.gl/tjCPAU. Here we 112 print only the transcription for the top alternative of the top result. 113 114 In this case, responses are provided for interim results as well. If the 115 response is an interim one, print a line feed at the end of it, to allow 116 the next result to overwrite it, until the response is a final one. For the 117 final one, print a newline to preserve the finalized transcription. 118 """ 119 num_chars_printed = 0 120 for response in responses: 121 if not response.results: 122 continue 123 124 # The `results` list is consecutive. For streaming, we only care about 125 # the first result being considered, since once it's `is_final`, it 126 # moves on to considering the next utterance. 127 result = response.results[0] 128 if not result.alternatives: 129 continue 130 131 # Display the transcription of the top alternative. 132 transcript = result.alternatives[0].transcript 133 134 # Display interim results, but with a carriage return at the end of the 135 # line, so subsequent lines will overwrite them. 136 # 137 # If the previous result was longer than this one, we need to print 138 # some extra spaces to overwrite the previous result 139 overwrite_chars = " " * (num_chars_printed - len(transcript)) 140 141 if not result.is_final: 142 sys.stdout.write(transcript + overwrite_chars + "\r") 143 sys.stdout.flush() 144 145 num_chars_printed = len(transcript) 146 147 else: 148 print(transcript + overwrite_chars) 149 150 # Exit recognition if any of the transcribed phrases could be 151 # one of our keywords. 152 if re.search(r"\b(exit|quit)\b", transcript, re.I): 153 print("Exiting..") 154 break 155 156 num_chars_printed = 0 157 158def main(): 159 # See http://g.co/cloud/speech/docs/languages 160 # for a list of supported languages. 161 language_code = "ja-JP" # a BCP-47 language tag 162 163 client = speech.SpeechClient() 164 config = speech.RecognitionConfig( 165 encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16, 166 sample_rate_hertz=RATE, 167 language_code=language_code, 168 ) 169 170 streaming_config = speech.StreamingRecognitionConfig( 171 config=config, interim_results=True 172 ) 173 174 with MicrophoneStream(RATE, CHUNK) as stream: 175 audio_generator = stream.generator() 176 requests = ( 177 speech.StreamingRecognizeRequest(audio_content=content) 178 for content in audio_generator 179 ) 180 181 responses = client.streaming_recognize(streaming_config, requests) 182 183 # Now, put the transcription responses to use. 184 listen_print_loop(responses) 185 186 187if __name__ == "__main__": 188 main()
失礼いたしました。
下記元々google様のサンプルコード
### 該当のソースコード
```python
from __future__ import division
import re
import sys
from google.cloud import speech
import pyaudio
from six.moves import queue
# Audio recording parameters
RATE = 16000
CHUNK = int(RATE / 10) # 100ms
class MicrophoneStream(object):
"""Opens a recording stream as a generator yielding the audio chunks."""
def __init__(self, rate, chunk):
self._rate = rate
self._chunk = chunk
# Create a thread-safe buffer of audio data
self._buff = queue.Queue()
self.closed = True
def __enter__(self):
self._audio_interface = pyaudio.PyAudio()
self._audio_stream = self._audio_interface.open(
format=pyaudio.paInt16,
# The API currently only supports 1-channel (mono) audio
# https://goo.gl/z757pE
channels=1,
rate=self._rate,
input=True,
frames_per_buffer=self._chunk,
# Run the audio stream asynchronously to fill the buffer object.
# This is necessary so that the input device's buffer doesn't
# overflow while the calling thread makes network requests, etc.
stream_callback=self._fill_buffer,
)
self.closed = False
return self
def __exit__(self, type, value, traceback):
self._audio_stream.stop_stream()
self._audio_stream.close()
self.closed = True
# Signal the generator to terminate so that the client's
# streaming_recognize method will not block the process termination.
self._buff.put(None)
self._audio_interface.terminate()
def _fill_buffer(self, in_data, frame_count, time_info, status_flags):
"""Continuously collect data from the audio stream, into the buffer."""
self._buff.put(in_data)
return None, pyaudio.paContinue
def generator(self):
while not self.closed:
# Use a blocking get() to ensure there's at least one chunk of
# data, and stop iteration if the chunk is None, indicating the
# end of the audio stream.
chunk = self._buff.get()
if chunk is None:
return
data = [chunk]
# Now consume whatever other data's still buffered.
while True:
try:
chunk = self._buff.get(block=False)
if chunk is None:
return
data.append(chunk)
except queue.Empty:
break
yield b"".join(data)
def listen_print_loop(responses):
"""Iterates through server responses and prints them.
The responses passed is a generator that will block until a response
is provided by the server.
Each response may contain multiple results, and each result may contain
multiple alternatives; for details, see https://goo.gl/tjCPAU. Here we
print only the transcription for the top alternative of the top result.
In this case, responses are provided for interim results as well. If the
response is an interim one, print a line feed at the end of it, to allow
the next result to overwrite it, until the response is a final one. For the
final one, print a newline to preserve the finalized transcription.
"""
num_chars_printed = 0
for response in responses:
if not response.results:
continue
# The `results` list is consecutive. For streaming, we only care about
# the first result being considered, since once it's `is_final`, it
# moves on to considering the next utterance.
result = response.results[0]
if not result.alternatives:
continue
# Display the transcription of the top alternative.
transcript = result.alternatives[0].transcript
# Display interim results, but with a carriage return at the end of the
# line, so subsequent lines will overwrite them.
#
# If the previous result was longer than this one, we need to print
# some extra spaces to overwrite the previous result
overwrite_chars = " " * (num_chars_printed - len(transcript))
if not result.is_final:
sys.stdout.write(transcript + overwrite_chars + "\r")
sys.stdout.flush()
num_chars_printed = len(transcript)
else:
print(transcript + overwrite_chars)
# Exit recognition if any of the transcribed phrases could be
# one of our keywords.
if re.search(r"\b(exit|quit)\b", transcript, re.I):
print("Exiting..")
break
num_chars_printed = 0
def main():
# See http://g.co/cloud/speech/docs/languages
# for a list of supported languages.
language_code = "en-US" # a BCP-47 language tag
client = speech.SpeechClient()
config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=RATE,
language_code=language_code,
)
streaming_config = speech.StreamingRecognitionConfig(
config=config, interim_results=True
)
with MicrophoneStream(RATE, CHUNK) as stream:
audio_generator = stream.generator()
requests = (
speech.StreamingRecognizeRequest(audio_content=content)
for content in audio_generator
)
responses = client.streaming_recognize(streaming_config, requests)
# Now, put the transcription responses to use.
listen_print_loop(responses)
if __name__ == "__main__":
main()
```
元々質問したコードは、サンプルコードを修正して出来るようしています。
それから参考サイトの
■スプレッドシートに保存するためのコード
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
import gspread
scopes = ['https://www.googleapis.com/auth/spreadsheets']
json_file = '取得した認証キー(.json)'
credentials = ServiceAccountCredentials.from_json_keyfile_name(json_file, scopes=scopes)
http_auth = credentials.authorize(Http())
doc_id = 'ここにはスプレッドシートのidを記載(URLのhttps://docs.google.com/spreadsheets/d/以下の部分)'
client = gspread.authorize(credentials)
googlefile = client.open_by_key(doc_id)
worksheet = googlefile.sheet1
def gSheetEdit(text):
worksheet.append_row([text])
を増やしてますが、スプレッドシートにはアクセスできているみたいです。
ググっても参考になるものがでないので、関数などのヒントをいただきたいです。
回答1件
あなたの回答
tips
プレビュー