4
1

More than 3 years have passed since last update.

Google Colaboratory上からIBM Watson Speech to Textをいじってみた

Posted at

業務の中ある程度長い会話を録音した音声ファイルを文字起こししたいと思う機会がありました。
Google Cloud Speech-to-Textなどに代表される文字起こしAPIを調べたところ、実装が比較的容易という評判及び500分/月まで使える無料枠がある(2020/07/09現在)ことからIBM Watson Speech to Textを使えないかあれこれ試してみました。

結果的に、Google ColaboratoryでPythonコードを実行することでWatson Speek to Textを実行させることに成功したので備忘録としてまとめます。

IBM Watson Speech to textの概要については@ysugiyama12さんの「音声を文字起こしするAPIを何種類か触ってみた」が大変分かりやすいかと思います。

IBM Cloudのアカウントが有ればこのSpeech to Textが1ヶ月あたり500分まで無料で使えます。単純計算ですがGoogle Cloud Speech-to-Textの5倍の無料枠です。ちなみに500分を超えると1分ごとに最大で0.02米ドルかかるとのこと。

開発環境

Python 3.6.9 (Google Colaboratory)
IBM Watson(4.5.0)

大雑把な流れ

IBM Watson Speech to Text編

  • IBM Cloudのサイトより、ダッシュボードにアクセス
  • Watson Speek to textのリソースを作成する
  • 管理画面よりAPI鍵とURLを発行する

Google Colaboratory編

  • 音声ファイルをwavまたはflacで用意(今回はflacを用意しました)
  • 音声ファイルをGoogle Driveにアップロード
  • Google DriveをGoogle Colaboratoryにマウント 参考
  • Google ColaboratoryのインスタンスにIBM Watsonのインストール(4.5.0以上) 参考1 参考2 
  • Pythonスクリプトを用意
  • スクリプトの実行

Sample Code

管理画面にあるAPI鍵とURLを置き換えてください。
音声ファイルはGoogle Driveの任意の位置にアップロードし、そのパスを指定すると読み込めます。
結果は本文のみを1つのテキストファイルにまとめて音声ファイルと同じパスに出力しています。
こちらのコードを一部改変したものになります。

# from watson_developer_cloud import SpeechToTextV1
from ibm_watson import SpeechToTextV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
import json

# define
apikey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
url = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
audio_file = open("/content/drive/My Drive/hogehoge.flac", "rb")
cont_type = "audio/flac"
lang = "ja-JP_BroadbandModel"

authenticator = IAMAuthenticator("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
stt = SpeechToTextV1(
   authenticator=authenticator
)
stt.set_service_url("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")

#print
sttResult = result_json.get_result()
print(sttResult)

#save text file
with open('/content/drive/My Drive/hugahuga.txt', 'w') as f:
  raw_text=[]
  for i in range(len(sttResult["results"])):
      txt = sttResult["results"][i]["alternatives"][0]["transcript"]
      raw_text.append(txt)
  for i in range(len(raw_text)):
    f.write(raw_text[i])
  f.close()

詰まった箇所

はじめはSpeechToTextV1にAPI鍵とURLを渡す書き方をしていたのですが、実行するとTypeErrorが出て動かず困っておりました。
已む無くエラー文でググり、出てきたStackoverflowの記事を見たところ、「今はIAMAuthenticatorをimportする書き方じゃないとエラーでるよ」的な回答をされている方がおられました。
そのアドバイスを見つつAPI鍵とURLの情報をもたせたら無事に動くようになりました。

終わりに

環境構築を考えなくてもある程度なんとでもなるColaboratoryは凄いですねぇ。
IBM Watsonも楽に使えるよう考えられて作られている様子がちらほら見えてですごくありがたいです。
加えて先達の皆様の知恵のお陰で500時間/月もの無料枠が使えるAI文字起こし的サムシングがシュッと作れました。
日本語の文章としては手直しは必要そうではありますが、それも今後どんどん改善されていくのでしょう。

リファレンス

音声を文字起こしするAPIを何種類か触ってみた
[Python]WatsonのSpeech To Textを使うお話
IBM Cloud
Speech to Text - IBM Cloud API Docs
【秒速で無料GPUを使う】深層学習実践Tips on Colaboratory
Getting the following TypeError: __init__() got an unexpected keyword argument 'iam_apikey'

Google Colaboratory と IBM Watson Personality Insights を用いてクラウド環境上で性格診断ツールを作る

4
1
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
4
1