【问题标题】:GCP Speech to text - Java API not workingGCP 语音转文本 - Java API 不起作用
【发布时间】:2019-08-13 09:50:04
【问题描述】:

我在 Chrome 浏览器中使用 MediaRecorder 录制了一个示例 .webm 文件。当我使用 Google 语音 java 客户端获取视频的转录时,它返回空转录。这是我的代码的样子

SpeechSettings settings = null;
Path path = Paths.get("D:\\scrap\\gcp_test.webm");
byte[] content = null;
try {
    content = Files.readAllBytes(path);
    settings = SpeechSettings.newBuilder().setCredentialsProvider(credentialsProvider).build();
} catch (IOException e1) {
    throw new IllegalStateException(e1);
}

try (SpeechClient speech = SpeechClient.create(settings)) {
    // Builds the request for remote FLAC file
    RecognitionConfig config = RecognitionConfig.newBuilder()
                    .setEncoding(AudioEncoding.LINEAR16)
                    .setLanguageCode("en-US")
                    .setUseEnhanced(true)
                    .setModel("video")
                    .setEnableAutomaticPunctuation(true)
                    .setSampleRateHertz(48000)
                    .build();

    RecognitionAudio audio = RecognitionAudio.newBuilder().setContent(ByteString.copyFrom(content)).build();

    // RecognitionAudio audio = RecognitionAudio.newBuilder().setUri("gs://xxxx/gcp_test.webm") .build();

    // Use blocking call for getting audio transcript
    RecognizeResponse response = speech.recognize(config, audio);
    List<SpeechRecognitionResult> results = response.getResultsList();

    for (SpeechRecognitionResult result : results) {
        SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
        System.out.printf("Transcription: %s%n", alternative.getTranscript());
    }
} catch (Exception e) {
    e.printStackTrace();
    System.err.println(e.getMessage());
}

如果,我使用相同的文件并访问https://cloud.google.com/speech-to-text/ 并在演示部分上传文件。它似乎工作正常并显示转录。我对这里出了什么问题一无所知。我验证了演示发送的请求,这里看起来像

我正在发送确切的参数集,但这不起作用。尝试将文件上传到云存储,但结果也相同(没有转录)。

【问题讨论】:

    标签: java google-cloud-platform google-cloud-speech transcription


    【解决方案1】:

    经过错误和试验(并查看 javascript 示例)后,我可以解决问题。音频的序列化版本应为 FLAC 格式。我将视频文件(webm)按原样发送到 Google Cloud。网站上的demo使用Javascript Audio API提取音频流,然后以base64格式发送数据使其工作。

    这是我为获得输出而执行的步骤。

    1. 使用 FFMPEG 从 webm 中提取音频流为 FLAC 格式。

      ffmpeg -i sample.webm -vn -acodec flac sample.flac

    2. 应使用存储云或以字节字符串形式发送提取的文件。

    3. 在调用语音 API 时设置适当的模型(对于英语语言 video 模型有效,而对于法语语言 command_and_search)。我对此没有任何合乎逻辑的理由。我在谷歌云站点上的演示中反复试验后意识到了这一点。

    【讨论】:

      【解决方案2】:

      我得到了使用 flac 编码文件的结果。

      带有时间戳的示例代码结果词,

      public class SpeechToTextSample {
      
      public static void main(String... args) throws Exception {
      
       try (SpeechClient speechClient = SpeechClient.create()) {
      
         String gcsUriFlac = "gs://yourfile.flac";
      
         RecognitionConfig config =
             RecognitionConfig.newBuilder()
                 .setEncoding(AudioEncoding.FLAC)  
                 .setEnableWordTimeOffsets(true)
                 .setLanguageCode("en-US")
                 .build();
      
         RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(gcsUriFlac).build(); //for large files
         OperationFuture<LongRunningRecognizeResponse, LongRunningRecognizeMetadata> response = speechClient.longRunningRecognizeAsync(config, audio);
         while (!response.isDone()) {
                System.out.println("Waiting for response...");
                Thread.sleep(1000);
              }
         // Performs speech recognition on the audio file
      
         List<SpeechRecognitionResult> results = response.get().getResultsList();
      
         for (SpeechRecognitionResult result : results) {
            SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
           System.out.printf("Transcription: %s%n", alternative.getTranscript());
           for (WordInfo wordInfo : alternative.getWordsList()) {
               System.out.println(wordInfo.getWord());
               System.out.printf(
                   "\t%s.%s sec - %s.%s sec\n",
                   wordInfo.getStartTime().getSeconds(),
                   wordInfo.getStartTime().getNanos() / 100000000,
                   wordInfo.getEndTime().getSeconds(),
                   wordInfo.getEndTime().getNanos() / 100000000);
             }
         }
       }
      }
      }
      

      GCP 支持不同的语言,我在示例中使用了“en-US”。 请参考以下链接document了解语言列表。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-29
        • 2018-03-29
        相关资源
        最近更新 更多