THP2903 commited on
Commit
1a638f1
·
verified ·
1 Parent(s): f40b122

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -16
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import streamlit as st
2
  import torch as pt
3
  import torchaudio
4
  import cv2
@@ -7,7 +7,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
- from PIL import Image
11
 
12
  def convert_video_to_audio_moviepy(video_file, output_ext="wav"):
13
  """Converts video to audio using MoviePy library that uses `ffmpeg` under the hood"""
@@ -31,11 +30,15 @@ def process_video_audio(video_path):
31
  face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
32
 
33
  if len(wav[0]) > 261540:
 
34
  train_audio_wave[0, :] = wav[0][:261540]
35
  else:
 
36
  train_audio_wave[0, :len(wav[0])] = wav[0][:]
37
  train_audio_cnn[0, :, :, 0] = mfcc(train_audio_wave[0])
38
 
 
 
39
  cap = cv2.VideoCapture(video_path)
40
  frame_idx = 0
41
  last_frame = None
@@ -76,23 +79,24 @@ def predict_emotion(video_path):
76
  predicted_label = np.argmax(predictions)
77
  return last_frame, audio_path, predicted_label
78
 
79
- def predict_emotion_streamlit(video_path):
80
  emotion_dict = {0: 'neutral', 1: 'calm', 2: 'happy', 3: 'sad', 4: 'angry', 5: 'fearful'}
81
  last_frame, audio_path, predicted_label = predict_emotion(video_path)
82
  predicted_emotion = emotion_dict[predicted_label]
83
  return last_frame, audio_path, predicted_emotion
84
 
85
- st.title("Emotion Recognition from Video")
86
- st.write("Upload a video and get the predicted emotion.")
87
-
88
- video_file = st.file_uploader("Upload a video", type=["mp4", "avi", "mov"])
 
 
 
 
 
 
 
 
 
89
 
90
- if video_file is not None:
91
- with open("uploaded_video.mp4", "wb") as f:
92
- f.write(video_file.getbuffer())
93
-
94
- last_frame, audio_path, predicted_emotion = predict_emotion_streamlit("uploaded_video.mp4")
95
-
96
- st.image(last_frame, caption="Last Frame", use_column_width=True)
97
- st.audio(audio_path, format="audio/wav")
98
- st.text(f"Predicted Emotion: {predicted_emotion}")
 
1
+ import gradio as gr
2
  import torch as pt
3
  import torchaudio
4
  import cv2
 
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"""
 
30
  face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
31
 
32
  if len(wav[0]) > 261540:
33
+ print(wav.shape)
34
  train_audio_wave[0, :] = wav[0][:261540]
35
  else:
36
+ print(wav.shape)
37
  train_audio_wave[0, :len(wav[0])] = wav[0][:]
38
  train_audio_cnn[0, :, :, 0] = mfcc(train_audio_wave[0])
39
 
40
+ print(train_audio_cnn[0].shape)
41
+
42
  cap = cv2.VideoCapture(video_path)
43
  frame_idx = 0
44
  last_frame = None
 
79
  predicted_label = np.argmax(predictions)
80
  return last_frame, audio_path, predicted_label
81
 
82
+ def predict_emotion_gradio(video_path):
83
  emotion_dict = {0: 'neutral', 1: 'calm', 2: 'happy', 3: 'sad', 4: 'angry', 5: 'fearful'}
84
  last_frame, audio_path, predicted_label = predict_emotion(video_path)
85
  predicted_emotion = emotion_dict[predicted_label]
86
  return last_frame, audio_path, predicted_emotion
87
 
88
+ iface = gr.Interface(
89
+ fn=predict_emotion_gradio,
90
+ inputs=[
91
+ gr.Video(label="Upload a video")
92
+ ],
93
+ outputs=[
94
+ gr.Image(label="Last Frame"),
95
+ gr.Audio(label = "Audio"),
96
+ gr.Textbox(label="Predicted Emotion")
97
+ ],
98
+ title="Emotion Recognition from Video",
99
+ description="Upload a video and get the predicted emotion."
100
+ )
101
 
102
+ iface.launch()