GoogleCloudPlatform / android-docs-samples

Apache License 2.0
373 stars 594 forks source link

A question about longRunningRecognize with SpeechGrpc #78

Open eliaho opened 6 years ago

eliaho commented 6 years ago

I'm trying to perform longRunningRecognize which uses the SpeechGrpc api, the code is running without exceptions and after about a minute i get a response but the alternatives that i get in the result are empty, tried with a few files, i will be glad to understand what I'm doing wrong?

the code :

private SpeechGrpc.SpeechStub mApi; private OperationsGrpc.OperationsStub operationApi;

public void recognizeLongGcs(String gcsUri) throws Exception {
try {
mApi.longRunningRecognize(LongRunningRecognizeRequest.newBuilder() .setConfig( RecognitionConfig.newBuilder() .setEncoding(RecognitionConfig.AudioEncoding.FLAC) .setLanguageCode("iw") .setSampleRateHertz(16000) .build()) .setAudio( RecognitionAudio.newBuilder().setUri(gcsUri).build()) .build(), mLongFileResponseObserver);
} catch (Exception e) { AnalyticsModel.getInstance().logError(this.getClass().getSimpleName() , e ); } }

private final StreamObserver mLongFileResponseObserver = new StreamObserver() { Operation response; String responseName; @Override public void onNext(Operation response) { String text = null; List words = null;
responseName =response.getName(); try { if (response.getDone()) { Any a = response.getResponse(); LongRunningRecognizeResponse longRunningRecognizeResponse = LongRunningRecognizeResponse.parseFrom(a.getValue()); final SpeechRecognitionResult result = longRunningRecognizeResponse.getResults(0); if (result.getAlternativesCount() > 0) { final SpeechRecognitionAlternative alternative = result.getAlternatives(0); text = alternative.getTranscript(); words = alternative.getWordsList(); } } else { Thread.sleep(10000); operationApi.getOperation(GetOperationRequest.newBuilder().setName(responseName).build(), mLongFileResponseObserver); } } catch (Exception e) { } }

@Override
public void onError(Throwable t) {            
    Log.e(TAG, "Error calling the API.", t);
}

@Override
public void onCompleted() {
    Log.i(TAG, "API completed.");            
}

};