|
| 1 | +import numpy as np |
| 2 | +import torch |
| 3 | +from modelscope.pipelines import pipeline |
| 4 | +from modelscope.utils.constant import Tasks |
| 5 | + |
| 6 | +# 初始化 |
| 7 | +sv_pipeline = pipeline( |
| 8 | + task=Tasks.speaker_verification, model="iic/speech_campplus_sv_zh-cn_3dspeaker_16k" |
| 9 | +) |
| 10 | + |
| 11 | +voiceprints = {} |
| 12 | + |
| 13 | + |
| 14 | +def _to_numpy(x): |
| 15 | + return x.cpu().numpy() if torch.is_tensor(x) else np.asarray(x) |
| 16 | + |
| 17 | + |
| 18 | +def register_voiceprint(name, audio_path): |
| 19 | + """登记声纹特征""" |
| 20 | + result = sv_pipeline([audio_path], output_emb=True) |
| 21 | + emb = _to_numpy(result["embs"][0]) # 1 条音频只取第 0 条 |
| 22 | + voiceprints[name] = emb |
| 23 | + print(f"已登记: {name}") |
| 24 | + |
| 25 | + |
| 26 | +def identify_speaker(audio_path): |
| 27 | + """识别声纹所属""" |
| 28 | + test_result = sv_pipeline([audio_path], output_emb=True) |
| 29 | + test_emb = _to_numpy(test_result["embs"][0]) |
| 30 | + |
| 31 | + similarities = {} |
| 32 | + for name, emb in voiceprints.items(): |
| 33 | + cos_sim = np.dot(test_emb, emb) / ( |
| 34 | + np.linalg.norm(test_emb) * np.linalg.norm(emb) |
| 35 | + ) |
| 36 | + similarities[name] = cos_sim |
| 37 | + |
| 38 | + match_name = max(similarities, key=similarities.get) |
| 39 | + return match_name, similarities[match_name], similarities |
| 40 | + |
| 41 | + |
| 42 | +if __name__ == "__main__": |
| 43 | + register_voiceprint("max_output_size", "test//test0.wav") |
| 44 | + register_voiceprint("tts1", "test//test1.wav") |
| 45 | + |
| 46 | + test_file = "test//test2.wav" |
| 47 | + match_name, match_score, all_scores = identify_speaker(test_file) |
| 48 | + |
| 49 | + print(f"\n识别结果: {test_file} 属于 {match_name}") |
| 50 | + print(f"匹配分数: {match_score:.4f}") |
| 51 | + print("\n所有声纹对比分数:") |
| 52 | + for name, score in all_scores.items(): |
| 53 | + print(f"{name}: {score:.4f}") |
0 commit comments