使用 Watson 和 IoT Platform 服务构建家庭助理移动应用程序(8)
 
- UID
- 1066743
|

使用 Watson 和 IoT Platform 服务构建家庭助理移动应用程序(8)
初始化- 创建一个名为 WatsonExt.swift 的文件,以便包含连接 Watson Text to Speech、Speech to Text 和 Conversation 服务的代码。为 ViewController 声明一个扩展。
- 要初始化 Watson Text to Speech 服务,需要从 Bluemix Service Credentials 获取用户名和密码。
1
2
| textToSpeech = TextToSpeech(username: Credentials.TextToSpeechUsername,
password: Credentials.TextToSpeechPassword)
|
- 要初始化 Watson Speech to Text 服务,需要从 Bluemix Service Credentials 获取用户名和密码。
1
2
| speechToTextSession = SpeechToTextSession(username: Credentials.SpeechToTextUsername,
password: Credentials.SpeechToTextPassword)
|
- 添加一个回调来处理语音到文本的转换结果。
1
| speechToTextSession?.onResults = onResults
|
- 最后,要初始化 Watson Conversation 服务,需要从 Bluemix Service Credentials 获取用户名和密码。此外,还需要使用 WORKSPACE_ID。在第一次连接时,Conversation 服务会定义一个 conversation_start 对话框,用于返回初始响应(通常配置为问候语)。因此,调用 didReceiveConversationResponse() 函数来附加该消息,并使用 Text to Speech 服务来合成该文本。保存上下文,后续交互中会用到它们。
1
2
3
4
5
6
7
8
9
10
| conversation = Conversation(username: Credentials.ConversationUsername,
password: Credentials.ConversationPassword,
version: "2017-03-12")
let failure = { (error: Error) in print(error) }
conversation?.message(withWorkspace: Credentials.ConversationWorkspaceID, failure: failure) {
response in
print("output.text: \(response.output.text)")
self.didReceiveConversationResponse(response.output.text)
self.context = response.context
}
|
Watson Speech to Text 服务sttStartStreaming() 函数用于连接到 Speech to Text 服务,而且可以启动传输请求。它还可以启动麦克风输入。以下函数会在用户按下麦克风按钮时调用。
1
2
3
4
5
6
7
8
9
10
| func sttStartStreaming() {
// define settings
var settings = RecognitionSettings(contentType: .opus)
settings.continuous = true
settings.interimResults = true
self.speechToTextSession?.connect()
self.speechToTextSession?.startRequest(settings: settings)
self.speechToTextSession?.startMicrophone()
}
|
sttStopStreaming() 函数用于停止 Speech-to-Text 服务。它也可以停止麦克风输入。以下函数会在用户释放麦克风按钮时调用。
1
2
3
4
5
6
7
8
9
10
| func sttStopStreaming() {
self.speechToTextSession?.stopMicrophone()
self.speechToTextSession?.stopRequest()
// No need to disconnect -- the connection will timeout if the microphone
// is not used again within 30 seconds. This avoids the overhead of
// connecting and disconnecting the session with every press of the
// microphone button.
//self.speechToTextSession?.disconnect()
//self.speechToText?.stopRecognizeMicrophone()
}
|
onResults() 回调函数基于来自 Speech to Text 服务的最佳转录结果来更新文本视图小部件。
1
2
3
4
| func onResults(results: SpeechRecognitionResults) {
self.inputToolbar.contentView.textView.text = results.bestTranscript
self.inputToolbar.toggleSendButtonEnabled()
}
|
Watson Text to SpeechttsSynthesize() 函数使用了 Text to Speech 服务,基于一个句子来合成一条语音。它会播放合成的音频数据。
1
2
3
4
5
6
7
8
9
| func ttsSynthesize(_ sentence: String) {
// Synthesize the text
let failure = { (error: Error) in print(error) }
self.textToSpeech?.synthesize(sentence, voice: SynthesisVoice.gb_Kate.rawValue, failure: failure) { data in
self.audioPlayer = try! AVAudioPlayer(data: data)
self.audioPlayer.prepareToPlay()
self.audioPlayer.play()
}
}
|
Watson ConversationconversationRequestResponse() 函数处理与 Conversation 服务的交互。它向 Conversation 服务发送请求(文本形式)并接收响应。然后它会调用 didReceiveConversationResponse() 函数来处理响应文本。最后,它会调用 issueCommand() 函数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| func conversationRequestResponse(_ text: String) {
let failure = { (error: Error) in print(error) }
let request = MessageRequest(text: text, context: self.context)
self.conversation?.message(withWorkspace: Credentials.ConversationWorkspaceID,
request: request,
failure: failure) {
response in
print(response.output.text)
self.didReceiveConversationResponse(response.output.text)
self.context = response.context
// issue command based on intents and entities
print("appl_action: \(response.context.json["appl_action"])")
self.issueCommand(intents: response.intents, entities: response.entities)
}
}
|
issueCommand() 函数破译来自 Conversation 服务的意图,并通过 Watson IoT Platform 服务将命令发送到 Raspberry Pi。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| func issueCommand(intents: [Intent], entities: [Entity]) {
for intent in intents {
print("intent: \(intent.intent), confidence: \(intent.confidence) ")
}
for entity in entities {
print("entity: \(entity.entity), value: \(entity.value)")
}
for intent in intents {
if intent.confidence > 0.9 {
switch intent.intent {
case "OnLight":
let command = Command(action: "On", object: "Light", intent: intent.intent)
sendToDevice(command, subtopic: "light")
case "OffLight":
let command = Command(action: "Off", object: "Light", intent: intent.intent)
sendToDevice(command, subtopic: "light")
case "TakePicture":
let command = Command(action: "Take", object: "Picture", intent: intent.intent)
sendToDevice(command, subtopic: "camera")
default:
print("No such command")
return
}
}
}
}
|
连接 Watson IoT Platform 服务本节将介绍如何设置与 Watson IoT Platform 服务的连接。
准备 Xcode 项目- 将以下代码添加到 Podfile 来安装 ,后者使用了 MQTT 协议并提供了一个实用的 Swift SDK 来与 Watson IoT Platform 服务通信。另外,添加 SwiftyJSON 来解析和格式化 JSON 消息。
1
2
| pod 'CocoaMQTT'
pod 'SwiftyJSON'
|
- 运行下面的命令来安装 CocoaMQTT 和 SwiftyJSON 依赖项。
|
|
|
|
|
|