Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Pythonで声・音を扱ってみた(pyaudio,GCP)

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

 Pythonで声・音を扱ってみた(pyaudio,GCP)

Pythonで声・音を扱ってみた(pyaudio,GCP)

Avatar for vinorthman

vinorthman

May 18, 2020
Tweet

More Decks by vinorthman

Other Decks in Technology

Transcript

  1. ・04.⾃動議事メモの仕組みを考えてみた ローカルPC Python3.6 PCのマイク google.cloud .speech PyAudio audio_stream 発⾳レスポンスあれば 取込んだ⾳声Buff

    Cloud Speech-to-Text API GoogleCloudPlatform ⾳声Buff Text Google Sheets API 事前にSpredSheet⽤意(共有(Doc_idで) Textを Cellに追加 gspread Text Text
  2. ・05.順に解説 ローカルPC Python3.6 PCのマイク google.cloud .speech PyAudio audio_stream 発⾳レスポンスあれば 取込んだ⾳声Buff

    Cloud Speech-to-Text API GoogleCloudPlatform ⾳声Buff Text Google Sheets API 事前にSpredSheet⽤意(共有(Doc_idで) Textを Cellに追加 gspread Text Text ▪PyAudio PyAudioは、Pythonのオーディオ関連ライブラリ ⾳声の録⾳、再⽣、書き出しに対応 Pip install pyaudio 例1) import pyaudio p = pyaudio.PyAudio() stream = p.open(format=pyaudio.paFloat32, channels=1, rate=44100, frames_per_buffer=1024, output=True) stream.write(stream,261.626(周波数),40(⾳の⻑さ),1)… ドレミのドの⾳ 例2) CHUNK = 1024 FORMAT = pyaudio.paInt16 # 16bit CHANNELS = 1 # monaural RATE = 48000 # sampling frequency [Hz] data = stream.read(CHUNK) ‘⾳声読み込み
  3. ・05.順に解説 ローカルPC Python3.6 PCのマイク google.cloud .speech PyAudio audio_stream 発⾳レスポンスあれば 取込んだ⾳声Buff

    Cloud Speech-to-Text API GoogleCloudPlatform ⾳声Buff Text Google Sheets API 事前にSpredSheet⽤意(共有(Doc_idで) Textを Cellに追加 gspread Text Text ▪google.cloud.speech ⾳声情報をCloud Speech-to-Text APIに渡し、Textを受け取る →⾳声ストリーム情報(⾳声,レート,モノラル,…)でAPIコール、 変換されたテキスト受け取り [設定] GCP側 ・Googleアカウントを作成→GCPコンソールにログイン&プロジェクト ・Google Speech-to-Text APIを有効にします。 「認証情報」→「認証情報を作成」→「サービスアカウントキー」→、jsonファイルを作成 PC側 pip install google gcloud google-auth google-cloud-speech grpc-google-cloud-speech-v1 pyaudio ・サービスアカウントキーを登録 Windowsの場合︓ set GOOGLE_APPLICATION_CREDENTIALS=[PATH]
  4. ・05.順に解説 ローカルPC Python3.6 PCのマイク google.cloud .speech PyAudio audio_stream 発⾳レスポンスあれば 取込んだ⾳声Buff

    Cloud Speech-to-Text API GoogleCloudPlatform ⾳声Buff Text Google Sheets API 事前にSpredSheet⽤意(共有(Doc_idで) Textを Cellに追加 gspread Text Text ▪google.cloud.speech ★Google Cloud Speech-to-Text API 認識モデル command_and_search ・・・ ⾳声検索やコマンドコールに適したモデル [以下、⽇本語対応していません] phone-call ・・・ 電話から発せられた⾳声に適したモデル video ・・・ 動画の字幕作成などに適したモデル
  5. ・05.順に解説 ローカルPC Python3.6 PCのマイク google.cloud .speech PyAudio audio_stream 発⾳レスポンスあれば 取込んだ⾳声Buff

    Cloud Speech-to-Text API GoogleCloudPlatform ⾳声Buff Text Google Sheets API 事前にSpredSheet⽤意(共有(Doc_idで) Textを Cellに追加 gspread Text Text ▪google.cloud.speech ⾳声情報をCloud Speech-to-Text APIに渡し、Textを受け取る →⾳声ストリーム情報(⾳声,レート,モノラル,…)でAPIコール、 変換されたテキスト受け取り 例) from google.cloud import speech_v1 as speech client = speech.SpeechClient() config = types.RecognitionConfig( encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16, sample_rate_hertz=self.rate, model=None, speech_contexts=[types.SpeechContext( )], language_code=language_code) streaming_config = types.StreamingRecognitionConfig( config=config, single_utterance=True, interim_results=True ) audio_generator = stream.generator() requests = (types.StreamingRecognizeRequest(audio_content=content) for content in audio_generator) responses = client.streaming_recognize(streaming_config, requests) GCP上のサンプルソース参照 してください。
  6. ・05.順に解説 ローカルPC Python3.6 PCのマイク google.cloud .speech PyAudio audio_stream 発⾳レスポンスあれば 取込んだ⾳声Buff

    Cloud Speech-to-Text API GoogleCloudPlatform ⾳声Buff Text Google Sheets API 事前にSpredSheet⽤意(共有(Doc_idで) Textを Cellに追加 gspread Text Text ▪gspread 前処理で⾳声から変換されたテキスト(⽇本語)を Google Spreadに追記する 例) import gspread →spreadのdoc_id(URLから)のsheet1を指定 →テキストを⾏追記していく doc_id = '1tUYPnGdUu8Nbzgu_exvVbUdqoPrDAKNNPJSweiCFOxM' client = gspread.authorize(credentials) googlefile = client.open_by_key(doc_id) worksheet = googlefile.sheet1 worksheet.append_row([text])
  7. ・06.とにかくこれで、⾃宅でテストして動作した︕ ローカルPC Python3.6 PCのマイク google.cloud .speech PyAudio audio_stream 発⾳レスポンスあれば 取込んだ⾳声Buff

    Cloud Speech-to-Text API GoogleCloudPlatform ⾳声Buff Text Google Sheets API 事前にSpredSheet⽤意(共有(Doc_idで) Textを Cellに追加 gspread Text Text ポッドキャストでNHK ニュースを流してみた。 ニュース⾳声から変換されたテキストが追記されていった︕ 例) 東京都は、13⽇都内で新たに10⼈が新型コロナウイルスに感染していることを確認したと発表し ました。また、12⽇の都内の陽性率は暫定値で5.0%でした。 ⭐良かった点 ・かなり正確変換、⽇本語特有の同⾳異義語も息継ぎや⽂終了までに⽂脈から推定して訂正して くれる。 ・専⾨⽤語の変換ミスがあるが、時事ネタもキチンと変換。
  8. ・07.うまくいかない理由、また考慮する必要がある事 ローカルPC Python3.6 PCのマイク google.cloud .speech PyAudio audio_stream 発⾳レスポンスあれば 取込んだ⾳声Buff

    Cloud Speech-to-Text API GoogleCloudPlatform ⾳声Buff Text Google Sheets API 事前にSpredSheet⽤意(共有(Doc_idで) Textを Cellに追加 gspread Text Text 1.Cloud Speech-to-Text API のレスポンスが悪い。 ①特に⾳声6〜7秒以上の場合。処理が追いつかないと、 読み漏れで変換を⾶ばしてしまう。 →これが致命的。(⽇本語だからか︖) (テストしたリソースは⼗分なハズだが…︓eo光、PC︓i5 MEM16G) 2.課⾦が発⽣する,受ける⾳声データの上限がある。 今は…無料で$300分のサービス利⽤中だが、$0.006/15 秒 ⾳声データ,1回利⽤5分まで、1⽇:1728秒=19分弱 3.Proxy環境でポート制限(ssh)があると、利⽤できない
  9. ・07.うまくいかない理由、また考慮する必要がある事 ローカルPC Python3.6 PCのマイク google.cloud .speech PyAudio audio_stream 発⾳レスポンスあれば 取込んだ⾳声Buff

    Cloud Speech-to-Text API GoogleCloudPlatform ⾳声Buff Text Google Sheets API 事前にSpredSheet⽤意(共有(Doc_idで) Textを Cellに追加 gspread Text Text 3.pyaudio ①端末の環境に依存する部分が⼤きい︕ ・pythonは、3.6までしか対応していない(最近更新されていない) ・OS︓macOS,ubuntu,windows10proと試したが…唯⼀win10proのみ動作 →ネット上では、⾳声のサンプリングレートや1回の採取時間 を調整すれば…など情報があったが、うまく⾏きませんでした。
  10. ・08.今回、トライしてみて ローカルPC Python3.6 PCのマイク google.cloud .speech PyAudio audio_stream 発⾳レスポンスあれば 取込んだ⾳声Buff

    Cloud Speech-to-Text API GoogleCloudPlatform ⾳声Buff Text Google Sheets API 事前にSpredSheet⽤意(共有(Doc_idで) Textを Cellに追加 gspread Text Text 今回、トライしてみて、 ①⾳声データのAPIへの受け渡しなどで改善の余地ありだが、 やはり⽇本語&複数⼈の会話の⽂字起こしは、まだハードルが⾼い いずれ…ZoomやGoogleMeetで⽇本語⽂字起こし機能がでてくるだろうが… ②とはいえ、GCPのAPI、制限があるもの、簡単に利⽤できたかな。 (もう⾳声の話で無くなっていますが…) APIサービスの組み合わせで、様々な仕組みが作れそうだと実感 ③もうひとつ、副産物として…
  11. ・09.もうひとつ、副産物として… ローカルPC Python3.6 PCのマイク google.cloud .speech PyAudio audio_stream Cloud Speech-to-Text

    API GoogleCloudPlatform ⾳声Buff Text Google Sheets API 事前にSpredSheet⽤意(共有(Doc_idで) Textを Cellに追加 gspread Text Text もうひとつ、副産物として…趣味のアナログシンセの⾳の⾒える化︔︔ せっかく⾳声データの取り込めるようになったので、 ⾳声の⾒える化、やってみた。 ①オシロスコープ X軸︓時間、Y軸︓⾳の⼤きさ、周波数の波 ↓フーリエ変換掛けると… ②スペクトルアナライザー x:周波数 , y:周波数毎の⾳の⼤きさ PyQt5 pyqtgraph