Kaldi Python IO

1.2.2 · active · verified Wed Apr 15

kaldi-python-io is a pure Python library providing an I/O interface for accessing data in the Kaldi speech recognition toolkit's native formats. It allows reading and writing various Kaldi data types such as matrices, vectors, and alignments directly in Python. The current version is 1.2.2, with new releases typically occurring every few months based on development activity.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a NumPy matrix, write it to a Kaldi archive (`.ark`) file using `kaldi-python-io`, and then read it back. It includes file cleanup.

import kaldi_io
import numpy as np
import os

# Define a path for the temporary Kaldi archive file
temp_ark_path = "temp_matrix.ark"

try:
    # 1. Create a NumPy array (e.g., a feature matrix)
    # Kaldi typically uses float32 for features
    feature_matrix = np.array([[1.1, 2.2, 3.3],
                               [4.4, 5.5, 6.6]], dtype=np.float32)

    print(f"Original matrix:\n{feature_matrix}")

    # 2. Write the NumPy array to a Kaldi archive file
    # The library supports writing directly to a file path or file-like object
    # 'key' is important for Kaldi archives to identify the data
    with kaldi_io.open_or_fd(temp_ark_path, 'wb') as f:
        kaldi_io.write_mat(f, feature_matrix, key='utt1_features')

    print(f"Successfully wrote matrix to '{temp_ark_path}' with key 'utt1_features'.")

    # 3. Read the matrix back from the Kaldi archive file
    # kaldi_io.read_mat_s is a generator for multiple matrices in an ark file
    read_data_generator = kaldi_io.read_mat_s(temp_ark_path)
    
    # Since we wrote only one, we expect one (key, matrix) tuple. Get the first.
    key, loaded_matrix = next(read_data_generator)

    print(f"\nRead back data:")
    print(f"Key: {key}")
    print(f"Matrix:\n{loaded_matrix}")

    # Verify if the loaded matrix matches the original
    assert np.allclose(feature_matrix, loaded_matrix)
    print("\nVerification successful: Loaded matrix matches original.")

finally:
    # Clean up the temporary file
    if os.path.exists(temp_ark_path):
        os.remove(temp_ark_path)
        print(f"Cleaned up temporary file: {temp_ark_path}")

view raw JSON →