|
|
|
|
|
|
|
|
|
import streamlit as st |
|
import pyaudio |
|
import wave |
|
import torch |
|
from transformers import Wav2Vec2ForSequenceClassification, Wav2Vec2Processor |
|
import numpy as np |
|
import time |
|
|
|
|
|
model_name = "superb/wav2vec2-base-superb-er" |
|
processor = Wav2Vec2Processor.from_pretrained(model_name) |
|
model = Wav2Vec2ForSequenceClassification.from_pretrained(model_name) |
|
|
|
|
|
CHUNK = 1024 |
|
FORMAT = pyaudio.paInt16 |
|
CHANNELS = 1 |
|
RATE = 16000 |
|
|
|
|
|
def predict_emotion(audio_data): |
|
inputs = processor(audio_data, sampling_rate=RATE, return_tensors="pt", padding=True) |
|
with torch.no_grad(): |
|
logits = model(**inputs).logits |
|
predicted_id = torch.argmax(logits, dim=-1).item() |
|
emotion = model.config.id2label[predicted_id] |
|
return emotion |
|
|
|
|
|
st.title("Détection des émotions en temps réel") |
|
|
|
|
|
start_button = st.button("Démarrer l'enregistrement") |
|
stop_button = st.button("Arrêter l'enregistrement") |
|
|
|
|
|
emotion_placeholder = st.empty() |
|
final_emotion_placeholder = st.empty() |
|
|
|
if start_button: |
|
st.write("Enregistrement en cours...") |
|
audio = pyaudio.PyAudio() |
|
stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) |
|
|
|
frames = [] |
|
real_time_emotions = [] |
|
|
|
while not stop_button: |
|
data = stream.read(CHUNK) |
|
frames.append(data) |
|
|
|
|
|
if len(frames) >= RATE // CHUNK: |
|
audio_segment = np.frombuffer(b''.join(frames[-(RATE // CHUNK):]), dtype=np.int16) |
|
emotion = predict_emotion(audio_segment) |
|
real_time_emotions.append(emotion) |
|
emotion_placeholder.line_chart(real_time_emotions) |
|
|
|
|
|
stream.stop_stream() |
|
stream.close() |
|
audio.terminate() |
|
|
|
|
|
wf = wave.open("output.wav", "wb") |
|
wf.setnchannels(CHANNELS) |
|
wf.setsampwidth(audio.get_sample_size(FORMAT)) |
|
wf.setframerate(RATE) |
|
wf.writeframes(b"".join(frames)) |
|
wf.close() |
|
|
|
|
|
full_audio_data = np.frombuffer(b''.join(frames), dtype=np.int16) |
|
final_emotion = predict_emotion(full_audio_data) |
|
|
|
final_emotion_placeholder.write(f"Émotion finale prédite : {final_emotion}") |
|
|