首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Resemblyzer实现电话对话中的说话人数字化

用Resemblyzer实现电话对话中的说话人数字化
EN

Stack Overflow用户
提问于 2021-06-16 14:43:14
回答 1查看 470关注 0票数 0

我有电话对话的录音,我用Resemblyzer它集群音频基于扬声器。输出是labelling,它基本上是一本字典,它是当人发言时(speaker_label,start_time,end_time)的字典。

我需要分段录音发言者明智的时间在标签。我已经为此做了一周了。

代码语言:javascript
复制
from resemblyzer import preprocess_wav, VoiceEncoder
from pathlib import Path
import pickle
import scipy.io.wavfile
from spectralcluster import SpectralClusterer

audio_file_path = 'C:/Users/...'
wav_fpath = Path(audio_file_path)

wav = preprocess_wav(wav_fpath)
encoder = VoiceEncoder("cpu")
_, cont_embeds, wav_splits = encoder.embed_utterance(wav, return_partials=True, rate=16)
print(cont_embeds.shape)            

clusterer = SpectralClusterer(
    min_clusters=2,
    max_clusters=100,
    p_percentile=0.90,
    gaussian_blur_sigma=1)

labels = clusterer.predict(cont_embeds)

def create_labelling(labels, wav_splits):
    from resemblyzer.audio import sampling_rate
    times = [((s.start + s.stop) / 2) / sampling_rate for s in wav_splits]
    labelling = []
    start_time = 0

    for i, time in enumerate(times):
        if i > 0 and labels[i] != labels[i - 1]:
            temp = [str(labels[i - 1]), start_time, time]
            labelling.append(tuple(temp))
            start_time = time
        if i == len(times) - 1:
            temp = [str(labels[i]), start_time, time]
            labelling.append(tuple(temp))

    return labelling

labelling = create_labelling(labels, wav_splits)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-22 04:49:08

这段代码非常有用:首先添加一个包含时间戳的time_stamps.txt文件来修剪音频(time_stamps.txt文件应该是逗号分隔的)。然后,添加音频文件名和它的格式,它做的工作。我在github,https://github.com/raotnameh/Trim_audio上找到了这个

代码语言:javascript
复制
import numpy as np
    from pydub import AudioSegment
    
    
    def trim(start,end,file_name,format_,i):
        t1 = start
        t2 = end
        t1 = t1 * 1000 #Works in milliseconds
        t2 = t2 * 1000
        newAudio = AudioSegment.from_wav(file_name + "." +format_)
        newAudio = newAudio[t1:t2]
        newAudio.export(file_name+ "_" + str(i) + '.wav', format=format_) #Exports to a wav file in the current path.
    
    if __name__ == '__main__':
    
        with open("time_stamps.txt", "rb") as file:
            contents = list(map(float,file.read().decode("utf-8").split(',').strip()))
    
        file_name = "male"
        format_ = "wav"
        for i in range(len(contents)):
            try :trim(contents[i],contents[i+1],file_name,format_,i)
            except : pass
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68004943

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档