4f60e7ae96
- Analyze why Whisper detected 'nn' instead of Japanese - Document root causes: poor audio quality, high noise, model confusion - Provide diagnostic tool (analyze_whisper_language.py) - Document 4 solutions: force language, audio preprocessing, model upgrade, parameter tuning - Recommend integration into Momentry ASR pipeline
123 lines
3.5 KiB
Python
123 lines
3.5 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Whisper 語言檢測分析工具
|
|
用於診斷語言檢測失敗的原因
|
|
"""
|
|
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def analyze_asr_output(asr_json_path):
|
|
"""分析 ASR 輸出,診斷語言檢測問題"""
|
|
|
|
with open(asr_json_path) as f:
|
|
data = json.load(f)
|
|
|
|
print("=" * 60)
|
|
print("Whisper 語言檢測分析報告")
|
|
print("=" * 60)
|
|
|
|
# 1. 語言檢測
|
|
detected_lang = data.get("language", "unknown")
|
|
print(f"\n檢測到的語言: {detected_lang}")
|
|
|
|
# 2. 分析 segments
|
|
segments = data.get("segments", [])
|
|
if not segments:
|
|
print("\n❌ 沒有找到 segments")
|
|
return
|
|
|
|
print(f"\n總片段數: {len(segments)}")
|
|
|
|
# 3. 質量指標統計
|
|
avg_no_speech_prob = sum(s.get("no_speech_prob", 0) for s in segments) / len(segments)
|
|
avg_logprob = sum(s.get("avg_logprob", 0) for s in segments) / len(segments)
|
|
|
|
print(f"\n音頻質量指標:")
|
|
print(f" 平均靜音概率: {avg_no_speech_prob:.2%}")
|
|
print(f" 平均對數概率: {avg_logprob:.3f}")
|
|
|
|
# 4. 問題診斷
|
|
print(f"\n問題診斷:")
|
|
|
|
if avg_no_speech_prob > 0.5:
|
|
print(" ❌ 靜音概率過高 (>50%)")
|
|
print(" → 音頻可能主要是噪音或背景音樂")
|
|
print(" → 建議: 分離人聲軌道")
|
|
|
|
if avg_logprob < -0.7:
|
|
print(" ❌ 置信度過低 (< -0.7)")
|
|
print(" → Whisper 對輸出非常不確定")
|
|
print(" → 建議: 強制指定語言或更換模型")
|
|
|
|
# 5. 文本分析
|
|
all_text = " ".join(s.get("text", "") for s in segments)
|
|
print(f"\n文本分析:")
|
|
print(f" 總字符數: {len(all_text)}")
|
|
|
|
# 檢測字符類型
|
|
char_types = set()
|
|
for char in all_text:
|
|
if '\u0D80' <= char <= '\u0DFF':
|
|
char_types.add("Sinhala")
|
|
elif '\u3040' <= char <= '\u309F':
|
|
char_types.add("Hiragana")
|
|
elif '\u30A0' <= char <= '\u30FF':
|
|
char_types.add("Katakana")
|
|
elif '\u4E00' <= char <= '\u9FFF':
|
|
char_types.add("CJK")
|
|
|
|
if char_types:
|
|
print(f" 檢測到的字符類型: {', '.join(char_types)}")
|
|
|
|
# 6. 提供解決方案
|
|
print("\n" + "=" * 60)
|
|
print("解決方案")
|
|
print("=" * 60)
|
|
|
|
print("\n1. 強制指定語言 (推薦):")
|
|
print(" whisper audio.mp3 --language ja --task transcribe")
|
|
|
|
print("\n2. 使用更強的模型:")
|
|
print(" whisper audio.mp3 --language ja --model large-v3")
|
|
|
|
print("\n3. 降低溫度參數:")
|
|
print(" whisper audio.mp3 --language ja --temperature 0.0")
|
|
|
|
print("\n4. 分離人聲:")
|
|
print(" pip install demucs")
|
|
print(" demucs --two-stems=vocals audio.mp3")
|
|
print(" whisper vocals.wav --language ja")
|
|
|
|
print("\n5. Python 代碼示例:")
|
|
print("""
|
|
import whisper
|
|
|
|
model = whisper.load_model("large-v3")
|
|
result = model.transcribe(
|
|
"audio.mp3",
|
|
language="ja",
|
|
task="transcribe",
|
|
temperature=0.0,
|
|
condition_on_previous_text=False,
|
|
verbose=True
|
|
)
|
|
print(result["text"])
|
|
""")
|
|
|
|
return {
|
|
"detected_language": detected_lang,
|
|
"avg_no_speech_prob": avg_no_speech_prob,
|
|
"avg_logprob": avg_logprob,
|
|
"quality": "poor" if avg_no_speech_prob > 0.5 or avg_logprob < -0.7 else "good"
|
|
}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) < 2:
|
|
print("用法: python analyze_whisper_language.py <asr.json>")
|
|
sys.exit(1)
|
|
|
|
analyze_asr_output(sys.argv[1]) |