Upload Frondend python file

parent 56114cb7
import os
import numpy as np
import librosa
import tensorflow as tf
import pyaudio
import wave
import threading
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.app import MDApp
from kivymd.uix.button import MDRaisedButton
from kivymd.uix.label import MDLabel
from main_imports import MDScreen
from ProjectFiles.applibs import utils
import warnings
import pandas as pd
from sklearn import metrics
from sklearn.model_selection import train_test_split
from sklearn.tree import export_graphviz
from six import StringIO
import pydotplus
from sklearn.tree import DecisionTreeClassifier
warnings.filterwarnings('ignore')
utils.load_kv("speak.kv")
# Load the saved model
model = tf.keras.models.load_model('model.h5')
# Define a function to extract features from audio data
def extract_features(audio_data, sr=22050):
mfcc = librosa.feature.mfcc(y=audio_data, sr=sr, n_mfcc=20)
mfcc_mean = np.mean(mfcc, axis=1)
mfcc_delta = librosa.feature.delta(mfcc, width=3)
mfcc_delta2 = librosa.feature.delta(mfcc, width=3, order=2)
mfcc_delta_mean = np.mean(mfcc_delta, axis=1)
mfcc_delta2_mean = np.mean(mfcc_delta2, axis=1)
features = np.concatenate((mfcc_mean, mfcc_delta_mean, mfcc_delta2_mean))
return features
# Define a function to predict the mood from audio data
def predict_mood(audio_data):
features = extract_features(audio_data)
features = features.reshape(1, -1)
predictions = model.predict(features)
emotion_labels = ['angry', 'calm', 'disgust', 'fearful', 'happy', 'neutral', 'sad']
predicted_label = emotion_labels[np.argmax(predictions)]
return predicted_label
class speak_Screen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.recording_thread = None
self.recording = False
self.audio_buffer = []
def btnA(self):
if not self.recording:
self.start_recording()
self.ids.l1.text = "Recording started"
else:
self.ids.l1.text = "Please wait, text is getting ready"
self.analyze_audio()
def start_recording(self):
self.recording = True
print("Recording started")
self.audio_buffer = [] # Clear the audio buffer
self.recording_thread = threading.Thread(target=self.record_audio)
self.recording_thread.start()
def record_audio(self):
chunk = 1024
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=44100, input=True, frames_per_buffer=1024)
frames = []
while self.recording:
data = stream.read(1024)
frames.append(data)
self.audio_buffer.append(data)
stream.stop_stream()
stream.close()
p.terminate()
def analyze_audio(self):
if not self.audio_buffer:
return
audio_data = np.frombuffer(b''.join(self.audio_buffer), dtype=np.int16)
# Normalize audio data to the range [-1, 1]
audio_data = audio_data.astype(np.float32) / 32768.0
temp_audio_file = "temp_audio.wav"
wf = wave.open(temp_audio_file, 'wb')
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(44100)
wf.writeframes(audio_data.tobytes())
wf.close()
predicted_mood = predict_mood(audio_data)
print('Predicted mood:', predicted_mood)
self.ids.l1.text = 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