LoginSignup
9

More than 5 years have passed since last update.

posted at

サクッとWatson Speech-to-TextをiOSで動かす

simon Sasakiさんの記事「Watson音声認識(STT:Speech to Text)をSwiftアプリで使ってみた。」を読んで、Watson Speech-to-TextのSwiftアプリを試そうとしたのですが、GitHubのwatson-developer-cloudの構成が変わっており、少し迷ったのでメモとして残します。

1. 事前準備

Carthageを使うのでインストールしておきます。Homebrewからインストール可能です。
$ brew install carthage

また、Bluemixにて、Speech to Textのサービスを作り、「Service credentials」に書かれているusername, passwordをメモしておきます。

Screen_Shot_stt.png

2. サンプルプロジェクトのクローンと依存関係の解決

watson-developer-cloud/swift-sdk内のソースではなく、Watson-developer-cloud/speech-to-text-swift を使います。

$ git clone https://github.com/watson-developer-cloud/speech-to-text-swift.git
$ cd speech-to-text-swift/

その後、Carthageで依存関係の解決をします。もともとSwift3.0.0でプリコンパイルされている状態のため、Swift 3.0.0以降の場合ではオプションを追加します。
$ carthage update --platform iOS --no-use-binaries(Swift 3.0.0以降の場合)
$ carthage update --platform iOS(Swift 3.0.0以前の場合)

3. Credentialファイルの編集

「Speech to text」ディレクトリ内にある「CredentialsExample.swift」をコピーし「Credentials.swift」を作ります。

$ cd Speech\ to\ Text
$ cp CredentialsExample.swift Credentials.swift`

Speech to Text.xcodeprojを開き、Credentials.swiftを編集します。SpeechToTextUsernameとSpeechToTextPasswordの値に、それぞれ 1.事前準備でメモしたサービスのusername, passwordを入力します。

Credentials.swift
struct Credentials {
    static let SpeechToTextUsername = "your-username-here"
    static let SpeechToTextPassword = "your-password-here"
}

4. 実行

あとは、Xcode上で「Speech to Text」プロジェクトのBundle IdentifierとTeamの変更をすれば、実行できるようになりました。

以前とアプリも少し変わっている模様。サンプルのAudio Fileからテキスト化するモード。
IMG_9771.PNG

マイクからの音声をテキスト化するモード。
IMG_9768.PNG

5. 日本語への対応

マイクからの音声をテキスト化するモードの
デフォルトの場合はmodelが英語(en-US_BroadbandModel)になってしまうようですが、recognizeMicrophoneの引数のmodelとして「ja-JP_BroadbandModel」に指定することで、日本語に対応できます。

MicrophoneViewController.swift
// start recognizing microphone audio
speechToText.recognizeMicrophone(settings: settings,  model: "ja-JP_BroadbandModel", failure: failure) {
    results in
    self.textView.text = results.bestTranscript
}

とりあえず日本語は出ました!
IMG_9770.PNG

周りがうるさい環境だと精度は良くないようです。あとはうちの猫の名前(ムーチョ)が「部長」になったりしているので、カスタムモデルを作って単語を登録し、呼び出せるようになれば認識精度も上がるのだろうと思います。また今度もうちょっといじってみたい。

参考資料

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
What you can do with signing up
9