0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Google ColabでSpeechToTextしたい!

Posted at

インストールして

!pip install --upgrade google-cloud-speech

からの

speechToText.py
from google.cloud import speech_v1p1beta1 as speech
import time

# サービスアカウントファイルを使用して認証する
client = speech.SpeechClient.from_service_account_json('credentials.json')

# 音声ファイルが格納されている実際の GCS バケット URI に置き換えてください
gcs_uri = "gs://test/test.mp3"

audio = speech.RecognitionAudio(uri=gcs_uri)
config = speech.RecognitionConfig(
    encoding=speech.RecognitionConfig.AudioEncoding.MP3,  # ファイルタイプに合わせて更新済み
    language_code="ja-JP",  # 言語を日本語に設定
    sample_rate_hertz=16000  # ファイルのサンプルレートと一致していることを確認
)

# 大きなファイルに対して非同期認識を使用する
operation = client.long_running_recognize(config=config, audio=audio)
print("Waiting for operation to complete...")
while not operation.done():
    # metadata が取得できる場合は進捗を表示(存在しなければ「処理中」と表示)
    metadata = operation.metadata
    if metadata is not None and hasattr(metadata, "progress_percent"):
        print("進捗: {}%".format(metadata.progress_percent))
    else:
        print("処理中...")
    time.sleep(10)
print("認識処理完了!")
response = operation.result()

# 転写結果を表示
for result in response.results:
    print("Transcript: {}".format(result.alternatives[0].transcript))

できた。
あらかじめ音声ファイルは「Cloud Storage」に入れておいてね
あと認証ファイルも必要っぽい(サンプルは使ってなかった・・)

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?