1/infer_contrast.py

53 lines
2.3 KiB
Python
Raw Normal View History

2025-04-18 19:56:58 +08:00
import argparse
import functools
import itertools
from mvector.predict import MVectorPredictor
from mvector.utils.utils import add_arguments, print_arguments
from mvector.data_utils.audio import AudioSegment
parser = argparse.ArgumentParser(description=__doc__)
add_arg = functools.partial(add_arguments, argparser=parser)
add_arg('configs', str, 'configs/ecapa_tdnn.yml', '配置文件')
add_arg('use_gpu', bool, True, '是否使用GPU预测')
add_arg('audio_path1', str, 'dataset/source/王翔/wx1_5.wav', '预测第一个音频')
add_arg('audio_path2', str, 'dataset/source/刘云杰/lyj_no_5.wav', '预测第二个音频')
add_arg('threshold', float, 0.7, '判断是否为同一个人的阈值')
add_arg('model_path', str, 'models/test_model', '导出的预测模型文件路径')
args = parser.parse_args()
# print_arguments(args=args)
# 获取识别器
predictor = MVectorPredictor(configs=args.configs,
model_path=args.model_path,
use_gpu=args.use_gpu)
def load_audio_paths(file_path):
with open(file_path, 'r',encoding='utf-8') as file:
return [line.strip() for line in file if line.strip()]
def compare_audio_files(audio_paths, threshold):
# itertools.combinations 生成所有可能的两两组合,无重复
for audio1, audio2 in itertools.combinations(audio_paths, 2):
dist = predictor.contrast(audio1, audio2)
if dist > threshold:
print(f"{audio1}{audio2} 为同一个人,相似度为:{dist}")
# else:
# print(f"{audio1} 和 {audio2} 不是同一个人,相似度为:{dist}")
file_path = 'dataset/ces.txt' # 假设音频路径存储在此文件中
# 执行比对
audio_paths = load_audio_paths(file_path)
compare_audio_files(audio_paths, args.threshold)
# # AudioSegment.silent_semoval(args.audio_path1, args.audio_path1)
# # AudioSegment.silent_semoval(args.audio_path2, args.audio_path2)
# dist = predictor.contrast(args.audio_path1, args.audio_path2)
# if dist > args.threshold:
# print(f"{args.audio_path1} 和 {args.audio_path2} 为同一个人,相似度为:{dist}")
# else:
# print(f"{args.audio_path1} 和 {args.audio_path2} 不是同一个人,相似度为:{dist}")