Spaces:
Sleeping
Sleeping
| 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) |