代码拉取完成,页面将自动刷新
import librosa
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Model
layer_name = 'global_max_pooling2d'
model = tf.keras.models.load_model('models/resnet.h5')
intermediate_layer_model = Model(inputs=model.input, outputs=model.get_layer(layer_name).output)
# 读取音频数据
def load_data(data_path):
wav, sr = librosa.load(data_path, sr=16000)
intervals = librosa.effects.split(wav, top_db=20)
wav_output = []
for sliced in intervals:
wav_output.extend(wav[sliced[0]:sliced[1]])
assert len(wav_output) >= 8000, "有效音频小于0.5s"
wav_output = np.array(wav_output)
ps = librosa.feature.melspectrogram(y=wav_output, sr=sr, hop_length=256).astype(np.float32)
ps = ps[np.newaxis, ..., np.newaxis]
return ps
def infer(audio_path):
data = load_data(audio_path)
feature = intermediate_layer_model.predict(data)
return feature
if __name__ == '__main__':
# 要预测的两个人的音频文件
person1 = 'dataset/ST-CMDS-20170001_1-OS/20170001P00001A0001.wav'
person2 = 'dataset/ST-CMDS-20170001_1-OS/20170001P00001A0101.wav'
feature1 = infer(person1)[0]
feature2 = infer(person2)[0]
# 对角余弦值
dist = np.dot(feature1, feature2) / (np.linalg.norm(feature1) * np.linalg.norm(feature2))
if dist > 0.7:
print("%s 和 %s 为同一个人,相似度为:%f" % (person1, person2, dist))
else:
print("%s 和 %s 不是同一个人,相似度为:%f" % (person1, person2, dist))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。