1

我正在使用 node.js 客户端库,特别是 startRecognition 方法,并使用 Speech.operation 来获得结果。但是,我得到以下结果,而不是结果 - 查看包含一些编码字符串而不是 JavaScript 对象的“值”对象。

{ result: 'response',
  name: '3939860839213416358',
  metadata: 
   { typeUrl: 'type.googleapis.com/google.cloud.speech.v1.LongRunningRecognizeMetadata',
     value: 'CGQSCwiKx47IBRCg6pRuGgsIqM6OyAUQgO+vYQ==' },
  done: true,
  error: null,
  response: 
   { typeUrl: 'type.googleapis.com/google.cloud.speech.v1.LongRunningRecognizeResponse',
     value: 'EoQaCtICCsoCVGhleSBzbWFsbCBsYXRlc3QgZW52aXJvbm1lbnQuIFdlIGhhdmUgYSBjb3Jwb3JhdGUgYnVzaW5lc3Mgc2VydmljZSBhbmQgdGhlcmVmb3Jl' } }

有人见过这个吗?这是一个错误吗?或者有没有办法将其解码为 JavaScript 对象?

这是一个演示问题的代码片段:

var Speech = require('@google-cloud/speech')({
  projectId: 'my project name',
  keyFilename: '<key file name>.json'
});

var opName='';

var config = {
  encoding: 'LINEAR16',
  sampleRateHertz: 48000,
  languageCode: 'en-US',
  maxAlternatives: 10
};

asyncGoogleASR('gs://file-location',config);

function asyncGoogleASR(googleCloudURI,request) {
    Speech.startRecognition(googleCloudURI, request,async_callback);
}
function async_callback(err, operation, apiResponse) {
  if (err) {
    console.log(err);
  }
  opName=operation.latestResponse.name;

   operation
    .on('error', function(err) {
        console.log("error");
        console.log(err);
    })
    .on('complete', function(results) {
        console.log(results);  // this works okay
        var op = Speech.operation(opName);
           op
            .on('error', function(err) {
                console.log("error");
                console.log(err);
            })
            .on('complete', function(results) {
                console.log(results);  // this prints garbage
            });
   });


 }
4

1 回答 1

1

LongRunningOperation 从第一个请求返回,但您必须在操作完成后检索结果。换句话说,speech.startRecognize将返回您轮询的“操作”标识符,直到操作完成并稍后用于检索结果。

以下代码经过测试可以正常工作,可能有助于您入门:

const Speech = require('@google-cloud/speech');
const speech = Speech();
const request = {
  encoding: encoding,
  sampleRateHertz: sampleRateHertz,
  languageCode: languageCode
};

speech.startRecognition(gcsUri, request)
  .then((results) => {
    const operation = results[0];
    return operation.promise();
  })
  .then((results) => {
    const transcription = results[0];
    console.log(`Transcription: ${transcription}`);
  })
  .catch((err) => {
    console.error('ERROR:', err);
  });

请注意,此代码不是使用speech.recognize,而是使用speech.startRecognize(如果它们存储在 Google Cloud Storage 中,则允许您使用大文件)。

要查看它的工作原理,请尝试:

node recognize.js async-gcs gs://gcs-test-data/vr.flac -e FLAC -r 16000`

Github 项目所示。

于 2017-05-05T19:34:50.723 回答