Ejemplo n.º 1
0
Archivo: cli.py Proyecto: wangyu/openl3
def run(inputs, output_dir=None, suffix=None, input_repr="mel256", content_type="music",
        embedding_size=6144, center=True, hop_size=0.1, verbose=False):
    """
    Computes and saves L3 embedding for given inputs.

    Parameters
    ----------
    inputs : list of str, or str
        File/directory path or list of file/directory paths to be processed
    output_dir : str or None
        Path to directory for saving output files. If None, output files will
        be saved to the directory containing the input file.
    suffix : str or None
        String to be appended to the output filename, i.e. <base filename>_<suffix>.npy.
        If None, then no suffix will be added, i.e. <base filename>.npy.
    input_repr : "linear", "mel128", or "mel256"
        Spectrogram representation used for model.
    content_type : "music" or "env"
        Type of content used to train embedding.
    embedding_size : 6144 or 512
        Embedding dimensionality.
    center : boolean
        If True, pads beginning of signal so timestamps correspond
        to center of window.
    hop_size : float
        Hop size in seconds.
    quiet : boolean
        If True, suppress all non-error output to stdout

    Returns
    -------
    """

    if isinstance(inputs, string_types):
        file_list = [inputs]
    elif isinstance(inputs, Iterable):
        file_list = get_file_list(inputs)
    else:
        raise OpenL3Error('Invalid input: {}'.format(str(inputs)))

    if len(file_list) == 0:
        print('openl3: No WAV files found in {}. Aborting.'.format(str(inputs)))
        sys.exit(-1)

    # Load model
    model = load_embedding_model(input_repr, content_type, embedding_size)

    # Process all files in the arguments
    for filepath in file_list:
        if verbose:
            print('openl3: Processing: {}'.format(filepath))
        process_file(filepath,
                     output_dir=output_dir,
                     suffix=suffix,
                     model=model,
                     center=center,
                     hop_size=hop_size,
                     verbose=verbose)
    if verbose:
        print('openl3: Done!')
def embedFiles(datasetPath, newDir):
    soundfiles = urbansound8k.load_dataset()

    model = openl3.models.load_embedding_model(input_repr="mel256", content_type="env",
                                           embedding_size=512)
    for index in range(len(soundfiles)):
        sample = soundfiles.iloc[index, :] #fetch each sample from the dataset
        path = urbansound8k.sample_path(sample,datasetPath) #find the path given the sample, and the directory the files lay in.
        fold = sample["fold"] #get the fold for the sound from the sample
        differentDir = newDir+str(fold) #assign directory to save file in by adding the respective fold to the path
        openl3.process_file(path, suffix=None, output_dir=differentDir,model=model, content_type="env", embedding_size=512, hop_size=0.5, verbose=True) #save the embedded file in its respective fold in the differentDir directory
Ejemplo n.º 3
0
def test_process_file():
    test_output_dir = tempfile.mkdtemp()
    test_subdir = os.path.join(test_output_dir, "subdir")
    os.makedirs(test_subdir)

    # Make a copy of the file so we can test the case where we save to the same directory
    input_path_alt = os.path.join(test_subdir, "chirp_mono.wav")
    shutil.copy(CHIRP_MONO_PATH, test_subdir)

    invalid_file_path = os.path.join(test_subdir, "invalid.wav")
    with open(invalid_file_path, 'w') as f:
        f.write('This is not an audio file.')

    exp_output_path1 = os.path.join(test_output_dir, "chirp_mono.npz")
    exp_output_path2 = os.path.join(test_output_dir, "chirp_mono_suffix.npz")
    exp_output_path3 = os.path.join(test_subdir, "chirp_mono.npz")
    try:
        openl3.process_file(CHIRP_MONO_PATH, output_dir=test_output_dir)
        openl3.process_file(CHIRP_MONO_PATH,
                            output_dir=test_output_dir,
                            suffix='suffix')
        openl3.process_file(input_path_alt)

        # Make sure we fail when invalid files are provided
        pytest.raises(OpenL3Error, openl3.process_file, invalid_file_path)

        # Make sure paths all exist
        assert os.path.exists(exp_output_path1)
        assert os.path.exists(exp_output_path2)
        assert os.path.exists(exp_output_path3)

        data = np.load(exp_output_path1)
        assert 'embedding' in data
        assert 'timestamps' in data

        embedding = data['embedding']
        timestamps = data['timestamps']

        # Quick sanity check on data
        assert embedding.ndim == 2
        assert timestamps.ndim == 1

        # Make sure that suffices work
    finally:
        shutil.rmtree(test_output_dir)

    # Make sure we fail when file cannot be opened
    pytest.raises(OpenL3Error, openl3.process_file, '/fake/directory/asdf.wav')