从大的.tgz文件中提取.wav文件,并将它们存储到数组中进行分类

问题描述 投票:0回答:1

我需要执行机器学习任务,其中在录音中发音多个英语单词。我的任务是将多个录音作为输入,并对每个录音中哪个单词的发音进行分类。录音是单独的.wav文件,并一起存储在.tgz文件中。在我的代码中,我想提供.tgz文件作为输入,并将所有单个.wav文件存储到数据数组中。这是我到目前为止尝试过的:

data = np.array([])
tar = tarfile.open("wav.tgz", "r:gz")
for member in tar.getmembers():
    f = tar.extractfile(member)
    if f is not None:
        data = np.asarray(f.read())
tar.close()

但是,当打印数据时,将所有信息作为字节返回,存储在数组中,如下所示:

b'RIFF$}\x00\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00\x80>\x00\x00\x00}\x00\x00\x02\x00\x10\x00data\x00}\x00\x00\xcd\xff\xce\xff\xcb\xff\xca\xff\xcb\xff\xc8\xff\xcc\xff\xce\xff\xd0\xff\xd4\xff\xd1\xff\xcc\xff\xc9\xff\xdd\xff\xe5\xff\xd7\xff\xd3\xff\xd4\xff\xdd\xff\xd9\xff\xca\xff\xd9\xff\xe4\xff\xea\xff\xf4\xff\xed\xff\xeb\xff\xec\xff\xf1\xff\xf2\xff\xe5\xff\xec\xff\xf3\xff\xe9\xff\xea\xff\xe5\xff\xe1\xff\xe4\xff\xe0\xff\xe7\xff\xef\xff\xef\xff\xf4\xff\xf4\xff\xf6\xff\xf4\xff\xe8\xff\xde\

这会持续一段时间,因为它是一个很大的文件。有人可以帮我什么最好的方法是从.tgz文件中分别提取这些文件吗?另外,我不确定提取文件后处理文件的最佳方法是什么,也欢迎提供有关该部分的更多建议!

python extraction multiclass-classification
1个回答
0
投票

尝试使用scipy.io.wavfile

import scipy

all_data = []
with tarfile.open("wav.tgz", "r:gz") as tar:
    for member in tar.getmembers():
        f = tar.extractfile(member)
        if f is not None:
            rate, data = scipy.io.wavfile.read(f)
            all_data.append(data)
© www.soinside.com 2019 - 2024. All rights reserved.