Misty-Sprach-Coach/livemodus/whisper_live_check.py
2026-05-06 12:46:05 +00:00

58 lines
1.8 KiB
Python

import whisper
import numpy as np
import subprocess
import sys
from config import RTSP_URL
def run_live_stream():
print(f"--- Starte Live-Transkription ---")
print(f"Verbindung zu: {RTSP_URL}")
# 1. Whisper Modell laden (base ist schnell genug für Live)
print("Lade KI-Modell...")
model = whisper.load_model("base")
# 2. FFmpeg Befehl für den Live-Stream
# Wir ziehen Audio direkt von Misty und wandeln es in das Whisper-Format
command = [
'ffmpeg',
'-rtsp_transport', 'tcp', # TCP ist stabiler für den Roboter
'-i', RTSP_URL,
'-ar', '16000', # Whisper braucht 16kHz
'-ac', '1', # Mono
'-f', 's16le', # Raw PCM Format
'-' # Ausgabe an Pipe (stdout)
]
# Startet den FFmpeg-Prozess im Hintergrund
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
print("\n[LIVE] Höre zu... (Strg+C zum Beenden)\n")
try:
while True:
# Wir lesen einen 5-Sekunden Block für die Analyse
# 16000 Samples/s * 2 Bytes/Sample * 5s = 160000 Bytes
raw_audio = process.stdout.read(160000)
if not raw_audio:
break
# In Numpy-Array umwandeln und normalisieren
audio_np = np.frombuffer(raw_audio, dtype=np.int16).astype(np.float32) / 32768.0
# Transkription durchführen
result = model.transcribe(audio_np, fp16=False, language="de")
# Text ausgeben
text = result['text'].strip()
if text:
print(f"Erkannt: {text}")
except KeyboardInterrupt:
print("\nStoppe Live-Stream...")
finally:
process.terminate()
if __name__ == "__main__":
run_live_stream()