我正在使用Dialogflow v2客户端库进行c#发送流检测意图请求。我正在努力阅读响应流。
使用以下代码,我得到以下响应:
IAsyncEnumerator<StreamingDetectIntentResponse> responseStream = duplexStream.ResponseStream;
while (await responseStream.MoveNext())
{
StreamingDetectIntentResponse response = responseStream.Current;
Console.WriteLine(response);
}
//Response
...
{ "recognitionResult": { "messageType": "TRANSCRIPT", "transcript": "sometime next week" } }
{ "recognitionResult": { "messageType": "END_OF_SINGLE_UTTERANCE" } }
{ "recognitionResult": { "messageType": "TRANSCRIPT", "transcript": "sometime next week", "isFinal": true, "confidence": 0.888456166 } }
Exception thrown: 'Grpc.Core.RpcException' in mscorlib.dll
{ "responseId": "...", "queryResult": {...}}//truncated
我认为上述错误是由于一旦Single Utterance为true我没有编写完整的异步代码。我尝试使用以下代码解决此问题,并收到以下错误:
IAsyncEnumerator<StreamingDetectIntentResponse> responseStream = duplexStream.ResponseStream;
while (await responseStream.MoveNext())
{
StreamingDetectIntentResponse response = responseStream.Current;
Console.WriteLine(response);
if (response.RecognitionResult.MessageType == StreamingRecognitionResult.Types.MessageType.EndOfSingleUtterance)
{
await duplexStream.WriteCompleteAsync();
done = true;
Console.WriteLine("A single utterance has been spoken");
}
}
//Response
...
{ "recognitionResult": { "messageType": "TRANSCRIPT", "transcript": "sometime next week" } }
{ "recognitionResult": { "messageType": "END_OF_SINGLE_UTTERANCE" } }
A single utterance has been spoken
Exception thrown: 'System.InvalidOperationException' in Google.Api.Gax.Grpc.dll
{ "recognitionResult": { "messageType": "TRANSCRIPT", "transcript": "sometime next week", "isFinal": true, "confidence": 0.892199934 } }
{ "responseId": "...", "queryResult": {...}}//truncated
Exception thrown: 'System.NullReferenceException' in myconsoleapp.exe
Exception thrown: 'System.InvalidOperationException' in Google.Api.Gax.Grpc.dll
的 例外为Cannot call WriteAsync after WriteCompleteAsync
'System.NullReferenceException'
的 例外为Reference not set to an instance of an object at line: if (response.RecognitionResult.MessageType == StreamingRecognitionResult.Types.MessageType.EndOfSingleUtterance)
所有这些都在复制我的实际应用程序的控制台应用程序中,我希望解决这些问题将使我能够直接修复我的实际应用程序。其余代码为:
public async Task StreamingDetectIntent()
{
bool done = false;
SessionsClient sessionsClient = SessionsClient.Create();
SessionsClient.StreamingDetectIntentStream duplexStream = sessionsClient.StreamingDetectIntent();
Task.Run(async () =>
{
//See above snippets for the code in this section. This section appears to be causing the errors
});
while (!done)
{
StreamingDetectIntentRequest request = new StreamingDetectIntentRequest
{
Session = new SessionName("telebot-pianomoves-v1", "iq3464rfdw").ToString(),
SingleUtterance = true,
QueryInput = new QueryInput() {
AudioConfig = new InputAudioConfig()
{
AudioEncoding = Google.Cloud.Dialogflow.V2.AudioEncoding.Linear16,
SampleRateHertz = 16000,
LanguageCode = "en-US",
},
},
};
await duplexStream.WriteAsync(request);
byte[] bytes = System.IO.File.ReadAllBytes(@"C:\Users\hjstu\Desktop\Telebot\shortrecording.wav");
Array.Copy(bytes, 44, bytes, 0, bytes.Length - 44);
foreach (var batch in AsBatches(bytes, 320))
{
await duplexStream.WriteAsync(new StreamingDetectIntentRequest()
{
InputAudio = Google.Protobuf.ByteString.CopyFrom(batch)
});
System.Threading.Thread.Sleep(17);
}
}
}
public static IEnumerable<T[]> AsBatches<T>(T[] input, int n)
{
for (int i = 0, r = input.Length; r >= n; r -= n, i += n)
{
var result = new T[n];
Array.Copy(input, i, result, 0, n);
yield return result;
}
}