4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Google Cloud Speech APIを試す Part2(Goで書いてみる)

Posted at

Part2

Google Cloud Speech APIを試す
の続きでGoで書いてみる。

必要なライブラリ

$ go get golang.org/x/net/context
$ go get golang.org/x/oauth2/google
$ go get google.golang.org/api/speech/v1beta1

Goで書いてみる

・Clientを生成

ctx := context.Background()

b, err := ioutil.ReadFile("<your service account keyfile>")
if err != nil {
	log.Fatalf("Unable to read service account keyfile: %v", err)
}
config, err := google.JWTConfigFromJSON(b, speech.CloudPlatformScope)
if err != nil {
	log.Fatalf("Unable to read service account keyfile: %v", err)
}
client := config.Client(ctx)
  • ServiceをNew
srv, err := speech.New(client)
if err != nil {
	log.Fatal(err)
}
  • SyncRecognizeRequestをCallout(Do)
// for Syncrecognize
res, err := srv.Speech.Syncrecognize(&speech.SyncRecognizeRequest{
	Config: &speech.RecognitionConfig{
		Encoding:     "FLAC",
		SampleRate:   16000,
		LanguageCode: "ja-JP",
	},
	Audio: &speech.RecognitionAudio{
		Uri: "gs://<your bucket name>/demo/sample.flac",
	},
}).Do()
if err != nil {
	log.Fatal(err)
}

Configの内容は、以前の内容:Google Cloud Speech APIを試すと同じです。

サンプルソース

試した結果については、GitHubに置いてあります。

参考

GitHub - Speech API/v1bata1

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?