LoginSignup
2
1

More than 1 year has passed since last update.

Watson Speech to Text でカスタムモデルを使う

Posted at

初めに

Watson Speech to Text では、独自の文書を登録してカスタムモデルをつくることができます。
社内用語等が入った文書を登録することにより、そのような単語が入った音声の認識率をあげることができます。
ここでは、そのカスタムモデルを作成する手順をご紹介します。なお、言語はPythonを使用します。

前提

IBM Cloud にて、Speech to Text の有料のインスタンスが作成されていること
※ライトプラン(無料プラン)ではカスタムモデルは使用できません。

実施手順

1. カスタムモデルの作成

文書を登録するためのカスタムモデルを作成します。

create_cutommodel.py
import json
from ibm_watson import SpeechToTextV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

apiKey = ''     # インスタンスのAPIKeyを指定
url = ''                # インスタンスのEndPoint URLを指定

authenticator = IAMAuthenticator(apiKey)
speech_to_text = SpeechToTextV1(
    authenticator=authenticator
)

speech_to_text.set_service_url(url)

language_model = speech_to_text.create_language_model(
    'Test Custom Model',
    'ja-JP_BroadbandModel',
    description='Test Custom Model'
).get_result()
print(json.dumps(language_model, indent=2))

実行結果

python create_cusommodel.py

{
  "customization_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

cusomization_idの値は、このあと使用します。

2. 文書の登録

作成したカスタムモデルに文書を登録します。
ここでは、corpus1.txtという文書ファイルをcorpus1という名前で登録します。

add_corpus.py
from os.path import join, dirname
from ibm_watson import SpeechToTextV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

apiKey = ''     # インスタンスのAPIKeyを指定
url = ''                # インスタンスのEndPoint URLを指定
customization_id = ''    # モデル作成時に生成されたIDを指定

authenticator = IAMAuthenticator(apiKey)
speech_to_text = SpeechToTextV1(
    authenticator=authenticator
)

speech_to_text.set_service_url(url)

with open(join(dirname(__file__), './.', 'corpus1.txt'),
               'rb') as corpus_file:
    speech_to_text.add_corpus(
        customization_id,
        'corpus1',
        corpus_file
    )

実行結果

python add_corpus.py

※正常実行された場合は、何も出力されません

3. 登録文書の確認

文書が正常に登録された確認します。

get_corpus.py
import json
from ibm_watson import SpeechToTextV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

apiKey = ''     # インスタンスのAPIKeyを指定
url = ''                # インスタンスのEndPoint URLを指定
customization_id = ''    # モデル作成時に生成されたIDを指定

authenticator = IAMAuthenticator(apiKey)
speech_to_text = SpeechToTextV1(
    authenticator=authenticator
)

speech_to_text.set_service_url(url)

corpus = speech_to_text.get_corpus(
    customization_id,
    'corpus1'
).get_result()
print(json.dumps(corpus, indent=2))

実行結果

python get_cusommodel.py

{
  "out_of_vocabulary_words": 0,
  "total_words": 115,
  "name": "corpus1",
  "status": "analyzed"
}

※statusがanalyzedになっていることを確認します。

4. カスタムモデルの学習

登録した文書を反映させるために、学習を行います。

train_custommodel.py
from ibm_watson import SpeechToTextV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

apiKey = ''     # インスタンスのAPIKeyを指定
url = ''                # インスタンスのEndPoint URLを指定
customization_id = ''    # モデル作成時に生成されたIDを指定

authenticator = IAMAuthenticator(apiKey)
speech_to_text = SpeechToTextV1(
    authenticator=authenticator
)

speech_to_text.set_service_url(url)

speech_to_text.train_language_model(customization_id)

実行結果

python train_custommodel.py

※正常実行された場合は、何も出力されません

5. 学習結果の確認

学習が正常に終了したかを確認します。

get_custommodel.py
import json
from ibm_watson import SpeechToTextV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

apiKey = ''     # インスタンスのAPIKeyを指定
url = ''                # インスタンスのEndPoint URLを指定
customization_id = ''    # モデル作成時に生成されたIDを指定

authenticator = IAMAuthenticator(apiKey)
speech_to_text = SpeechToTextV1(
    authenticator=authenticator
)

speech_to_text.set_service_url(url)

language_model = speech_to_text.get_language_model(customization_id).get_result()
print(json.dumps(language_model, indent=2))

実行結果

python get_custommodel.py

{
  "owner": "xxxxxxxxxxxxxxxxxxxxxxxxx",
  "base_model_name": "ja-JP_BroadbandModel",
  "customization_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "dialect": "ja-JP",
  "versions": [
    "ja-JP_BroadbandModel.v2020-09-10"
  ],
  "created": "2023-01-31T00:29:34.370Z",
  "name": "Test Custom Model",
  "description": "Test Custom Model",
  "progress": 100,
  "language": "ja-JP",
  "updated": "2023-01-31T00:43:09.439Z",
  "status": "available"
}

※statusがavailableになっていることを確認します。

6. カスタムモデルを使用して、文字起こしを実行

transcribe.py
from os.path import join, dirname
import json
from ibm_watson import SpeechToTextV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

apiKey = ''     # インスタンスのAPIKeyを指定
url = ''                # インスタンスのEndPoint URLを指定
customization_id = ''    # モデル作成時に生成されたIDを指定

authenticator = IAMAuthenticator(apiKey)
speech_to_text = SpeechToTextV1(
    authenticator=authenticator
)

speech_to_text.set_service_url(url)

with open(join(dirname(__file__), './.', 'sample-medical1.wav'),
               'rb') as audio_file:
    speech_recognition_results = speech_to_text.recognize(
        audio=audio_file,
        content_type='audio/wav',
        model='ja-JP_BroadbandModel',
        language_customization_id=customization_id
    ).get_result()
print(json.dumps(speech_recognition_results, indent=2, ensure_ascii=False))

実行結果

python transcribe.py

{
  "result_index": 0,
  "results": [
    {
      "final": true,
      "alternatives": [
        {
          "transcript": "技術 の 概要 に つきまして ご説明 させて いただきます ",
          "confidence": 0.76
        }
      ]
    },
(以下省略)
2
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
2
1