【发布时间】:2021-06-25 07:57:15
【问题描述】:
我想测试语音识别。我检查了 Google 示例代码网站。我已经尝试了几种不同的代码示例,但还没有得到一个有效的。这是最新的。它不返回错误,只是一个空的响应对象。我尝试了不同版本的语音识别,谷歌的样本都没有。下面是我在网上找到的最简单的测试代码。 google 示例站点没有指定使用哪种音频文件,因此这可能是个问题。但是 .wav 文件通常包含一个指定编码、采样率等的标头。我曾使用 .wav 文件测试过 Python 等其他语言的语音识别,但从未出现过问题。我尝试省略可选的 Encoding 和 SampleRateHertz 字段,但像往常一样返回了相同的空响应对象。没有错误或异常,只是一个空响应。
package main
import (
"fmt"
"context"
"io"
"io/ioutil"
"os"
speech "cloud.google.com/go/speech/apiv1"
speechpb "google.golang.org/genproto/googleapis/cloud/speech/v1"
)
func send(w io.Writer, client *speech.Client, filename string) error {
ctx := context.Background()
data, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
// Send the contents of the audio file with the encoding and
// and sample rate information to be transcripted.
req := &speechpb.LongRunningRecognizeRequest{
Config: &speechpb.RecognitionConfig{
Encoding: speechpb.RecognitionConfig_LINEAR16,
SampleRateHertz: 16000,
LanguageCode: "en-US",
},
Audio: &speechpb.RecognitionAudio{
AudioSource: &speechpb.RecognitionAudio_Content{Content: data},
},
}
op, err := client.LongRunningRecognize(ctx, req)
if err != nil {
return err
}
resp, err := op.Wait(ctx)
if err != nil {
return err
}
// Print the results.
fmt.Println(resp,"is response from Google")
for _, result := range resp.Results {
for _, alt := range result.Alternatives {
fmt.Fprintf(w, "\"%v\" (confidence=%3f)\n", alt.Transcript, alt.Confidence)
}
}
return nil
}
func main() {
ctx := context.Background()
var speech_client,err = speech.NewClient(ctx)
if err != nil {
fmt.Println("error creating speech client")
}
send(os.Stdout,speech_client,"hello.wav")
}
【问题讨论】:
标签: go speech-recognition