File size: 2,739 Bytes
60171af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
################################
### NOT YET TESTED
###############################

import streamlit as st
import pyaudio
import wave
import torch
from transformers import Wav2Vec2ForSequenceClassification, Wav2Vec2Processor
import numpy as np
import time

# Charger le modèle Wav2Vec2 pour la classification des émotions
model_name = "superb/wav2vec2-base-superb-er"  # Exemple de modèle pour la reconnaissance des émotions
processor = Wav2Vec2Processor.from_pretrained(model_name)
model = Wav2Vec2ForSequenceClassification.from_pretrained(model_name)

# Paramètres audio
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000

# Fonction pour prédire l'émotion à partir d'un segment audio
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

# Interface Streamlit
st.title("Détection des émotions en temps réel")

# Boutons pour démarrer et arrêter l'enregistrement
start_button = st.button("Démarrer l'enregistrement")
stop_button = st.button("Arrêter l'enregistrement")

# Zone de visualisation des émotions en temps réel
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)
        
        # Traitement en temps réel (par tranche de 1 seconde)
        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)  # Affichage graphique des émotions

    # Arrêt de l'enregistrement
    stream.stop_stream()
    stream.close()
    audio.terminate()
    
    # Sauvegarde de l'audio enregistré
    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()

    # Prédiction finale sur tout l'audio enregistré
    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}")