Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 377
anarchy Avatar asked Apr 03 '19 10:04

anarchy


People also ask

How to use speech recognition in flutter with Dart?

A flutter plugin to use the speech recognition iOS10+ / Android 4.1+ Depend on it Add this to your package's pubspec.yaml file: Install it You can install packages from the command line: Import it Now in your Dart code, you can use: On iOS, by default the plugin is configured for French, English, Russian, Spanish, Italian.

Why should I create a Google Cloud speech-to-text account?

If you're new to Google Cloud, create an account to evaluate how Speech-to-Text performs in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.

What languages does the flutter plugin work in?

On iOS, by default the plugin is configured for French, English, Russian, Spanish, Italian. On Android, without additional installations, it will probably works only with the default device locale. If you get a MissingPluginException, try to flutter build apk on Android, or flutter build ios

Can you add offline voice commands to your Flutter App?

Picovoice recently released a series of Flutter packages that have made adding offline voice commands to your mobile app a walk in the park. No, seriously — you’ll have time to take a walk in the park after doing it. For this tutorial, I’ve taken some inspiration from last year’s ubiquitous #flutterclock challenge and created a simple clock app.


2 Answers

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
like image 110
DazWilkin Avatar answered Oct 08 '22 09:10

DazWilkin


For all who are still interested in the topic. I have released a Flutter package that supports Google's Speech-to-Text Api via grpc. This also allows the use of streamingRecognize.

You can find it here: https://pub.dev/packages/google_speech

like image 22
Felix Junghans Avatar answered Oct 08 '22 09:10

Felix Junghans