LoginSignup
9
11

More than 5 years have passed since last update.

Google Cloud Speech APIを使って音声をテキストへ変換する

Last updated at Posted at 2017-10-31

iMacの内蔵マイクから入力した声を、Google Cloud Speech APIを使ってテキストに変換する例です。
日本語に対応しています。

参考にしたページ(ありがとうございます)
http://blog.fun-multicores.tokyo/p/blog-page_12.html

検証した環境

  • PC:iMac (21.5-inch, Late 2013)
  • OS:macOS High Sierra

インストールするもの

その他

GoogleアカウントをGoogle Cloud Platformに登録する(上記URLを参考に設定し、jsonファイルをダウンロードする)
環境変数を作っておく

export GCLOUD_PROJECT=プロジェクト名
export GOOGLE_APPLICATION_CREDENTIALS="jsonファイル名"

実行に必要なファイル

  • package.json
  • ***.json(上記の環境変数で設定したファイル)
  • test.js

package.jsonのあるディレクトリでnpm installすること。

実行方法

node test.js

マイクに向かって喋るとテキスト化されて表示される。

コード

package.json
{
  "name": "test",
  "version": "0.0.1",
  "private": true,
  "license": "Apache Version 2.0",
  "author": "Google Inc.",
  "scripts": {
    "test": "cd ..; npm run st -- --verbose speech/system-test/*.test.js"
  },
  "dependencies": {
    "@google-cloud/speech": "0.9.0",
    "@google-cloud/storage": "1.0.0",
    "node-record-lpcm16": "0.3.0"
  },
  "engines": {
    "node": ">=4.3.2"
  }
}
test.js
const record = require('node-record-lpcm16');
const speech = require('@google-cloud/speech');
const speechClient = speech();
const SAMPLE_RATE = 16000

const request = {
  config: {
    encoding: 'LINEAR16',
    sampleRateHertz: SAMPLE_RATE,
    languageCode: 'ja-jp'
  }
};

const recognizeStream = speechClient.createRecognizeStream(request)
  .on('error', console.error)
  .on('data', (data) => {
    console.log(data.results);
  });

record.start({
  sampleRate: SAMPLE_RATE,
  thresholdStart: 0.2, // silence threshold to start recording, overrides threshold
  thresholdEnd: 0.1, // silence threshold to end recording, overrides threshold
  silence: '0.1' // seconds of silence before ending
}).pipe(recognizeStream);
9
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
11