Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch as pt
|
| 3 |
import torchaudio
|
|
@@ -7,6 +17,7 @@ import numpy as np
|
|
| 7 |
import tensorflow as tf
|
| 8 |
from tensorflow.keras.models import load_model
|
| 9 |
from moviepy.editor import VideoFileClip
|
|
|
|
| 10 |
|
| 11 |
def convert_video_to_audio_moviepy(video_file, output_ext="wav"):
|
| 12 |
"""Converts video to audio using MoviePy library that uses `ffmpeg` under the hood"""
|
|
@@ -29,7 +40,8 @@ def process_video_audio(video_path):
|
|
| 29 |
|
| 30 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 31 |
|
| 32 |
-
if
|
|
|
|
| 33 |
print(wav.shape)
|
| 34 |
train_audio_wave[0, :] = wav[0][:261540]
|
| 35 |
else:
|
|
@@ -79,24 +91,91 @@ def predict_emotion(video_path):
|
|
| 79 |
predicted_label = np.argmax(predictions)
|
| 80 |
return last_frame, audio_path, predicted_label
|
| 81 |
|
| 82 |
-
def predict_emotion_gradio(video_path):
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
last_frame, audio_path, predicted_label = predict_emotion(video_path)
|
| 85 |
predicted_emotion = emotion_dict[predicted_label]
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
iface = gr.Interface(
|
| 89 |
-
fn=
|
| 90 |
inputs=[
|
| 91 |
gr.Video(label="Upload a video")
|
| 92 |
],
|
| 93 |
outputs=[
|
| 94 |
gr.Image(label="Last Frame"),
|
| 95 |
-
gr.Audio(label
|
| 96 |
-
gr.Textbox(label="Predicted Emotion")
|
|
|
|
| 97 |
],
|
| 98 |
-
title="Emotion Recognition
|
| 99 |
-
description="Upload a video and get the predicted emotion."
|
| 100 |
)
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
iface.launch()
|
|
|
|
| 1 |
+
# import gradio as gr
|
| 2 |
+
# import torch as pt
|
| 3 |
+
# import torchaudio
|
| 4 |
+
# import cv2
|
| 5 |
+
# import os
|
| 6 |
+
# import numpy as np
|
| 7 |
+
# import tensorflow as tf
|
| 8 |
+
# from tensorflow.keras.models import load_model
|
| 9 |
+
# from moviepy.editor import VideoFileClip
|
| 10 |
+
|
| 11 |
import gradio as gr
|
| 12 |
import torch as pt
|
| 13 |
import torchaudio
|
|
|
|
| 17 |
import tensorflow as tf
|
| 18 |
from tensorflow.keras.models import load_model
|
| 19 |
from moviepy.editor import VideoFileClip
|
| 20 |
+
import socketIO_client as sio
|
| 21 |
|
| 22 |
def convert_video_to_audio_moviepy(video_file, output_ext="wav"):
|
| 23 |
"""Converts video to audio using MoviePy library that uses `ffmpeg` under the hood"""
|
|
|
|
| 40 |
|
| 41 |
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 42 |
|
| 43 |
+
if
|
| 44 |
+
len(wav[0]) > 261540:
|
| 45 |
print(wav.shape)
|
| 46 |
train_audio_wave[0, :] = wav[0][:261540]
|
| 47 |
else:
|
|
|
|
| 91 |
predicted_label = np.argmax(predictions)
|
| 92 |
return last_frame, audio_path, predicted_label
|
| 93 |
|
| 94 |
+
# def predict_emotion_gradio(video_path):
|
| 95 |
+
# emotion_dict = {0: 'neutral', 1: 'calm', 2: 'happy', 3: 'sad', 4: 'angry', 5: 'fearful'}
|
| 96 |
+
# last_frame, audio_path, predicted_label = predict_emotion(video_path)
|
| 97 |
+
# predicted_emotion = emotion_dict[predicted_label]
|
| 98 |
+
# return last_frame, audio_path, predicted_emotion
|
| 99 |
+
|
| 100 |
+
# iface = gr.Interface(
|
| 101 |
+
# fn=predict_emotion_gradio,
|
| 102 |
+
# inputs=[
|
| 103 |
+
# gr.Video(label="Upload a video")
|
| 104 |
+
# ],
|
| 105 |
+
# outputs=[
|
| 106 |
+
# gr.Image(label="Last Frame"),
|
| 107 |
+
# gr.Audio(label = "Audio"),
|
| 108 |
+
# gr.Textbox(label="Predicted Emotion")
|
| 109 |
+
# ],
|
| 110 |
+
# title="Emotion Recognition from Video",
|
| 111 |
+
# description="Upload a video and get the predicted emotion."
|
| 112 |
+
# )
|
| 113 |
+
|
| 114 |
+
# iface.launch()
|
| 115 |
+
|
| 116 |
+
def run_chat_server(app):
|
| 117 |
+
"""Runs a chat server using socket.IO"""
|
| 118 |
+
clients = []
|
| 119 |
+
messages = []
|
| 120 |
+
|
| 121 |
+
@app.route('/chat', methods=['GET', 'POST'])
|
| 122 |
+
def chat():
|
| 123 |
+
return app.socketio.send(messages)
|
| 124 |
+
|
| 125 |
+
@app.socketio.on('message')
|
| 126 |
+
def handle_message(message):
|
| 127 |
+
clients.append(message['client'])
|
| 128 |
+
messages.append(message)
|
| 129 |
+
app.logger.info(f'Received message: {message}')
|
| 130 |
+
app.socketio.emit('message', message, skip_sid=True)
|
| 131 |
+
|
| 132 |
+
@app.socketio.on('connect')
|
| 133 |
+
def handle_connect():
|
| 134 |
+
app.logger.info('Client connected')
|
| 135 |
+
|
| 136 |
+
@app.socketio.on('disconnect')
|
| 137 |
+
def handle_disconnect():
|
| 138 |
+
app.logger.info('Client disconnected')
|
| 139 |
+
|
| 140 |
+
if __name__ == '__main__':
|
| 141 |
+
app.run(debug=True)
|
| 142 |
+
|
| 143 |
+
def predict_emotion_with_chat(video_path):
|
| 144 |
last_frame, audio_path, predicted_label = predict_emotion(video_path)
|
| 145 |
predicted_emotion = emotion_dict[predicted_label]
|
| 146 |
+
|
| 147 |
+
# Connect to the chat server
|
| 148 |
+
client = sio.Client()
|
| 149 |
+
client.connect('http://localhost:5000/chat')
|
| 150 |
+
|
| 151 |
+
# Send the predicted emotion to the chat server
|
| 152 |
+
client.emit('message', {'client': 'Emotion Recognition', 'message': f'Predicted emotion: {predicted_emotion}'})
|
| 153 |
+
|
| 154 |
+
# Receive messages from the chat server
|
| 155 |
+
for msg in client.events:
|
| 156 |
+
print(msg)
|
| 157 |
+
|
| 158 |
+
return last_frame, audio_path, predicted_emotion, messages
|
| 159 |
|
| 160 |
iface = gr.Interface(
|
| 161 |
+
fn=predict_emotion_with_chat,
|
| 162 |
inputs=[
|
| 163 |
gr.Video(label="Upload a video")
|
| 164 |
],
|
| 165 |
outputs=[
|
| 166 |
gr.Image(label="Last Frame"),
|
| 167 |
+
gr.Audio(label="Audio"),
|
| 168 |
+
gr.Textbox(label="Predicted Emotion"),
|
| 169 |
+
gr.Chatbox(label="Chat")
|
| 170 |
],
|
| 171 |
+
title="Emotion Recognition with Chat",
|
| 172 |
+
description="Upload a video and get the predicted emotion. Chat with others in real-time."
|
| 173 |
)
|
| 174 |
|
| 175 |
+
# Start the Gradio interface and the chat server
|
| 176 |
+
from flask import Flask
|
| 177 |
+
app = Flask(__name__)
|
| 178 |
+
app.config['SECRET_KEY'] = 'secret'
|
| 179 |
+
app.socketio = sio.SocketIO(app)
|
| 180 |
+
run_chat_server(app)
|
| 181 |
iface.launch()
|