-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
64 additions
and
199 deletions.
There are no files selected for viewing
36 changes: 0 additions & 36 deletions
36
src/ASR-with-Speech-Sentiment-Analysis-Text-Summarizer/Speech_Sentiment_Analysis/demo.py
This file was deleted.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
src/ASR-with-Speech-Sentiment-Analysis-Text-Summarizer/Speech_Sentiment_Analysis/engine.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import numpy as np | ||
import joblib | ||
import librosa | ||
import torch | ||
|
||
from sklearn.preprocessing import StandardScaler | ||
from neuralnet.model import HybridModel | ||
from feature import getMELspectrogram, splitIntoChunks | ||
|
||
|
||
EMOTIONS = { | ||
1: 'neutral', | ||
2: 'calm', | ||
3: 'happy', | ||
4: 'sad', | ||
5: 'angry', | ||
6: 'fear', | ||
7: 'disgust', | ||
0: 'surprise' | ||
} | ||
|
||
scaler = StandardScaler() | ||
model = HybridModel(len(EMOTIONS)) | ||
model.load_state_dict(torch.load("model/speech_sentiment.pt", map_location=torch.device('cpu'))) | ||
SAMPLE_RATE = 48000 | ||
scaler = joblib.load('model/scaler.pkl') | ||
|
||
def process_audio(audio_file_path): | ||
global scaler | ||
chunked_spec = [] | ||
|
||
# Load audio file | ||
audio, sample_rate = librosa.load(audio_file_path, sr=SAMPLE_RATE, duration=3) | ||
signal = np.zeros((int(SAMPLE_RATE * 3),)) | ||
signal[:len(audio)] = audio | ||
mel_spectrogram = getMELspectrogram(signal, SAMPLE_RATE) | ||
chunks = splitIntoChunks(mel_spectrogram, win_size=128, stride=64) | ||
|
||
chunked_spec.append(chunks) | ||
chunks = np.stack(chunked_spec, axis=0) | ||
chunks = np.expand_dims(chunks, axis=2) | ||
|
||
# Reshape the chunks | ||
chunks = np.reshape(chunks, newshape=(1, -1)) | ||
chunks_scaled = scaler.transform(chunks) | ||
chunks_scaled = np.reshape(chunks_scaled, newshape=(1, 7, 1, 128, 128)) | ||
|
||
# Convert to tensor for model input | ||
chunks_tensor = torch.tensor(chunks_scaled).float() | ||
|
||
# Model inference | ||
with torch.inference_mode(): | ||
model.eval() | ||
_, output_softmax, _ = model(chunks_tensor) | ||
predictions = torch.argmax(output_softmax, dim=1) | ||
print(predictions) | ||
predicted_emotion = EMOTIONS[predictions.item()] | ||
|
||
print(f"Predicted Emotion: {predicted_emotion}") | ||
return predicted_emotion | ||
|
||
file_path = "fear.wav" | ||
process_audio(file_path) | ||
|
File renamed without changes.
121 changes: 0 additions & 121 deletions
121
src/ASR-with-Speech-Sentiment-Analysis-Text-Summarizer/Speech_Sentiment_Analysis/main.py
This file was deleted.
Oops, something went wrong.
42 changes: 0 additions & 42 deletions
42
src/ASR-with-Speech-Sentiment-Analysis-Text-Summarizer/Speech_Sentiment_Analysis/utils.py
This file was deleted.
Oops, something went wrong.