LoginSignup
6
4

More than 3 years have passed since last update.

Google Cloud Speech to Textを試す

Last updated at Posted at 2020-04-28

音声のテキスト化を試してみたい

例えばこんなAPIがある

  • Google Cloud Speech-to-Text
  • Watson Speech to Text
  • Bing Speech API
  • Amazon Transcribe
  • NTT SpeechRec
  • AmiVoice

...etc

今回はこの中では割と簡単かつ制度がいいらしいということで、Google Cloud Speech-to-Textを試すこととする。


Google Cloud Speech-to-Textについて

特徴

  • Google Cloud Platformで提供されているAPI
  • 一月 60分の解析は無料。15秒ごとに$0.006
  • 対応言語は120種類
  • 音声ファイルの他にもストリーミング音声も対応している。

こちら本家のリンク


実行方法の種類

基本的な実行方法は大きく分けて3種類

  • 短い音声(1分未満)の解析に向いている「同期音声認識」
  • 長い音声の解析に向いている「非同期音声認識」
  • リアルタイムな解析が可能な「ストリーミング音声認識」

今回は「同期音声認識」と「非同期音声認識」を試す。


まずはクイックスタート

こちら本家のクイックスタート

コマンドラインで実行してみる。
実際に実行するまでには以下の手順も必要だったが、本題からそれるためここでは省略する。

  • Google Cloud Platformの登録
  • GCPのプロジェクト作成
  • 認証情報の作成
  • gcloudのインストール

上記の準備完了後に以下を実施

1.次のテキストを含む JSON リクエスト ファイルを作成し、sync-request.json 書式なしテキスト ファイルとして保存します。

sync-request.json
   {
      "config": {
          "encoding":"FLAC",
          "sampleRateHertz": 16000,
          "languageCode": "en-US",
          "enableWordTimeOffsets": false
      },
      "audio": {
          "uri":"gs://cloud-samples-tests/speech/brooklyn.flac"
      }
    }

引用元

GCPの認証を通すためGOOGLE_APPLICATION_CREDENTIALS環境変数に認証情報ファイルのパスを指定

2.curl を使用して speech:recognize リクエストを作成し、ステップ 1 で設定した JSON リクエストのファイル名を渡します。

    curl -s -H "Content-Type: application/json" \
        -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
        https://speech.googleapis.com/v1/speech:recognize \
        -d @sync-request.json

引用元

こちらは実際の実行結果

{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "how old is the Brooklyn Bridge",
          "confidence": 0.98314303
        }
      ]
    }
  ]
}

transcriptが変換された文章
confidenceはどれくらいの制度で変換されたかが0.0〜1.0で表される。ただし当てにはならない。


自分で用意したファイルで「同期音声認識」を試す

フリー素材サイトからセリフが入っている音声を適当に拾ってきてflacに変換
音声データをCloud Storageにアップする

音声データの指定方法はCloud Storageにアップするか、Base64に変換してテキストで直接指定することもできる。

今回は日本語をテキスト化するため、languageCodeja-JPを指定する。
FLACかWAVファイルなら、encodingsampleRateHertzは指定しなくても良い。
uriにはCloud StorageにアップしたファイルのURIを指定。

sync-request.json
{
  "config": {
    "languageCode": "ja-JP"
  },
  "audio": {
    "uri":"gs://xxxxx/xxxx.flac"
  }
}

「非同期音声認識」を試す

実行方法は同期音声認識とほぼ一緒
呼び出すメソッド名がspeech:longrunningrecognizeとなる。

ただし、非同期なので結果の取得方法が異なる。

実行

    curl -s -H "Content-Type: application/json" \
        -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
        https://speech.googleapis.com/v1/speech:longrunningrecognize \
        -d @sync-request.json

実行すると次のような結果が得られる

{
  "name": "7220377802738537728"
}

your-operation-nameは先程取得したnameに置き換えて下のコマンドを実行する。

curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
"https://speech.googleapis.com/v1/operations/your-operation-name"
6
4
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
6
4