yunusajib commited on
Commit
bf93411
·
verified ·
1 Parent(s): 6e0a659

update app.py v1

Browse files
Files changed (1) hide show
  1. app.py +17 -31
app.py CHANGED
@@ -2,49 +2,39 @@ import gradio as gr
2
  import numpy as np
3
  import cv2
4
  import torch
5
- import torchvision.transforms as transforms
6
- from fer import FER
7
- import librosa
8
- from python_speech_features import mfcc
9
  import pandas as pd
10
  from datetime import datetime
11
  import time
12
  from transformers import pipeline
 
 
 
13
 
14
  # Initialize models
15
- emotion_detector = FER(mtcnn=True) # Facial expression recognition
16
  voice_classifier = pipeline("audio-classification", model="superb/hubert-base-superb-er")
17
 
18
  # Global variables to store results
19
  emotion_history = []
20
- current_emotions = {"face": "Neutral", "voice": "Neutral"}
21
  last_update_time = time.time()
22
 
23
- # Preprocessing for face detection
24
- transform = transforms.Compose([
25
- transforms.ToPILImage(),
26
- transforms.Resize((48, 48)),
27
- transforms.Grayscale(),
28
- transforms.ToTensor(),
29
- ])
30
-
31
  def analyze_face(frame):
32
- """Analyze facial expressions in the frame"""
33
  try:
34
- # Convert frame to RGB (FER expects RGB)
35
  rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
36
 
37
  # Detect emotions
38
- results = emotion_detector.detect_emotions(rgb_frame)
39
 
40
- if results:
41
- emotions = results[0]['emotions']
42
  dominant_emotion = max(emotions, key=emotions.get)
43
  return dominant_emotion, emotions
44
- return "Neutral", {"angry": 0, "disgust": 0, "fear": 0, "happy": 0, "sad": 0, "surprise": 0, "neutral": 1}
45
  except Exception as e:
46
  print(f"Face analysis error: {e}")
47
- return "Neutral", {"angry": 0, "disgust": 0, "fear": 0, "happy": 0, "sad": 0, "surprise": 0, "neutral": 1}
48
 
49
  def analyze_voice(audio):
50
  """Analyze voice tone from audio"""
@@ -117,10 +107,10 @@ def get_practitioner_advice(face_emotion, voice_emotion):
117
  advice.append("Patient appears distressed. Consider speaking more slowly and with reassurance.")
118
  elif face_emotion == "angry":
119
  advice.append("Patient seems frustrated. Acknowledge their concerns and maintain calm demeanor.")
120
- elif face_emotion == "confused":
121
- advice.append("Patient may not understand. Consider rephrasing or providing more explanation.")
122
- elif face_emotion == "pain":
123
- advice.append("Patient appears to be in pain. Consider asking about discomfort.")
124
 
125
  # Voice emotion advice
126
  if voice_emotion in ["sad", "fear"]:
@@ -140,7 +130,7 @@ def process_input(video, audio):
140
  frame = cv2.cvtColor(video, cv2.COLOR_RGB2BGR)
141
  face_emotion, face_details = analyze_face(frame)
142
  else:
143
- face_emotion, face_details = "Neutral", {}
144
 
145
  # Process audio
146
  if audio is not None:
@@ -212,8 +202,4 @@ with gr.Blocks(title="Patient Emotion Recognition", theme="soft") as demo:
212
  submit_btn.click(
213
  process_input,
214
  inputs=[video_input, audio_input],
215
- outputs=[current_face, current_voice, timeline_output, advice_output, face_details, voice_details]
216
- )
217
-
218
- if __name__ == "__main__":
219
- demo.launch(debug=True)
 
2
  import numpy as np
3
  import cv2
4
  import torch
 
 
 
 
5
  import pandas as pd
6
  from datetime import datetime
7
  import time
8
  from transformers import pipeline
9
+ from deepface import DeepFace
10
+ import librosa
11
+ from python_speech_features import mfcc
12
 
13
  # Initialize models
 
14
  voice_classifier = pipeline("audio-classification", model="superb/hubert-base-superb-er")
15
 
16
  # Global variables to store results
17
  emotion_history = []
18
+ current_emotions = {"face": "neutral", "voice": "neutral"}
19
  last_update_time = time.time()
20
 
 
 
 
 
 
 
 
 
21
  def analyze_face(frame):
22
+ """Analyze facial expressions in the frame using DeepFace"""
23
  try:
24
+ # Convert frame to RGB (DeepFace expects RGB)
25
  rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
26
 
27
  # Detect emotions
28
+ result = DeepFace.analyze(rgb_frame, actions=['emotion'], enforce_detection=False)
29
 
30
+ if result and isinstance(result, list):
31
+ emotions = result[0]['emotion']
32
  dominant_emotion = max(emotions, key=emotions.get)
33
  return dominant_emotion, emotions
34
+ return "neutral", {"angry": 0, "disgust": 0, "fear": 0, "happy": 0, "sad": 0, "surprise": 0, "neutral": 1}
35
  except Exception as e:
36
  print(f"Face analysis error: {e}")
37
+ return "neutral", {"angry": 0, "disgust": 0, "fear": 0, "happy": 0, "sad": 0, "surprise": 0, "neutral": 1}
38
 
39
  def analyze_voice(audio):
40
  """Analyze voice tone from audio"""
 
107
  advice.append("Patient appears distressed. Consider speaking more slowly and with reassurance.")
108
  elif face_emotion == "angry":
109
  advice.append("Patient seems frustrated. Acknowledge their concerns and maintain calm demeanor.")
110
+ elif face_emotion == "disgust":
111
+ advice.append("Patient may be uncomfortable. Check if they're experiencing any discomfort.")
112
+ elif face_emotion == "surprise":
113
+ advice.append("Patient seems surprised. Ensure they understand all information.")
114
 
115
  # Voice emotion advice
116
  if voice_emotion in ["sad", "fear"]:
 
130
  frame = cv2.cvtColor(video, cv2.COLOR_RGB2BGR)
131
  face_emotion, face_details = analyze_face(frame)
132
  else:
133
+ face_emotion, face_details = "neutral", {}
134
 
135
  # Process audio
136
  if audio is not None:
 
202
  submit_btn.click(
203
  process_input,
204
  inputs=[video_input, audio_input],
205
+