Spaces:
Sleeping
Sleeping
File size: 1,114 Bytes
e88666e f2cac03 e88666e f2cac03 e88666e f2cac03 | 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 | import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import librosa
import matplotlib.pyplot as plt
from transformers import Wav2Vec2Processor, Wav2Vec2Model
from config import TRAIN_AUDIO
import torch
sample_file = os.path.join(TRAIN_AUDIO, 'dia47_utt11.mp4')
audio, sr = librosa.load(sample_file, sr=16000)
# 1st Using MFCC with librosa to extract features
# Audio -> Feature Extraction -> CNN/LSTM -> Emotion Recognition
mfcc = librosa.feature.mfcc(y=audio, sr=sr, n_mfcc=13)
plt.imshow(mfcc, aspect='auto', origin='lower')
plt.title("MFCC Features")
plt.colorbar()
plt.show()
# 2nd Using Modern Feature Extraction models (Wav2Vec2, HuBERT)
# they learn features directly from raw audio and are often more powerful for downstream tasks like emotion recognition
#Audio -> Pretrained Model -> Emotion Classifier
processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base")
model = Wav2Vec2Model.from_pretrained("facebook/wav2vec2-base")
inputs = processor(audio, sampling_rate=16000, return_tensors="pt")
outputs = model(**inputs)
print(outputs.last_hidden_state.shape) |