Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -40,11 +40,10 @@ class EmotionDetector:
|
|
40 |
self.face_detector = MTCNN(keep_all=False, device=self.device)
|
41 |
self.transform = transforms.Compose([
|
42 |
transforms.Resize((48, 48))
|
43 |
-
# No ToTensor() here – face already comes as tensor
|
44 |
])
|
45 |
self.softmax = torch.nn.Softmax(dim=1)
|
46 |
|
47 |
-
#
|
48 |
# self.model.load_state_dict(torch.load("emotion_model.pt", map_location=self.device))
|
49 |
|
50 |
def detect_emotions_video(self, video_path, sample_rate=30, max_size_mb=50):
|
@@ -79,8 +78,8 @@ class EmotionDetector:
|
|
79 |
continue
|
80 |
|
81 |
face_tensor = self.transform(face_tensor) # Resize
|
82 |
-
face_tensor = face_tensor.mean(dim=0, keepdim=True) #
|
83 |
-
face_tensor = face_tensor.unsqueeze(0).to(self.device) #
|
84 |
|
85 |
with torch.no_grad():
|
86 |
output = self.model(face_tensor)
|
@@ -132,23 +131,32 @@ class EmotionDetector:
|
|
132 |
def create_interface():
|
133 |
detector = EmotionDetector()
|
134 |
|
135 |
-
def process(
|
136 |
-
if
|
137 |
return None, "Please upload a video."
|
138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
|
140 |
return gr.Interface(
|
141 |
fn=process,
|
142 |
inputs=[
|
143 |
-
gr.Video(label="Upload Video"),
|
144 |
gr.Slider(minimum=1, maximum=60, step=1, value=30, label="Sample Rate (Frames)")
|
145 |
],
|
146 |
outputs=[
|
147 |
gr.Image(type="pil", label="Emotion Chart"),
|
148 |
gr.Textbox(label="Analysis Summary")
|
149 |
],
|
150 |
-
title="
|
151 |
-
description="
|
152 |
)
|
153 |
|
154 |
if __name__ == "__main__":
|
|
|
40 |
self.face_detector = MTCNN(keep_all=False, device=self.device)
|
41 |
self.transform = transforms.Compose([
|
42 |
transforms.Resize((48, 48))
|
|
|
43 |
])
|
44 |
self.softmax = torch.nn.Softmax(dim=1)
|
45 |
|
46 |
+
# Load pre-trained weights here if available
|
47 |
# self.model.load_state_dict(torch.load("emotion_model.pt", map_location=self.device))
|
48 |
|
49 |
def detect_emotions_video(self, video_path, sample_rate=30, max_size_mb=50):
|
|
|
78 |
continue
|
79 |
|
80 |
face_tensor = self.transform(face_tensor) # Resize
|
81 |
+
face_tensor = face_tensor.mean(dim=0, keepdim=True) # grayscale
|
82 |
+
face_tensor = face_tensor.unsqueeze(0).to(self.device) # batch + channel
|
83 |
|
84 |
with torch.no_grad():
|
85 |
output = self.model(face_tensor)
|
|
|
131 |
def create_interface():
|
132 |
detector = EmotionDetector()
|
133 |
|
134 |
+
def process(video_file, sample_rate):
|
135 |
+
if video_file is None:
|
136 |
return None, "Please upload a video."
|
137 |
+
|
138 |
+
try:
|
139 |
+
# Read and save to a temporary file
|
140 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp:
|
141 |
+
tmp.write(video_file.read())
|
142 |
+
tmp_path = tmp.name
|
143 |
+
|
144 |
+
return detector.detect_emotions_video(tmp_path, sample_rate)
|
145 |
+
except Exception as e:
|
146 |
+
return None, f"Error processing video: {str(e)}"
|
147 |
|
148 |
return gr.Interface(
|
149 |
fn=process,
|
150 |
inputs=[
|
151 |
+
gr.Video(label="Upload Video", type="file"), # file-like object
|
152 |
gr.Slider(minimum=1, maximum=60, step=1, value=30, label="Sample Rate (Frames)")
|
153 |
],
|
154 |
outputs=[
|
155 |
gr.Image(type="pil", label="Emotion Chart"),
|
156 |
gr.Textbox(label="Analysis Summary")
|
157 |
],
|
158 |
+
title="AI Emotion Detection",
|
159 |
+
description="Upload a video to analyze emotions over time."
|
160 |
)
|
161 |
|
162 |
if __name__ == "__main__":
|