Audio Dialogflow,来自音频的检测意图
我正在尝试将音频文件发送到dialogflow API以进行意图检测。我已经有一个代理工作得很好,但只处理文本。我正在尝试添加音频功能,但运气不好 我正在使用本页中提供的示例(Java): 这是我的代码:Audio Dialogflow,来自音频的检测意图,audio,android-intent,dialogflow-es,detection,Audio,Android Intent,Dialogflow Es,Detection,我正在尝试将音频文件发送到dialogflow API以进行意图检测。我已经有一个代理工作得很好,但只处理文本。我正在尝试添加音频功能,但运气不好 我正在使用本页中提供的示例(Java): 这是我的代码: public DetectIntentResponse detectIntentAudio(String projectId, byte [] bytes, String sessionId, String
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:
我甚至使用本例中提供的.wav进行了测试,在我的代理中创建了一个名为“预订房间”的新意图,并使用了该培训短语。它可以使用dialogflow控制台中的文本和音频工作,但只能使用文本,而不能使用我的代码中的音频。。。我正在发送他们提供的相同wav!(以上代码)
我总是收到相同的回复(QueryResult):
我需要线索什么的,我完全被困在这里了。没有日志,响应中没有错误。。。但是不起作用
谢谢我写信给dialogflow支持部门,并用一段工作代码回复了我的回复。它基本上与上面发布的相同,唯一的区别是base64编码,没有必要这样做 所以我删除了:
byte[] encodedAudio = Base64.encodeBase64(inputAudio);
(并直接使用inputAudio)
现在它正在按预期工作…Malpica:支持哪种类型的音频文件?您测试的是哪种类型的音频文件?支持的文件是flac和PCM wav 16位(16000 Hz)。我和wav一起工作。