尝试运行Google语音识别Java示例时,出现“ querys.json的所需路径”错误

时间:2019-04-26 18:32:02

标签: java json google-bigquery google-speech-api

我正在尝试在此处运行Java流语音识别示例:https://cloud.google.com/speech-to-text/docs/streaming-recognize#speech-streaming-mic-recognize-java

我在Eclipse中创建了一个新的gradle项目,将compile 'com.google.cloud:google-cloud-speech:1.1.0'compile 'com.google.cloud:google-cloud-bigquery:1.70.0'添加到依赖项中,然后在示例代码中将其从链接复制到主类。我可以看到示例脚本中没有使用第二个依赖项,但在那里需要它,否则会出现如下错误:Error: Could not find or load main class com.google.cloud.bigquery.benchmark.Benchmark

运行时添加了两个依赖项,我立即得到标题(need path to queries.json)中的错误,并且该应用程序退出。什么是querys.json文件,我如何为应用程序提供一个使其运行示例项目的路径? google API在我的系统上设置了正确的环境变量,并且API调用被配置为允许使用我正在使用的计算机的IP。

这是整个类脚本(仅项目中的脚本):

import com.google.api.gax.rpc.ClientStream;
import com.google.api.gax.rpc.ResponseObserver; 
import com.google.api.gax.rpc.StreamController;
import com.google.cloud.speech.v1.RecognitionAudio;
import com.google.cloud.speech.v1.RecognitionConfig;
import com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding;
import com.google.cloud.speech.v1.RecognizeResponse;
import com.google.cloud.speech.v1.SpeechClient;
import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
import com.google.cloud.speech.v1.SpeechRecognitionResult;
import com.google.cloud.speech.v1.StreamingRecognitionConfig;
import com.google.cloud.speech.v1.StreamingRecognitionResult;
import com.google.cloud.speech.v1.StreamingRecognizeRequest;
import com.google.cloud.speech.v1.StreamingRecognizeResponse; 
import com.google.protobuf.ByteString;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import javax.sound.sampled.AudioFormat; 
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.DataLine.Info; 
import javax.sound.sampled.TargetDataLine;


public class GoogleSpeechRecognition {
    /** Performs microphone streaming speech recognition with a duration of 1 minute. */
    public static void streamingMicRecognize() throws Exception {

      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 个答案:

答案 0 :(得分:1)

好,所以对我来说,这很简单,是我的错。默认运行配置错误地设置为google类之一,而不是您所称的包含示例代码的测试类。这是导致bigquery错误和querys.json错误的原因。只需使用示例代码将主类更正为测试类即可,它可以工作。另外,您根本不需要在gradle依赖项中包含compile 'com.google.cloud:google-cloud-bigquery:1.70.0',抱怨需要它的错误是由于运行配置中的主类设置不正确引起的。

Example output