Google SpeechClient API

时间:2018-11-01 13:12:17

标签: android speech-recognition google-speech-api

我正在尝试使用Google Speech API做一些工作。如何使用Google Speech API Java库指定身份验证密钥? 我正在使用此方法Google Speech API credentials

我想将音频文件转换为文本

谢谢

CredentialsProvider credentialsProvider = FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(new FileInputStream("home/hussain/AndroidStudioProjects/NestedLogics/RouteApplication/service-account.json")));

        SpeechSettings settings = SpeechSettings.newBuilder().setCredentialsProvider(credentialsProvider).build();
        //SpeechClient speechClient = SpeechClient.create(settings);
        SpeechClient speech = SpeechClient.create(settings);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            Path path = Paths.get(file_path);
            byte[] data = Files.readAllBytes(path);
            ByteString audioBytes = ByteString.copyFrom(data);
            RecognitionConfig config = RecognitionConfig.newBuilder()
                    .setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
                    .setSampleRateHertz(16000)
                    .setLanguageCode("en-US")
                    .build();
            RecognitionAudio audio = RecognitionAudio.newBuilder()
                    .setContent(audioBytes)
                    .build();

            RecognizeResponse response = speech.recognize(config,audio);
            List<SpeechRecognitionResult> results = response.getResultsList();
            for (SpeechRecognitionResult result: results)
            {
                SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
                Log.d("HOME", "convertAudioToSpeech: "+alternative.getTranscript());
            }
        }

build.gradle

dependencies {
   implementation fileTree(include: ['*.jar'], dir: 'libs')
   implementation 'com.android.support:appcompat-v7:28.0.0'
   implementation 'com.android.support.constraint:constraint-layout:1.1.3'
   implementation 'com.android.support:support-v4:28.0.0'
   implementation 'com.android.support:design:28.0.0'
   testImplementation 'junit:junit:4.12'
   androidTestImplementation 'com.android.support.test:runner:1.0.2'
   androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
   implementation('com.mapbox.mapboxsdk:mapbox-android-sdk:6.6.1') {
       transitive = true
   }
   implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-locationlayer:0.10.0'
   implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-localization:0.5.0'
   implementation 'com.mapbox.mapboxsdk:mapbox-android-core:0.2.1'
   //firebase
   implementation 'com.google.firebase:firebase-core:16.0.4'
   implementation 'com.google.firebase:firebase-auth:16.0.4'
   implementation 'com.google.firebase:firebase-database:16.0.4'
   implementation 'com.writingminds:FFmpegAndroid:0.3.2'
   implementation 'com.karumi:dexter:4.2.0' // this is use for runtime permissions
   implementation 'com.google.cloud:google-cloud-speech:0.30.0-alpha'
   annotationProcessor 'com.google.cloud:google-cloud-speech:0.30.0-alpha'
   implementation 'com.android.support:multidex:1.0.3'
 }

2 个答案:

答案 0 :(得分:0)

我对Google Cloud SDK经验不足,但是据我了解,您应该:

在Google Cloud Services下创建项目。

创建项目并设置付款方式后(由于此服务不是免费的),您将能够以JSON文件的形式下载私钥。

然后,您需要在计算机上设置环境变量名称GOOGLE_APPLICATION_CREDENTIALS,以指向该文件。 然后,您应该准备出发了。

您可以逐步详细了解如何here

点击蓝色按钮,然后从那里继续...

答案 1 :(得分:0)

generated具有凭据的JSON文件之后,将其放在Android项目源中的 app / src / main / res / raw / credential.json 下。然后在语音服务中创建代码:

InputStream stream = getContext().getResources().openRawResource(R.raw.credential);                   
SpeechSettings settings =
  SpeechSettings.newBuilder().setCredentialsProvider(
    new CredentialsProvider() {
      @Override
      public Credentials getCredentials() throws IOException {
        return GoogleCredentials.fromStream(stream);
      }
    }
  ).build();