音声認識APIを使ってプログラムを書いていました。
昨日までは正常に動作していたコードなのですが、突然KeyErrorでうまく認識結果を表示してくれなくなりました。
予想はresultsが空の状態で帰ってきているせいで不具合が起きていると思うのですが、解決策も思いつきません。
Python
1##### coding:utf8 2import base64 3from googleapiclient import discovery 4import httplib2 5 6import pyaudio #録音機能を使うためのライブラリ 7import wave #wavファイルを扱うためのライブラリ 8import time 9import math 10import audioop 11 12#APIキーを設定 13key = "APIキー" 14 15DISCOVERY_URL = ('https://{api}.googleapis.com/$discovery/rest?' 16 'version={apiVersion}') 17 18#音声を保存するファイル名 19WAVE_OUTPUT_FILENAME = "sample10.wav" 20 21RATE = 44100 22CHUNK = 1024 23CHANNELS = 1 24FORMAT = pyaudio.paInt16 25DECIBEL_THRESHOLD=50 26frames=[] 27frame_counter = 0 28start_Rec = False 29 30def get_speech_service(): 31 http = httplib2.Http() 32 return discovery.build( 33 'speech', 'v1', http=http, discoveryServiceUrl=DISCOVERY_URL, developerKey=key) 34 35def SpeechAPI(): 36 #音声ファイルを開く 37 waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb') 38 waveFile.setnchannels(CHANNELS) 39 waveFile.setsampwidth(p.get_sample_size(FORMAT)) 40 waveFile.setframerate(RATE) 41 waveFile.writeframes(b''.join(frames)) 42 waveFile.close() 43 with open(WAVE_OUTPUT_FILENAME, 'rb') as speech: 44 speech_content = base64.b64encode(speech.read()) 45 46 #APIの情報を取得して、音声認識を行う 47 service = get_speech_service() 48 service_request = service.speech().recognize( 49 body={ 50 'config': { 51 'encoding': 'LINEAR16', 52 'sampleRateHertz': 44100, 53 'languageCode': 'ja-JP', #日本語に設定 54 'enableWordTimeOffsets': 'false', 55 }, 56 'audio': { 57 'content': speech_content.decode('UTF-8') 58 } 59 }) 60 61 #SpeechAPIによる認識結果を保存 62 response = service_request.execute() 63 64 #見やすいようにコンソール画面で出力 65 for i in response["results"]: 66 print(i["alternatives"][0]["transcript"],"confidence:" , i["alternatives"][0]["confidence"]) 67 68if __name__=='__main__': 69 p = pyaudio.PyAudio() 70 71 stream = p.open(format=FORMAT, 72 channels=CHANNELS, 73 rate=RATE, 74 input=True, 75 frames_per_buffer=CHUNK) 76 while True: 77 data=stream.read(CHUNK)#ストリームを開く 78 rms=audioop.rms(data,2) 79 decibel=20*math.log10(rms) 80 if decibel >= DECIBEL_THRESHOLD:#音圧レベルが50dB以上なら録音開始 81 start_Rec=True 82 frame_counter=0 83 if start_Rec==True and decibel<DECIBEL_THRESHOLD: 84 frame_counter+=1 85 if frame_counter>=20:#録音開始後、20チャンク分の無音が続いた場合は録音終了 86 frame_counter=0 87 start_Rec=False 88 stream.stop_stream() 89 stream.close()#ストリーム閉じる 90 p.terminate() 91 SpeechAPI()#音声認識 92 stream = p.open(format=FORMAT,#ストリーム再開 93 channels=CHANNELS, 94 rate=RATE, 95 input=True, 96 frames_per_buffer=CHUNK) 97 frames=[] 98 if start_Rec==True: 99 frames.append(data)
KeyError Traceback (most recent call last) <ipython-input-7-e368a73a5a1c> in <module>() 89 stream.close() #ストリーム閉じる 90 p.terminate() ---> 91 SpeechAPI() #音声認識 92 stream = p.open(format=FORMAT,#ストリーム再開 93 channels=CHANNELS, <ipython-input-7-e368a73a5a1c> in SpeechAPI() 63 64 #見やすいようにコンソール画面で出力 ---> 65 for i in response["results"]: 66 print(i["alternatives"][0]["transcript"],"confidence:" , i["alternatives"][0]["confidence"]) 67 KeyError: 'results'
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/05/31 14:07
2018/05/31 19:50