如何在网络上使用谷歌语音到文本

Posted

技术标签:

【中文标题】如何在网络上使用谷歌语音到文本【英文标题】:How to use google speech-to-text on web 【发布时间】:2019-06-11 05:44:35 【问题描述】:

我想创建语音转文本网页,例如使用 Google 语音转文本在网络上运行的 google 语音转文本示例。

Google 语音转文本示例:https://cloud.google.com/speech-to-text/?hl=en

我尝试使用 Google 提供的示例代码。 这段代码在localhost上运行良好,但是在ibm云中出现如下错误:

无线路匹配接口 TargetDataLine 支持格式 PCM_SIGNED 16000.0 Hz,16 位,单声道,2 字节/帧,支持 little-endian。

我不知道如何解决这个错误。

ResponseObserver<StreamingRecognizeResponse> responseObserver = null;

  try (SpeechClient client = SpeechClient.create()) 

    responseObserver =
        new ResponseObserver<StreamingRecognizeResponse>() 
          ArrayList<StreamingRecognizeResponse> responses = new ArrayList<>();

          public void onStart(StreamController controller) 

          public void onResponse(StreamingRecognizeResponse response) 
            responses.add(response);
          

          public void onComplete() 
            for (StreamingRecognizeResponse response : responses) 
              StreamingRecognitionResult result = response.getResultsList().get(0);
              SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
              System.out.printf("Transcript : %s\n", alternative.getTranscript());
            
          

          public void onError(Throwable t) 
            System.out.println(t);
          
        ;

    ClientStream<StreamingRecognizeRequest> clientStream =
        client.streamingRecognizeCallable().splitCall(responseObserver);

    RecognitionConfig recognitionConfig =
        RecognitionConfig.newBuilder()
            .setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
            .setLanguageCode("en-US")
            .setSampleRateHertz(16000)
            .build();
    StreamingRecognitionConfig streamingRecognitionConfig =
        StreamingRecognitionConfig.newBuilder().setConfig(recognitionConfig).build();

    StreamingRecognizeRequest request =
        StreamingRecognizeRequest.newBuilder()
            .setStreamingConfig(streamingRecognitionConfig)
            .build(); // The first request in a streaming call has to be a config

    clientStream.send(request);
    // SampleRate:16000Hz, SampleSizeInBits: 16, Number of channels: 1, Signed: true,
    // bigEndian: false
    AudioFormat audioFormat = new AudioFormat(16000, 16, 1, true, false);
    DataLine.Info targetInfo =
        new Info(
            TargetDataLine.class,
            audioFormat); // Set the system information to read from the microphone audio stream

    if (!Audiosystem.isLineSupported(targetInfo)) 
      System.out.println("Microphone not supported");
      System.exit(0);
    
    // Target data line captures the audio stream the microphone produces.
    TargetDataLine targetDataLine = (TargetDataLine) AudioSystem.getLine(targetInfo);
    targetDataLine.open(audioFormat);
    targetDataLine.start();
    System.out.println("Start speaking");
    long startTime = System.currentTimeMillis();
    // Audio Input Stream
    AudioInputStream audio = new AudioInputStream(targetDataLine);
    while (true) 
      long estimatedTime = System.currentTimeMillis() - startTime;
      byte[] data = new byte[6400];
      audio.read(data);
      if (estimatedTime > 60000)  // 60 seconds
        System.out.println("Stop speaking.");
        targetDataLine.stop();
        targetDataLine.close();
        break;
      
      request =
          StreamingRecognizeRequest.newBuilder()
              .setAudioContent(ByteString.copyFrom(data))
              .build();
      clientStream.send(request);
    
   catch (Exception e) 
    System.out.println(e);
  
  responseObserver.onComplete();

【问题讨论】:

【参考方案1】:

您是否已将所有需要的访问密钥导入系统环境? 谷歌不允许在没有 API 密钥的情况下调用他们的 API。 您可以在此处找到如何将 API 添加到环境中 https://cloud.google.com/docs/authentication/api-keys

【讨论】:

以上是关于如何在网络上使用谷歌语音到文本的主要内容,如果未能解决你的问题,请参考以下文章

如何在已部署的 WPF 应用程序中使用谷歌语音文本 api 密钥?

如何细化神经网络模型?谷歌开源MorphNet

如何从您的网站搜索谷歌语音识别?

如何将元数据谷歌语音传递给文本 api - swift ios

iOS : 如何在谷歌地图上启用音频方向

如何使用语音打开 Android TV 应用