Using gcloud speech api for real-time speech recognition in dart, flutter

给你一囗甜甜゛ 提交于 2020-05-13 07:59:05

问题


I want to use Google's real-time speech recognition api in a flutter project, written in dart. I've activated a gcloud account, created the api key (which should be the only necessary authentication method for google speech) and written a basic apk which ought to send an audio stream to Google cloud and display the response. I imported the googleapis/speech and googleapis_auth plugin.

But I couldn't figure out how to set it up. They say you have to use gRPC, which makes sense as it should make it easy to use, but the implementation of their plugin on github doesn't seem to use it.

So can anyone tell me how to use it - setting up authentication and transcribing a speech?


回答1:


Update:

Here's a working sample:

https://gist.github.com/DazWilkin/34d628b998b4266be818ffb3efd688aa

You need only plug the values of a service account key.json and should receive:

{
    alternatives: [{
        confidence: 0.9835046,
        transcript: how old is the Brooklyn Bridge
    }]
}

It is poorly documented :-(

I'm familiar with Google API development but unfamiliar with Dart and with the Google Speech-to-Text API so, apologies in advance.

See: https://github.com/dart-lang/googleapis/tree/master/generated/googleapis

There are 2 flavors of Google SDK|library, the more common (API Client Libraries) and the new (Cloud [!] Client Libraries). IIUC, for Dart for Speech you're going to use the API Client Library and this doesn't use gRPC.

I'm going to tweak the sample by gut, so bear with me:

import 'package:googleapis/speech/v1.dart';
import 'package:googleapis_auth/auth_io.dart';

final _credentials = new ServiceAccountCredentials.fromJson(r'''
{
  "private_key_id": ...,
  "private_key": ...,
  "client_email": ...,
  "client_id": ...,
  "type": "service_account"
}
''');

const _SCOPES = const [SpeechApi.CloudPlatformScope];

void main() {
  clientViaServiceAccount(_credentials, _SCOPES).then((http_client) {
    var speech = new SpeechApi(http_client);
    speech...
  });
}

This requires the creation of a service account with appropriate permissions and a (JSON) key generated for it. Generally, the key file is loaded by the code but, in this example, it's provided as a string literal. The key will provide the content for fromJson. You ought (!) to be able to use Application Default Credentials for testing (easier) see the link below.

Somehow (!) the Dart API will include a method|function that makes this underlying REST call. The call expects some configuration and the audio:

https://cloud.google.com/speech-to-text/docs/reference/rest/v1/speech/recognize

I suspect it's this recognize and it expects a RecognizeRequest

Sorry I can't be of more help.

If you do get it working, please consider publishing the same so others may benefit.

NB

  • https://developers.google.com/identity/protocols/googlescopes#speechv1
  • https://pub.dartlang.org/packages/googleapis_auth_default_credentials


来源:https://stackoverflow.com/questions/55493003/using-gcloud-speech-api-for-real-time-speech-recognition-in-dart-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!