0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【GCP】Cloud speech-to-text apiを触ってみた

Last updated at Posted at 2020-05-07

参考URL

https://qiita.com/knyrc/items/7aab521edfc9bfb06625
(参考URLの解説的なサイト:https://qrunch.net/@OrionB312/entries/4o6rlrHxW39xSC5n)

流れ

・プロジェクトを作成or既存のプロジェクトを選択
・APIを有効化
・認証情報を設定
 今回はサービスアカウントキーを使用

export GOOGLE_APPLICATION_CREDENTIALS=/home/***/***.json

・コマンドシェルを起動
・以下のコードを記載したファイルを作成

# !/usr/bin/env python
# coding: utf-8
import argparse
import io
import sys
import codecs
import datetime
import locale

def transcribe_gcs(gcs_uri):
    from google.cloud import speech
    from google.cloud.speech import enums
    from google.cloud.speech import types
    client = speech.SpeechClient()

    audio = types.RecognitionAudio(uri=gcs_uri)
    config = types.RecognitionConfig(
        sample_rate_hertz=16000,
        encoding=enums.RecognitionConfig.AudioEncoding.FLAC,
        language_code='ja-JP')

    operation = client.long_running_recognize(config, audio)

    print('Waiting for operation to complete...')
    operationResult = operation.result()

    d = datetime.datetime.today()
    today = d.strftime("%Y%m%d-%H%M%S")
    fout = codecs.open('output{}.txt'.format(today), 'a', 'utf-8')

    for result in operationResult.results:
      for alternative in result.alternatives:
          fout.write(u'{}\n'.format(alternative.transcript))
    fout.close()

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument(
        'path', help='GCS path for audio file to be recognized')
    args = parser.parse_args()
    transcribe_gcs(args.path)

・音声ファイルを作成
 例として、スマホアプリの「サウンド&ボイスレコーダー - ASR」なら簡単に作成可能。
  Flacファイルにして、16000HZ、モノラルで録音する。
・Storageに作成した音声ファイルを格納する。
・「Python ファイル名 音声ファイルのURI」でコマンドを実行
・同階層にファイルができる
・ファイルの中身を確認して完了

詳細な手順の記載

1.プロジェクトを作成する。
※ここでは解説いたしません。
2.作成したプロジェクトにて、speech-to-text api の有効化をする
3.コンソール画面上部検索窓に「speech-to-text api 」を入力し、「Cloud speech-to-text api」をクリック 
4.APIを有効にするをクリック
5.APImangerの認証情報にある認証情報を作成をクリック
6.サービスアカウントキーをクリック
7.サービスアカウントキーが作成されたことを確認し、作成されたサービスアカウントキーをダウンロードする
8.メニューからstorageをクリック
9.任意のストレージを選択し、音声ファイルをアップロードする
10.音声ファイルを作成する
 例として、スマホアプリの「サウンド&ボイスレコーダー - ASR」なら簡単に作成可能。
  Flacファイルにして、16000HZ、モノラルで録音する。
11.Cloud Shell を開く
12.pathを通す。
export GOOGLE_APPLICATION_CREDENTIALS=/home/ユーザ名/***.json
13.Cloud Shell の右上にあるアップロードからspeech_to_text_test.pyファイルをアップロードする
14.「Python ファイル名 音声ファイルのURI」でコマンドを実行
15.同階層に実行した日時をファイル名としたファイルができる
16.ファイルの中身を確認すると翻訳した内容が出力される

エラー

・エラー集
https://qiita.com/hanlio/items/875b91e0d4931a57e86b

1

ImportError: cannot import name 'speech_v1' from 'google.cloud' (unknown location)

https://qiita.com/arumiti/items/835e87c35dcbb69d8ca2
ここを参考に。

python3 → import sys → print(sys.path)

の順にコマンドを打つ。

pythonのインストール先がどこかが表示される。

このとき、python3ではなく、python2.7に保存されていた。
なので、python2.7で実行すれば正常に実行される。

2

https://docs.python.org/ja/3/library/codecs.html
codecsの部分をutf-8に直した

参考情報

Flacはハイレゾ音源らしい
https://support.digion.com/curiostyle/2017/07/24/hires-free/

今後やりたい

https://iti.hatenablog.jp/entry/2017/05/09/112051
https://qiita.com/saihara-k/items/86ba457523daa02c1869

https://blog.apar.jp/web/9893/
Text-to-Speech API だった

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?