Upload Detect.py

parent 90128a31
import os
import numpy as np
import librosa
import tensorflow as tf
# Load the saved model
model = tf.keras.models.load_model('model.h5')
# Define a function to extract features from the audio file
def extract_features(audio_path):
# Load the audio file
y, sr = librosa.load(audio_path, sr=22050, duration=3)
# Extract features using Mel-frequency cepstral coefficients (MFCC)
mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=20)
# Compute the mean of each feature dimension
mfcc_mean = np.mean(mfcc, axis=1)
# Compute the first and second derivatives of the MFCC features
mfcc_delta = librosa.feature.delta(mfcc)
mfcc_delta2 = librosa.feature.delta(mfcc, order=2)
# Compute the mean of each derivative feature dimension
mfcc_delta_mean = np.mean(mfcc_delta, axis=1)
mfcc_delta2_mean = np.mean(mfcc_delta2, axis=1)
# Concatenate the feature vectors
features = np.concatenate((mfcc_mean, mfcc_delta_mean, mfcc_delta2_mean))
return features
# Define a function to predict the mood from the audio file
def predict_mood(audio_path):
# Extract features from the audio file
features = extract_features(audio_path)
# Reshape the features to match the input shape of the model
features = features.reshape(1, -1)
# Make the prediction
predictions = model.predict(features)
# Convert the prediction to an emotion label
emotion_labels = ['angry', 'calm', 'disgust', 'fearful', 'happy', 'neutral', 'sad']
predicted_label = emotion_labels[np.argmax(predictions)]
return predicted_label
# Test the function on an example audio file
audio_file = 'audio.wav'
predicted_mood = predict_mood(audio_file)
print('Predicted mood:', predicted_mood)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment