-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
82 lines (64 loc) · 2.64 KB
/
Copy pathapp.py
File metadata and controls
82 lines (64 loc) · 2.64 KB
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
import streamlit as st
import numpy as np
import sounddevice as sd
import librosa
import tensorflow as tf
from scipy.io.wavfile import write
from tensorflow.keras.models import load_model
# Load your pre-trained model
model_path = "emotion_detector.h5"
model = load_model(model_path)
# Define emotion labels
emotion_names = ['Happy', 'Sad', 'Angry', 'Fear', 'Disgust', 'Surprise']
# Streamlit app title
st.title("Real-Time Speech Emotion Detection")
# Set parameters for recording
duration = 5 # seconds
sample_rate = 22050 # Hz (matches librosa's default sample rate)
# Record audio function
def record_audio():
st.write("Recording for {} seconds...".format(duration))
audio = sd.rec(int(duration * sample_rate), samplerate=sample_rate, channels=1)
sd.wait() # Wait until recording is finished
st.write("Recording completed!")
return audio.flatten()
# Preprocess the audio to extract mel spectrograms
import librosa
import numpy as np
import librosa
import numpy as np
def preprocess_audio(audio, sample_rate=22050, target_shape=(128, 154)):
# Ensure audio is in the correct format
audio = np.float32(audio)
# Extract mel spectrogram
mel_spec = librosa.feature.melspectrogram(y=audio, sr=sample_rate, n_mels=target_shape[0])
mel_spec_db = librosa.power_to_db(mel_spec, ref=np.max)
# Adjust the time dimension to match the target shape
if mel_spec_db.shape[1] > target_shape[1]: # If too long, truncate
mel_spec_db = mel_spec_db[:, :target_shape[1]]
elif mel_spec_db.shape[1] < target_shape[1]: # If too short, pad with zeros
padding = target_shape[1] - mel_spec_db.shape[1]
mel_spec_db = np.pad(mel_spec_db, ((0, 0), (0, padding)), mode='constant')
# Add channel dimension and repeat to make 3 channels
mel_spec_db = mel_spec_db[..., np.newaxis]
mel_spec_db = np.repeat(mel_spec_db, 3, axis=-1)
return mel_spec_db
# Predict emotion function
def predict_emotion(audio):
# Preprocess audio
processed_audio = preprocess_audio(audio, sample_rate)
processed_audio = np.expand_dims(processed_audio, axis=0) # Add batch dimension
# Predict with the model
predictions = model.predict(processed_audio)
predicted_class = np.argmax(predictions, axis=1)[0]
emotion = emotion_names[predicted_class]
return emotion
# Streamlit UI for recording and predicting
if st.button("Record Audio"):
audio = record_audio()
# Save audio temporarily to check (optional)
write("temp_audio.wav", sample_rate, audio)
# Predict the emotion
emotion = predict_emotion(audio)
# Display result
st.write("Predicted Emotion:", emotion)