Dialogflow,从音频检测意图
Dialogflow, detection intent from audio
我正在尝试将音频文件发送到 dialogflow API 以进行意图检测。我已经有一个代理工作得很好,但只能处理文本。我正在尝试添加音频功能,但没有成功。
我正在使用此页面中提供的示例 (Java):
https://cloud.google.com/dialogflow-enterprise/docs/detect-intent-audio#detect-intent-text-java
这是我的代码:
public DetectIntentResponse detectIntentAudio(String projectId, byte [] bytes, String sessionId,
String languageCode)
throws Exception {
// Set the session name using the sessionId (UUID) and projectID (my-project-id)
SessionName session = SessionName.of(projectId, sessionId);
System.out.println("Session Path: " + session.toString());
// Note: hard coding audioEncoding and sampleRateHertz for simplicity.
// Audio encoding of the audio content sent in the query request.
AudioEncoding audioEncoding = AudioEncoding.AUDIO_ENCODING_LINEAR_16;
int sampleRateHertz = 16000;
// Instructs the speech recognizer how to process the audio content.
InputAudioConfig inputAudioConfig = InputAudioConfig.newBuilder()
.setAudioEncoding(audioEncoding) // audioEncoding = AudioEncoding.AUDIO_ENCODING_LINEAR_16
.setLanguageCode(languageCode) // languageCode = "en-US"
.setSampleRateHertz(sampleRateHertz) // sampleRateHertz = 16000
.build();
// Build the query with the InputAudioConfig
QueryInput queryInput = QueryInput.newBuilder().setAudioConfig(inputAudioConfig).build();
// Read the bytes from the audio file
byte[] inputAudio = Files.readAllBytes(Paths.get("/home/rmg/Audio/book_a_room.wav"));
byte[] encodedAudio = Base64.encodeBase64(inputAudio);
// Build the DetectIntentRequest
DetectIntentRequest request = DetectIntentRequest.newBuilder()
.setSession("projects/"+projectId+"/agent/sessions/" + sessionId)
.setQueryInput(queryInput)
.setInputAudio(ByteString.copyFrom(encodedAudio))
.build();
// Performs the detect intent request
DetectIntentResponse response = sessionsClient.detectIntent(request);
// Display the query result
QueryResult queryResult = response.getQueryResult();
System.out.println("====================");
System.out.format("Query Text: '%s'\n", queryResult.getQueryText());
System.out.format("Detected Intent: %s (confidence: %f)\n",
queryResult.getIntent().getDisplayName(), queryResult.getIntentDetectionConfidence());
System.out.format("Fulfillment Text: '%s'\n", queryResult.getFulfillmentText());
return response;
}
我尝试了几种格式,wav(PCM 16 位,几种采样率)和 FLAC,并且还按照此处所述(通过代码或控制台)以两种不同的方式将字节转换为 base64:
https://dialogflow.com/docs/reference/text-to-speech
我什至使用本例中提供的 .wav 进行了测试,在我的代理中创建了一个名为 "book a room" 的新意图,其中包含该训练短语。它使用来自 dialogflow 控制台的文本和音频工作,但只适用于文本,而不是来自我的代码的音频......我正在发送他们提供的相同 wav! (上面的代码)
我总是收到相同的响应 (QueryResult):
我需要线索或其他东西,我完全被困在这里了。没有日志,响应中没有错误...但不起作用。
谢谢
我写信给 dialogflow 支持,并用一段有效的代码回复了我。和上面的帖子基本一样,唯一不同的是base64编码,没必要。
所以我删除了:
byte[] encodedAudio = Base64.encodeBase64(inputAudio);
(并直接使用inputAudio)
现在它按预期工作...
我正在尝试将音频文件发送到 dialogflow API 以进行意图检测。我已经有一个代理工作得很好,但只能处理文本。我正在尝试添加音频功能,但没有成功。
我正在使用此页面中提供的示例 (Java):
https://cloud.google.com/dialogflow-enterprise/docs/detect-intent-audio#detect-intent-text-java
这是我的代码:
public DetectIntentResponse detectIntentAudio(String projectId, byte [] bytes, String sessionId,
String languageCode)
throws Exception {
// Set the session name using the sessionId (UUID) and projectID (my-project-id)
SessionName session = SessionName.of(projectId, sessionId);
System.out.println("Session Path: " + session.toString());
// Note: hard coding audioEncoding and sampleRateHertz for simplicity.
// Audio encoding of the audio content sent in the query request.
AudioEncoding audioEncoding = AudioEncoding.AUDIO_ENCODING_LINEAR_16;
int sampleRateHertz = 16000;
// Instructs the speech recognizer how to process the audio content.
InputAudioConfig inputAudioConfig = InputAudioConfig.newBuilder()
.setAudioEncoding(audioEncoding) // audioEncoding = AudioEncoding.AUDIO_ENCODING_LINEAR_16
.setLanguageCode(languageCode) // languageCode = "en-US"
.setSampleRateHertz(sampleRateHertz) // sampleRateHertz = 16000
.build();
// Build the query with the InputAudioConfig
QueryInput queryInput = QueryInput.newBuilder().setAudioConfig(inputAudioConfig).build();
// Read the bytes from the audio file
byte[] inputAudio = Files.readAllBytes(Paths.get("/home/rmg/Audio/book_a_room.wav"));
byte[] encodedAudio = Base64.encodeBase64(inputAudio);
// Build the DetectIntentRequest
DetectIntentRequest request = DetectIntentRequest.newBuilder()
.setSession("projects/"+projectId+"/agent/sessions/" + sessionId)
.setQueryInput(queryInput)
.setInputAudio(ByteString.copyFrom(encodedAudio))
.build();
// Performs the detect intent request
DetectIntentResponse response = sessionsClient.detectIntent(request);
// Display the query result
QueryResult queryResult = response.getQueryResult();
System.out.println("====================");
System.out.format("Query Text: '%s'\n", queryResult.getQueryText());
System.out.format("Detected Intent: %s (confidence: %f)\n",
queryResult.getIntent().getDisplayName(), queryResult.getIntentDetectionConfidence());
System.out.format("Fulfillment Text: '%s'\n", queryResult.getFulfillmentText());
return response;
}
我尝试了几种格式,wav(PCM 16 位,几种采样率)和 FLAC,并且还按照此处所述(通过代码或控制台)以两种不同的方式将字节转换为 base64:
https://dialogflow.com/docs/reference/text-to-speech
我什至使用本例中提供的 .wav 进行了测试,在我的代理中创建了一个名为 "book a room" 的新意图,其中包含该训练短语。它使用来自 dialogflow 控制台的文本和音频工作,但只适用于文本,而不是来自我的代码的音频......我正在发送他们提供的相同 wav! (上面的代码)
我总是收到相同的响应 (QueryResult):
我需要线索或其他东西,我完全被困在这里了。没有日志,响应中没有错误...但不起作用。
谢谢
我写信给 dialogflow 支持,并用一段有效的代码回复了我。和上面的帖子基本一样,唯一不同的是base64编码,没必要。
所以我删除了:
byte[] encodedAudio = Base64.encodeBase64(inputAudio);
(并直接使用inputAudio)
现在它按预期工作...