Spaces:
Sleeping
Sleeping
File size: 1,105 Bytes
730469b 892819b 730469b 892819b 730469b 892819b 730469b 892819b 730469b 892819b 730469b 892819b |
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 |
import librosa
import torch
import pandas as pd
from torch.utils.data import Dataset
import os
class EmotionDataset(Dataset):
def __init__(self, csv_file, processor):
self.data = pd.read_csv(csv_file, sep=",", header=0)
# print(self.data.info()) # Pour voir les premières lignes du dataset
self.processor = processor
self.emotion_labels = {"joie": 0, "colere": 1, "neutre": 2}
# print(self.data["emotion"].unique()) # Pour voir les valeurs exactes
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
base_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "data"))
audio_file = self.data.iloc[idx, 0]
label = self.emotion_labels[self.data.iloc[idx, 1].strip()]
audio_path = os.path.join(base_path, audio_file)
waveform, _ = librosa.load(audio_path, sr=16000) # Chargement audio
input_values = self.processor(waveform, return_tensors="pt", sampling_rate=16000).input_values
return input_values.squeeze(0), torch.tensor(label, dtype=torch.long)
|