前提・実現したいこと
Pythonでこま切れに音声認識をしたいです。
一番下のfor文を1周はできますが2周目の録音開始直前にエラーが出ます。
発生している問題・エラーメッセージ
Segmentation fault: 11
該当のソースコード
Python
1import threading 2import time 3import pyaudio 4import wave 5 6content_file = "/Users/[ユーザー名]/Documents/voice2text/"+input("メモファイルの名前は(英数字):")+".txt" 7name = 'voiceMemo.wav' 8iDeviceIndex = 0 # 録音機器の所在 9FORMAT = pyaudio.paInt16 10CHANNELS = 1 # モノラル 11RATE = 16000 # サンプルレート 12CHUNK = 2**11 # データ点数 13audio = pyaudio.PyAudio() 14rec = False # 録音フラグ 15 16 17def switching(): # 録音終了のためのスイッチング 18 end = "_" 19 global rec 20 while end != "": 21 if rec == True: 22 end = input('Enterで終了:') 23 if end == "": 24 rec = False 25 26 27def REC(): # 録音 28 frames = [] 29 global rec 30 while rec == False: 31 go = input('Enterで開始:') 32 if go == "": 33 rec = True 34 print("recording...") 35 36 stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, input_device_index=iDeviceIndex, frames_per_buffer=CHUNK) 37 t_start = time.time() # 時間計測開始 38 while True: 39 data = stream.read(CHUNK, exception_on_overflow = False) 40 frames.append(data) 41 if rec == False or (time.time() - t_start) >= 58: # キーが押されているか or 58s経過したか 42 break 43 print("finished recording") 44 stream.stop_stream() 45 stream.close() 46 audio.terminate() 47 48 waveFile = wave.open(name, "wb") 49 waveFile.setnchannels(CHANNELS) 50 waveFile.setsampwidth(audio.get_sample_size(FORMAT)) 51 waveFile.setframerate(RATE) 52 waveFile.writeframes(b''.join(frames)) 53 waveFile.close() 54 55 56def transcribe_file(speech_file): # 音声認識 57 """Transcribe the given audio file.""" 58 from google.cloud import speech 59 from google.cloud.speech import enums 60 from google.cloud.speech import types 61 import io 62 global text 63 client = speech.SpeechClient() 64 65 with io.open(speech_file, 'rb') as audio_file: 66 content = audio_file.read() 67 audio = types.RecognitionAudio(content=content) 68 config = types.RecognitionConfig( 69 encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16, 70 sample_rate_hertz=16000, 71 language_code='ja-JP') 72 response = client.recognize(config, audio) 73 # Each result is for a consecutive portion of the audio. Iterate through 74 # them to get the transcripts for the entire audio file. 75 for result in response.results: 76 # The first alternative is the most likely one for this portion. 77 print(u'Transcript: {}'.format(result.alternatives[0].transcript)) 78 text += result.alternatives[0].transcript 79 80 81for _ in range(10): 82 text = "" # 音声を文字に変換したものを入れる 83 thread1 = threading.Thread(target=switching) 84 thread2 = threading.Thread(target=REC) 85 thread3 = threading.Thread(target=transcribe_file, args=('voiceMemo.wav',)) 86 thread1.start() 87 thread2.start() 88 thread1.join() 89 thread2.join() 90 thread3.start() 91 thread3.join() 92 93 with open(content_file, mode='a') as f: 94 f.write('\n'+text) 95 96 with open(content_file) as f: 97 print(f.read()) 98 99 time.sleep(1)
試したこと
pysnooperでswitching()とREC()を検査
<2周目>
・・・
line 25 while end != "":
line 26 if rec == True:
line 25 while end != "":
line 26 if rec == True:
line 25 while end != "":
New var:....... go = ''
line 26 if rec == True:
line 37 if go == "":
line 25 while end != "":
line 26 if rec == True:
line 38 rec = True
line 25 while end != "":
line 39 print("recording...")
recording...
line 26 if rec == True:
line 35 while rec == False:
line 27 end = input('Enterで終了:')
line 41 stream = audio.open(format=FORMAT, channels=CHANNELS,rate=RATE,input=True,
input_device_index=iDeviceIndex, frames_per_buffer=CHUNK)
Segmentation fault: 11
補足情報(FW/ツールのバージョンなど)
Python 3.6.0
google-cloud-speech 1.3.2
PyAudio 0.2.11
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/02/18 13:44