Spaces:
Running on Zero
Running on Zero
File size: 2,760 Bytes
45c8776 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | import os
import librosa
import numpy as np
import pandas as pd
from tqdm import tqdm
from collections import Counter
# ===============================
# Load Labels
# ===============================
def load_dev_labels():
if os.path.exists("dataset/dev_labels.csv"):
labels = pd.read_csv("dataset/dev_labels.csv")
print(labels.head())
print("\nTotal Files:", len(labels))
return labels
return None
# ===============================
# Paths
# ===============================
AUDIO_FOLDER = "dataset/LA/ASVspoof2019_LA_dev/flac"
OUTPUT_FOLDER = "processed"
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
# ===============================
# Parameters
# ===============================
N_MFCC = 40
MAX_FRAMES = 138
X = []
y = []
# ===============================
# Label Encoding
# ===============================
def label_to_number(label):
if label == "bonafide":
return 0
return 1
# ===============================
# Pad / Truncate Function
# ===============================
def pad_or_truncate(mfcc):
current_frames = mfcc.shape[1]
if current_frames < MAX_FRAMES:
pad_width = MAX_FRAMES - current_frames
mfcc = np.pad(
mfcc,
pad_width=((0, 0), (0, pad_width)),
mode="constant"
)
else:
mfcc = mfcc[:, :MAX_FRAMES]
return mfcc
def main():
labels = load_dev_labels()
if labels is None:
print("Dev labels file 'dataset/dev_labels.csv' not found.")
return
X = []
y = []
print("Extracting dev features...")
for index, row in tqdm(labels.iterrows(), total=len(labels)):
audio_path = os.path.join(AUDIO_FOLDER, row["file_name"])
if not os.path.exists(audio_path):
continue
try:
signal, sr = librosa.load(audio_path, sr=16000)
mfcc = librosa.feature.mfcc(y=signal, sr=sr, n_mfcc=N_MFCC)
mfcc = pad_or_truncate(mfcc)
mfcc = (mfcc - np.mean(mfcc)) / (np.std(mfcc) + 1e-8)
X.append(mfcc)
y.append(label_to_number(row["label"]))
except Exception as e:
print(f"\nError processing {row['file_name']}: {e}")
print("\nProcessed Files :", len(X))
print("\nLabel Distribution:", Counter(y))
if len(X) == 0:
print("No dev audio files found in AUDIO_FOLDER.")
return
X = np.array(X, dtype=np.float32)
y = np.array(y, dtype=np.int32)
X = np.expand_dims(X, axis=-1)
print("\nFinal Shapes")
print("X :", X.shape)
print("y :", y.shape)
np.save("processed/X_dev.npy", X)
np.save("processed/y_dev.npy", y)
print("\nDevelopment dataset saved successfully!")
if __name__ == "__main__":
main() |