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

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

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

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

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Python 3.x

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

Python

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

Google

Googleは、アメリカ合衆国に位置する、インターネット関連のサービスや製品を提供している企業です。検索エンジンからアプリケーションの提供まで、多岐にわたるサービスを提供しています。

Q&A

解決済

1回答

1779閲覧

Segmentation fault: 11エラー Pythonで音声認識 お助けください

cl_

総合スコア9

Google Cloud Platform

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

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Python 3.x

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

Python

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

Google

Googleは、アメリカ合衆国に位置する、インターネット関連のサービスや製品を提供している企業です。検索エンジンからアプリケーションの提供まで、多岐にわたるサービスを提供しています。

0グッド

0クリップ

投稿2020/02/18 06:29

前提・実現したいこと

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

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

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

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

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

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

guest

回答1

0

ベストアンサー

ループ内で、audio.terminate()を呼ばないようにするか、
openの直前にaudio = pyaudio.PyAudio()を持ってきてはいかがですか?

投稿2020/02/18 09:32

t_obara

総合スコア5488

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

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

cl_

2020/02/18 13:44

openの直前に audio = pyaudio.Pyaudio() 持ってきたら上手くいきました、最高です。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問