Falonne Kpamegan commited on
Commit
2bcf2aa
·
2 Parent(s): 730469b 60171af
Files changed (2) hide show
  1. requirements.txt +6 -0
  2. views/real_time.py +80 -0
requirements.txt CHANGED
@@ -8,3 +8,9 @@ numpy
8
  pandas
9
  matplotlib
10
  scikit-learn
 
 
 
 
 
 
 
8
  pandas
9
  matplotlib
10
  scikit-learn
11
+ streamlit
12
+ streamlit_option_menu
13
+ torchaudio
14
+ huggingface
15
+ huggingface_hub
16
+ pyaudio
views/real_time.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ################################
2
+ ### NOT YET TESTED
3
+ ###############################
4
+
5
+ import streamlit as st
6
+ import pyaudio
7
+ import wave
8
+ import torch
9
+ from transformers import Wav2Vec2ForSequenceClassification, Wav2Vec2Processor
10
+ import numpy as np
11
+ import time
12
+
13
+ # Charger le modèle Wav2Vec2 pour la classification des émotions
14
+ model_name = "superb/wav2vec2-base-superb-er" # Exemple de modèle pour la reconnaissance des émotions
15
+ processor = Wav2Vec2Processor.from_pretrained(model_name)
16
+ model = Wav2Vec2ForSequenceClassification.from_pretrained(model_name)
17
+
18
+ # Paramètres audio
19
+ CHUNK = 1024
20
+ FORMAT = pyaudio.paInt16
21
+ CHANNELS = 1
22
+ RATE = 16000
23
+
24
+ # Fonction pour prédire l'émotion à partir d'un segment audio
25
+ def predict_emotion(audio_data):
26
+ inputs = processor(audio_data, sampling_rate=RATE, return_tensors="pt", padding=True)
27
+ with torch.no_grad():
28
+ logits = model(**inputs).logits
29
+ predicted_id = torch.argmax(logits, dim=-1).item()
30
+ emotion = model.config.id2label[predicted_id]
31
+ return emotion
32
+
33
+ # Interface Streamlit
34
+ st.title("Détection des émotions en temps réel")
35
+
36
+ # Boutons pour démarrer et arrêter l'enregistrement
37
+ start_button = st.button("Démarrer l'enregistrement")
38
+ stop_button = st.button("Arrêter l'enregistrement")
39
+
40
+ # Zone de visualisation des émotions en temps réel
41
+ emotion_placeholder = st.empty()
42
+ final_emotion_placeholder = st.empty()
43
+
44
+ if start_button:
45
+ st.write("Enregistrement en cours...")
46
+ audio = pyaudio.PyAudio()
47
+ stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)
48
+
49
+ frames = []
50
+ real_time_emotions = []
51
+
52
+ while not stop_button:
53
+ data = stream.read(CHUNK)
54
+ frames.append(data)
55
+
56
+ # Traitement en temps réel (par tranche de 1 seconde)
57
+ if len(frames) >= RATE // CHUNK:
58
+ audio_segment = np.frombuffer(b''.join(frames[-(RATE // CHUNK):]), dtype=np.int16)
59
+ emotion = predict_emotion(audio_segment)
60
+ real_time_emotions.append(emotion)
61
+ emotion_placeholder.line_chart(real_time_emotions) # Affichage graphique des émotions
62
+
63
+ # Arrêt de l'enregistrement
64
+ stream.stop_stream()
65
+ stream.close()
66
+ audio.terminate()
67
+
68
+ # Sauvegarde de l'audio enregistré
69
+ wf = wave.open("output.wav", "wb")
70
+ wf.setnchannels(CHANNELS)
71
+ wf.setsampwidth(audio.get_sample_size(FORMAT))
72
+ wf.setframerate(RATE)
73
+ wf.writeframes(b"".join(frames))
74
+ wf.close()
75
+
76
+ # Prédiction finale sur tout l'audio enregistré
77
+ full_audio_data = np.frombuffer(b''.join(frames), dtype=np.int16)
78
+ final_emotion = predict_emotion(full_audio_data)
79
+
80
+ final_emotion_placeholder.write(f"Émotion finale prédite : {final_emotion}")