speech framework
TRANSCRIPT
Copyright © Up-frontier, Inc. All rights reserved.
Speech Framework
1
Copyright © Up-frontier, Inc. All rights reserved.
アジェンダ• Speech Framework とは
• 使い⽅
• 使いどころ
2
Copyright © Up-frontier, Inc. All rights reserved.
アジェンダ• Speech Framework とは
• 使い⽅
• 使いどころ
3
Copyright © Up-frontier, Inc. All rights reserved.
…の前に、まずは Keyboard Dictation
4
Copyright © Up-frontier, Inc. All rights reserved.
Keyboard Dictation• iPhone 4S からキーボードに内蔵
されていた、⾳声⼊⼒機能
• OS の機能であって、アプリの機能ではない
• 設定 → ⼀般 → キーボード → ⾳声⼊⼒
5
Copyright © Up-frontier, Inc. All rights reserved.
Keyboard Dictation• iPhone 4S からキーボードに内蔵
されていた、⾳声⼊⼒機能
• OS の機能であって、アプリの機能ではない
• 設定 → ⼀般 → キーボード → ⾳声⼊⼒
6
これ
Copyright © Up-frontier, Inc. All rights reserved.
Keyboard Dictation• 有効無効の設定を知るための API がない
• 必ずキーボードとセット
• キーボード⾔語に依存
7
Copyright © Up-frontier, Inc. All rights reserved.
…では改めて Speech Framework
8
Copyright © Up-frontier, Inc. All rights reserved.
Speech Framework• 平たく⾔うと、⾳声認識APIです
• 受け取った⾳声をテキストとして扱う
• 認識の部分は Siriと同じエンジン を利⽤
• リッチな結果(ベストと候補、確度など)
• ⾳声ファイルからの⼊⼒も可能
9
Copyright © Up-frontier, Inc. All rights reserved.
• マイクを利⽤したリアルタイム⾳声 • wavやmp3などの⾳声データでもOK
10
Copyright © Up-frontier, Inc. All rights reserved.
注意点• 基本ネットワーク接続が必須
• リクエストに制限あり (アプリごと、端末ごと)
• 機密情報は NG (password, health, financial)
• ユーザに親切な UI を⼼がける- 録⾳中、認識途中の結果、最終的な認識結果
11
Copyright © Up-frontier, Inc. All rights reserved.
アジェンダ• Speech Framework とは
• 使い⽅
• 使いどころ
12
Copyright © Up-frontier, Inc. All rights reserved.
① ユーザの許可を得る
13
Copyright © Up-frontier, Inc. All rights reserved.
ユーザ許可
SFSpeechRecognizer.requestAuthorization { status in switch status { case .authorized: case .denied: case .notDetermined: case .restricted: } }
14
Copyright © Up-frontier, Inc. All rights reserved.
ユーザ許可
SFSpeechRecognizer.requestAuthorization { status in switch status { case .authorized: case .denied: case .notDetermined: case .restricted: } }
15
よくある権限ステータス (位置情報アクセスなど)
Copyright © Up-frontier, Inc. All rights reserved.
ユーザ許可
SFSpeechRecognizer.requestAuthorization { status in switch status { case .authorized: case .denied: case .notDetermined: case .restricted: } }
16
デバイス判断で NG 前述の”リクエスト制限”によるもの?
Copyright © Up-frontier, Inc. All rights reserved.17
Copyright © Up-frontier, Inc. All rights reserved.18
Copyright © Up-frontier, Inc. All rights reserved.
Info.plist
<key>NSSpeechRecognitionUsageDescription</key> <string>SpeechFramework の使⽤⽬的</string>
19
Copyright © Up-frontier, Inc. All rights reserved.
Info.plist
<key>NSSpeechRecognitionUsageDescription</key> <string>SpeechFramework の使⽤⽬的</string>
20
ローカライズが必要な場合は、 InfoPlist.string を利⽤しましょう。
(plistじゃ視認性も悪いしね)
Copyright © Up-frontier, Inc. All rights reserved.
② リクエストを作る
21
Copyright © Up-frontier, Inc. All rights reserved.
リクエスト
guard let audioURL = R.file.speechMp3() else { print("Could not get file URL!") return } let request = SFSpeechURLRecognitionRequest(url: audioURL)
22
Copyright © Up-frontier, Inc. All rights reserved.
リクエスト
guard let audioURL = R.file.speechMp3() else { print("Could not get file URL!") return } let request = SFSpeechURLRecognitionRequest(url: audioURL)
23
対象のファイルのURLを⽣成して
Copyright © Up-frontier, Inc. All rights reserved.
リクエスト
guard let audioURL = R.file.speechMp3() else { print("Could not get file URL!") return } let request = SFSpeechURLRecognitionRequest(url: audioURL)
24
リクエストを⽣成する
Copyright © Up-frontier, Inc. All rights reserved.
③ タスクを作って実⾏
25
Copyright © Up-frontier, Inc. All rights reserved.
タスクlet recognizer = SFSpeechRecognizer(
locale: Locale(identifier: "ja-JP")) recognizer?.recognitionTask(with: request) { result, error in
guard let result = result else { print("Error on recognition task: \(error)") return
} self.textView.text = result .bestTranscription.formattedString print(result.transcriptions)
}
26
Copyright © Up-frontier, Inc. All rights reserved.
タスクlet recognizer = SFSpeechRecognizer(
locale: Locale(identifier: "ja-JP")) recognizer?.recognitionTask(with: request) { result, error in
guard let result = result else { print("Error on recognition task: \(error)") return
} self.textView.text = result .bestTranscription.formattedString print(result.transcriptions)
}
27
ロケールを指定して SFSpeechRecognizer を⽣成
Copyright © Up-frontier, Inc. All rights reserved.
タスクlet recognizer = SFSpeechRecognizer(
locale: Locale(identifier: "ja-JP")) recognizer?.recognitionTask(with: request) { result, error in
guard let result = result else { print("Error on recognition task: \(error)") return
} self.textView.text = result .bestTranscription.formattedString print(result.transcriptions)
}
28
en-US を指定すれば、⽶英語
Copyright © Up-frontier, Inc. All rights reserved.
タスクlet recognizer = SFSpeechRecognizer(
locale: Locale(identifier: “ja-JP")) recognizer?.recognitionTask(with: request) { result, error in
guard let result = result else { print("Error on recognition task: \(error)") return
} self.textView.text = result .bestTranscription.formattedString print(result.transcriptions)
}
29
事前に⽣成した request を指定して タスクメソッドを実⾏する。
Copyright © Up-frontier, Inc. All rights reserved.
タスクlet recognizer = SFSpeechRecognizer(
locale: Locale(identifier: “ja-JP")) recognizer?.recognitionTask(with: request) { result, error in
guard let result = result else { print("Error on recognition task: \(error)") return
} self.textView.text = result .bestTranscription.formattedString print(result.transcriptions)
}
30
結果はクロージャで受け取る
Copyright © Up-frontier, Inc. All rights reserved.
タスクlet recognizer = SFSpeechRecognizer(
locale: Locale(identifier: “ja-JP")) recognizer?.recognitionTask(with: request) { result, error in
guard let result = result else { print("Error on recognition task: \(error)") return
} self.textView.text = result .bestTranscription.formattedString print(result.transcriptions)
}
31
Copyright © Up-frontier, Inc. All rights reserved.
タスクlet recognizer = SFSpeechRecognizer(
locale: Locale(identifier: “ja-JP")) recognizer?.recognitionTask(with: request) { result, error in
guard let result = result else { print("Error on recognition task: \(error)") return
} self.textView.text = result .bestTranscription.formattedString print(result.transcriptions)
}
32
1番確度の⾼いものを いい感じのフォーマットで受け取る
Copyright © Up-frontier, Inc. All rights reserved.
タスクlet recognizer = SFSpeechRecognizer(
locale: Locale(identifier: “ja-JP")) recognizer?.recognitionTask(with: request) { result, error in
guard let result = result else { print("Error on recognition task: \(error)") return
} self.textView.text = result .bestTranscription.formattedString print(result.transcriptions)
}
33
bestTranscription
導出過程の候補はここにある
Copyright © Up-frontier, Inc. All rights reserved.
使い⽅まとめ1. ユーザに許可を求めて
2. リクエストを作って
3. SFSpeechRecognizer にタスクとしてお願い
34
Copyright © Up-frontier, Inc. All rights reserved.
アジェンダ• Speech Framework とは
• 使い⽅
• 使いどころ
35
Copyright © Up-frontier, Inc. All rights reserved.
⾶びつく前に• Keyboard Dictation で実現できないかを
まず考える
• リクエスト制限などを考えると、コアの機能には持ってきにくい - 具体的な制限回数については明記なし
36
Copyright © Up-frontier, Inc. All rights reserved.
考えられるユースケース• 「チーズ」という⾔葉に反応して、
⾃動でシャッターを切るカメラ (WWDCビデオ)
• 外国語の発⾳練習
• ⾃作キーボードアプリに⾳声⼊⼒機能を実装
• Podcast やボイスレコーダーのテキスト起こし
37
Copyright © Up-frontier, Inc. All rights reserved.
考えられるユースケース
Speech Framework を利⽤した、英単語の発⾳練習アプリ
38
Copyright © Up-frontier, Inc. All rights reserved.
考えられるユースケース
Speech Framework を利⽤した、英単語の発⾳練習アプリ
39
Copyright © Up-frontier, Inc. All rights reserved.
まとめ
• 本家 Apple 製の⾳声認識API
• 解析エンジンは Siri と同様のもの
• マイク⼊⼒とファイル⼊⼒の両⽅で利⽤可能
• 利⽤回数や利⽤時間に制限がある
40