当前位置: 首页 > 知识库问答 >
问题:

TensorFlow数据集-如何播放/转换WAV文件(int64)?

薛华奥
2023-03-14

我想测试以下数据集:https://www.tensorflow.org/datasets/catalog/speech_commands

当我加载时

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf

import tensorflow_datasets as tfds
import IPython.display as ipd


ds, ds_info = tfds.load('speech_commands', shuffle_files=False, with_info=True)
ds_info
tfds.core.DatasetInfo(
    name='speech_commands',
    full_name='speech_commands/0.0.2',
    description="""
    An audio dataset of spoken words designed to help train and evaluate keyword
    spotting systems. Its primary goal is to provide a way to build and test small
    models that detect when a single word is spoken, from a set of ten target words,
    with as few false positives as possible from background noise or unrelated
    speech. Note that in the train and validation set, the label "unknown" is much
    more prevalent than the labels of the target words or background noise.
    One difference from the release version is the handling of silent segments.
    While in the test set the silence segments are regular 1 second files, in the
    training they are provided as long segments under "background_noise" folder.
    Here we split these background noise into 1 second clips, and also keep one of
    the files for the validation set.
    """,
    homepage='https://arxiv.org/abs/1804.03209',
    data_path='C:\\Users\\abc\\tensorflow_datasets\\speech_commands\\0.0.2',
    download_size=2.37 GiB,
    dataset_size=9.07 GiB,
    features=FeaturesDict({
        'audio': Audio(shape=(None,), dtype=tf.int64),
        'label': ClassLabel(shape=(), dtype=tf.int64, num_classes=12),
    }),
    supervised_keys=('audio', 'label'),
    splits={
        'test': <SplitInfo num_examples=4890, num_shards=4>,
        'train': <SplitInfo num_examples=106497, num_shards=128>,
        'validation': <SplitInfo num_examples=121, num_shards=1>,
    },
    citation="""@article{speechcommandsv2,
       author = {{Warden}, P.},
        title = "{Speech Commands: A Dataset for Limited-Vocabulary Speech Recognition}",
      journal = {ArXiv e-prints},
      archivePrefix = "arXiv",
      eprint = {1804.03209},
      primaryClass = "cs.CL",
      keywords = {Computer Science - Computation and Language, Computer Science - Human-Computer Interaction},
        year = 2018,
        month = apr,
        url = {https://arxiv.org/abs/1804.03209},
    }""",
)

音频文件是int64类型的数组,采样值为16000。我找不到有关如何在此数据集中播放文件的任何信息。从其他数据集中,我能够播放WAV-声音。区别之一是,其他DS使用浮点数组,而此DS使用int数组。也许我错过了一个对话步骤?

ds_list = list(ds['validation'])

idx = -1
audio, label = ds_list[idx]['audio'], ds_list[idx]['label']
ipd.Audio(audio, rate=16_000)

很明显,我在数据集中尝试了多个索引,但我总是受到干扰。一个音频条目如下所示:tf。张量([-112 1285-2002…-140 1000-595],形状=(16000,),数据类型=int64)

Ty:)


共有1个答案

伍溪叠
2023-03-14
匿名用户

根据源代码,在描述页面[1]上,声明:

它的主要目标是提供一种构建和测试小型模型的方法,这些模型可以从一组十个目标单词中检测出一个单词何时被说出,并且尽可能少的来自背景噪音或无关语音的误报。

起初,我能够像您所展示的那样播放嘈杂的波形文件。然后,我根据[2]修改我的代码以产生更清晰的声音。

我使用以下代码将tensor转换为wav格式。

import scipy.io.wavfile as wavfile
import tensorflow as tf
import tensorflow_datasets as tfds

# load speech commands dataset
ds = tfds.load('speech_commands', split=['train', 'validation', 'test'],
           shuffle_files=True)

# convert from tfds format to list
ds_train = list(ds[0])
ds_val = list(ds[1])

# convert from tensor int64 to numpy float32
sc1 = ds_list[0]['audio'].numpy().astype(np.float32)/np.iinfo(np.int16).max()
sv1 = ds_list[1]['audio'].numpy().astype(np.float32)/np.iinfo(np.int16).max()

# save as wav
wavfile('sc_train_1.wav', 16000, sc1)
wavfile('sc_train_1.wav', 16000, sv1)

诀窍是将int64转换为float32,然后除以np的最大值。int16:<代码>。aType(np.float32)/np。iinfo(np.int16)。max()。

现在,我可以听到比以前int64格式更清晰的声音。

https://github.com/tensorflow/datasets/blob/master/tensorflow_datasets/audio/speech_commands.pyhttps://github.com/google-research/google-research/blob/master/non_semantic_speech_benchmark/train_and_eval_sklearn_small_tfds_dataset.ipynb

 类似资料:
  • 问题内容: 我正在尝试使用Java播放* .wav文件。我希望它执行以下操作: 按下按钮时,播放一声短促的哔声。 我已经用谷歌搜索了,但是大多数代码都没有用。有人可以给我一个简单的代码片段来播放.wav文件吗? 问题答案: 没有Java反射的解决方案 , ) Java反射会降低性能。跑步:

  • 问题内容: 我尝试pygame播放wav文件,如下所示: 但是它改变了声音,我不知道为什么!我阅读了此链接解决方案,但无法解决播放wave文件的问题! 对于此解决方案,我不知道应该导入什么? 对于这个解决方案/ dev / dsp在新版本的linux中不存在: 而当我尝试pyglet它给我这个错误: 问题答案: 您可以使用PyAudio。我的Linux上的一个示例可以正常工作:

  • 问题内容: 我正在用Java 做辫子。如果倒带时间,声音会向后播放。如何向后播放WAV文件?也许有像这样的流?在Braid网站上,您可以看到我的意思。 更新: _ 解决! 看到我自己的帖子。_ 问题答案: !!!!!! 我自己解决了这个问题(14岁!!), 我写了这个课: 然后:

  • 基于StackExchange的代码,我编写了打开wav文件的代码。wav文件本身是一个有效的文件,因为它可以与我的Python程序正常配合使用。但是javascript函数不起作用。声音文件与我的html文件位于同一文件夹中。 任何帮助都将不胜感激。

  • 我有一些代码(在WPF应用程序中),当一些文本被复制到剪贴板时,它将使用SpeechSynthesizer读出文本(我所有的代码都在这篇文章的底部)。 然而,以这种方式播放音频不允许我暂停、倒带或播放等。 所以我想我应该使用SpeechSynthesis来保存一个wav文件。然后使用MediaPlayer类,因为它很容易暂停、播放等。 但是,保存文件后,该文件不会在我的媒体播放器中播放。这个文件很

  • 问题内容: 我试图互相播放一些WAV文件。我尝试了这种方法: 但这同时发挥了所有作用。所以我需要一个看起来像这样的函数: 向量包含文件,例如:, 我已经寻找了四个多小时,但似乎找不到有效的解决方案:( 我还尝试将WAV文件连接到一个AudioInputStream。它不会产生任何编译器错误,但是声音完全混乱了。码: 编辑 即使我尝试将前两个文件放在一起,也会失败: 问题答案: 这段代码有点底层,但