【问题标题】:Why does this Google speech code return an empty object?为什么这个 Google 语音代码返回一个空对象?
【发布时间】: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


    【解决方案1】:

    我以这种方式工作,如下所示。不要指定可选的 SampleRateHertz - 将从 *.wav 文件的标题中读取正确的值。注意:必须将我的立体声文件转换为单声道。你可以用 Audacity 做到这一点。

    package main
    
    import (
        "fmt"
        "io/ioutil"
        "golang.org/x/net/context"
        speech "cloud.google.com/go/speech/apiv1"
        speechpb "google.golang.org/genproto/googleapis/cloud/speech/v1"
    )
    
    func main () {
    
        // Once you are all set up, you need to start a new Context. A context is essentially an object that allows for more effective interaction with 3rd party APIs, by having individual handlers for when an API has completed a request, failed, or taken too long. Because we are going to be making requests to the Google Speech API, it is important to have handlers for these scenarios.
        
        ctx := context.Background() 
        
        client, err := speech.NewClient(ctx)
        if(err != nil){ 
            fmt.Println(err)
        }
        
        // The first line starts a new empty context, named ctx. This then gets passed into the NewClient method, which will use your authentication setup from earlier to confirm your specific account. The next block just catches any error that may be returned from the Speech API. If you do get an error, you may need to retry configuring your authentication keys. We now need to specify a sound file to use, and process it into an Audio data object:
        
        fileDir := "hello.wav"//;
        
        audioData, err := ioutil.ReadFile(fileDir)//;
        if(err != nil){
             fmt.Println(err)
        }
        
        // The module used is ioutil, and is responsible for formatting audio data into a kind of buffer, ready to be sent to the API. Next, we need to provide certain presets as a speechpb.RecognitionConfig object which is passed through as a parameter to the main method client.Recognize (which actually carries out the request):
        
            //SampleRateHertz: 22050,
        response, err := client.Recognize(ctx, &speechpb.RecognizeRequest{
            Config: &speechpb.RecognitionConfig{
            Encoding: speechpb.RecognitionConfig_LINEAR16,
            LanguageCode: "en-US",
        },
            Audio: &speechpb.RecognitionAudio{
            AudioSource: &speechpb.RecognitionAudio_Content{Content: audioData},
        },
        })
        
        // Most of the presets are self-explanatory, and easy to experiment with. The sample rate is essentially the quality of data, where the highest value allowed is 16000. The encoding field informs the program how to represent the data, where LINEAR16 refers to uncompressed 16-bit signed samples.
        
        if(err != nil){
             fmt.Println(err)
        }
        
        // Again, we check and output any errors from the response. Finally, we can output the transcript:
        
        if response != nil {
            fmt.Println("response is", response)
            for _, result := range response.Results{
                for _, alt := range result.Alternatives{
                     fmt.Println(alt.Transcript)
            
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-25
      相关资源
      最近更新 更多