我使用最佳示例的Go示例[live caption_from_file]代码来标识本地韩语语音频率。我将语言代码设置为“ ko-KR”,但在没有任何内容返回或错误提示的情况下,我无法识别stream.Recv()中返回io.EOF的代码。但是,如果我使用本地中文wav音频并将“语言代码”设置为“ cmn-Hans-CN”,则返回正确的识别文本不会有问题。有什么问题以及如何解决?
如果len(flag.Args())<1 { log.Fatal(“请将路径作为命令行参数传递到本地音频文件”) } audioFile:= flag.Arg(0) langEn:= flag.Arg(1)
ctx := context.Background()
client, err := speech.NewClient(ctx)
if err != nil {
log.Fatal(err)
}
stream, err := client.StreamingRecognize(ctx)
if err != nil {
log.Fatal(err)
}
// Send the initial configuration message.
if err := stream.Send(&speechpb.StreamingRecognizeRequest{
StreamingRequest: &speechpb.StreamingRecognizeRequest_StreamingConfig{
StreamingConfig: &speechpb.StreamingRecognitionConfig{
Config: &speechpb.RecognitionConfig{
Encoding: speechpb.RecognitionConfig_LINEAR16,
SampleRateHertz: 16000,
LanguageCode: langEn,
},
},
},
}); err != nil {
log.Fatal(err)
}
f, err := os.Open(audioFile)
if err != nil {
log.Fatal(err)
}
defer f.Close()
go func() {
buf := make([]byte, 1024)
for {
n, err := f.Read(buf)
if n > 0 {
if err := stream.Send(&speechpb.StreamingRecognizeRequest{
StreamingRequest: &speechpb.StreamingRecognizeRequest_AudioContent{
AudioContent: buf[:n],
},
}); err != nil {
log.Printf("Could not send audio: %v", err)
}
}
if err == io.EOF {
// Nothing else to pipe, close the stream.
if err := stream.CloseSend(); err != nil {
log.Fatalf("Could not close stream: %v", err)
}
return
}
if err != nil {
log.Printf("Could not read from %s: %v", audioFile, err)
continue
}
}
}()
for {
resp, err := stream.Recv()
//if err == io.EOF {
// break
//}
if err != nil {
log.Fatalf("Cannot stream results: %v", err)
}
if err := resp.Error; err != nil {
log.Fatalf("Could not recognize: %v", err)
}
for _, result := range resp.Results {
fmt.Printf("Result: %+v\n", result)
for _, alt := range result.Alternatives {
fmt.Printf("\"%v\" (confidence=%3f)\n", alt.Transcript, alt.Confidence)
}
}
}