40
Copyright © Up-frontier, Inc. All rights reserved. Speech Framework 1

Speech Framework

  • Upload
    gaprot

  • View
    386

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

Speech Framework

1

Page 2: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

アジェンダ• Speech Framework とは

• 使い⽅

• 使いどころ

2

Page 3: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

アジェンダ• Speech Framework とは

• 使い⽅

• 使いどころ

3

Page 4: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

…の前に、まずは Keyboard Dictation

4

Page 5: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

Keyboard Dictation• iPhone 4S からキーボードに内蔵

されていた、⾳声⼊⼒機能

• OS の機能であって、アプリの機能ではない

• 設定 → ⼀般 → キーボード → ⾳声⼊⼒

5

Page 6: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

Keyboard Dictation• iPhone 4S からキーボードに内蔵

されていた、⾳声⼊⼒機能

• OS の機能であって、アプリの機能ではない

• 設定 → ⼀般 → キーボード → ⾳声⼊⼒

6

これ

Page 7: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

Keyboard Dictation• 有効無効の設定を知るための API がない

• 必ずキーボードとセット

• キーボード⾔語に依存

7

Page 8: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

…では改めて Speech Framework

8

Page 9: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

Speech Framework• 平たく⾔うと、⾳声認識APIです

• 受け取った⾳声をテキストとして扱う

• 認識の部分は Siriと同じエンジン を利⽤

• リッチな結果(ベストと候補、確度など)

• ⾳声ファイルからの⼊⼒も可能

9

Page 10: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

• マイクを利⽤したリアルタイム⾳声 • wavやmp3などの⾳声データでもOK

10

Page 11: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

注意点• 基本ネットワーク接続が必須

• リクエストに制限あり (アプリごと、端末ごと)

• 機密情報は NG (password, health, financial)

• ユーザに親切な UI を⼼がける- 録⾳中、認識途中の結果、最終的な認識結果

11

Page 12: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

アジェンダ• Speech Framework とは

• 使い⽅

• 使いどころ

12

Page 13: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

① ユーザの許可を得る

13

Page 14: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

ユーザ許可

SFSpeechRecognizer.requestAuthorization { status in switch status { case .authorized: case .denied: case .notDetermined: case .restricted: } }

14

Page 15: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

ユーザ許可

SFSpeechRecognizer.requestAuthorization { status in switch status { case .authorized: case .denied: case .notDetermined: case .restricted: } }

15

よくある権限ステータス (位置情報アクセスなど)

Page 16: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

ユーザ許可

SFSpeechRecognizer.requestAuthorization { status in switch status { case .authorized: case .denied: case .notDetermined: case .restricted: } }

16

デバイス判断で NG 前述の”リクエスト制限”によるもの?

Page 17: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.17

Page 18: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.18

Page 19: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

Info.plist

<key>NSSpeechRecognitionUsageDescription</key> <string>SpeechFramework の使⽤⽬的</string>

19

Page 20: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

Info.plist

<key>NSSpeechRecognitionUsageDescription</key> <string>SpeechFramework の使⽤⽬的</string>

20

ローカライズが必要な場合は、 InfoPlist.string を利⽤しましょう。

(plistじゃ視認性も悪いしね)

Page 21: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

② リクエストを作る

21

Page 22: Speech Framework

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

Page 23: Speech Framework

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を⽣成して

Page 24: Speech Framework

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

リクエストを⽣成する

Page 25: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

③ タスクを作って実⾏

25

Page 26: Speech Framework

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

Page 27: Speech Framework

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 を⽣成

Page 28: Speech Framework

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 を指定すれば、⽶英語

Page 29: Speech Framework

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 を指定して タスクメソッドを実⾏する。

Page 30: Speech Framework

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

結果はクロージャで受け取る

Page 31: Speech Framework

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

Page 32: Speech Framework

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番確度の⾼いものを いい感じのフォーマットで受け取る

Page 33: Speech Framework

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

導出過程の候補はここにある

Page 34: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

使い⽅まとめ1. ユーザに許可を求めて

2. リクエストを作って

3. SFSpeechRecognizer にタスクとしてお願い

34

Page 35: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

アジェンダ• Speech Framework とは

• 使い⽅

• 使いどころ

35

Page 36: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

⾶びつく前に• Keyboard Dictation で実現できないかを

まず考える

• リクエスト制限などを考えると、コアの機能には持ってきにくい - 具体的な制限回数については明記なし

36

Page 37: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

考えられるユースケース• 「チーズ」という⾔葉に反応して、

⾃動でシャッターを切るカメラ (WWDCビデオ)

• 外国語の発⾳練習

• ⾃作キーボードアプリに⾳声⼊⼒機能を実装

• Podcast やボイスレコーダーのテキスト起こし

37

Page 38: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

考えられるユースケース

Speech Framework を利⽤した、英単語の発⾳練習アプリ

38

Page 39: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

考えられるユースケース

Speech Framework を利⽤した、英単語の発⾳練習アプリ

39

Page 40: Speech Framework

Copyright © Up-frontier, Inc. All rights reserved.

まとめ

• 本家 Apple 製の⾳声認識API

• 解析エンジンは Siri と同様のもの

• マイク⼊⼒とファイル⼊⼒の両⽅で利⽤可能

• 利⽤回数や利⽤時間に制限がある

40