ステップ4:リアルタイム処理(ストリーミング)

◆ ステップ4:リアルタイム処理(ストリーミング)◆

ステップ4「音声入力からリアルタイムSTTを試す」に進みます。

リアルタイムSTTの実装には以下の手順を含めます:

必要な要件:

✅ PCのマイクからリアルタイム音声を取得
✅ Azure Speech-to-Text APIを使ってテキスト化
✅ Pythonスクリプトの実装
✅ 実行結果の確認

実装の流れ:

  1. マイクから音声を取得pyaudio もしくは sounddevice を利用)
  2. Azure Speech-to-Text API に送信し、音声をテキスト化
  3. リアルタイム処理ができるようにストリーミング処理を組み込む
  4. テキスト化された内容を表示

リアルタイムSTTのPythonスクリプトを作成します。

このスクリプトでは、マイクからリアルタイムで音声を取得し、Azure Speech-to-Text API を使用してテキスト化します。

🔹ポイント:

  • start_continuous_recognition() を使用して、継続的に音声認識を行う
  • 認識結果をコンソールにリアルタイムで表示
  • session_stopped または canceled イベントで認識を終了

このコードを試し、認識の精度や遅延などを確認してください。

# Filename: realtime_speech_to_text.py
import azure.cognitiveservices.speech as speechsdk
import time
import winreg
import sys


def get_registry_value(key_path, value_name, hive=winreg.HKEY_LOCAL_MACHINE):
    try:
        with winreg.OpenKey(hive, key_path, 0, winreg.KEY_READ) as key:
            value, _ = winreg.QueryValueEx(key, value_name)
            return value
    except FileNotFoundError:
        print(f"⚠️ レジストリキーが見つかりません: {key_path}\\{value_name}")
        return None


REGISTRY_PATH = r"SOFTWARE\SpeechService"
SPEECH_SERVICE_APIKEY1 = get_registry_value(REGISTRY_PATH, "SPEECH_SERVICE_APIKEY1")
SPEECH_SERVICE_REGION = get_registry_value(REGISTRY_PATH, "SPEECH_SERVICE_REGION")


def recognize_from_microphone():
    if not SPEECH_SERVICE_APIKEY1 or not SPEECH_SERVICE_REGION:
        print("APIキーまたはリージョンが取得できません。")
        return

    speech_config = speechsdk.SpeechConfig(subscription=SPEECH_SERVICE_APIKEY1, region=SPEECH_SERVICE_REGION)
    speech_config.speech_recognition_language = "ja-JP"  # 日本語音声認識を設定

    speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)

    print("リアルタイム音声認識を開始します。マイクに向かって話してください。")

    def recognized_cb(evt):
        text = evt.result.text
        print("認識結果: {}".format(text))
        if "プログラム終了" in text:
            print("プログラム終了コマンドを検出しました。セッションを終了します。")
            speech_recognizer.stop_continuous_recognition()

    def stop_cb(evt):
        print("認識終了: {}".format(evt))
        print("プログラムを終了してください。")
        nonlocal done
        done = True
        sys.exit()  # プログラムを終了

    done = False
    speech_recognizer.recognized.connect(recognized_cb)
    speech_recognizer.session_stopped.connect(stop_cb)
    speech_recognizer.canceled.connect(stop_cb)

    speech_recognizer.start_continuous_recognition()

    while not done:
        time.sleep(0.5)

    speech_recognizer.stop_continuous_recognition()


if __name__ == "__main__":
    recognize_from_microphone()

Azureを使用した音声認識と文字化と翻訳について確認できた。

 >>> GITHUB:音声認識・翻訳学習の演習サンプル