Spaces:
Build error
Build error
Upload 4 files
Browse files- app.py +46 -0
- emotion_model .pt +3 -0
- requirements.txt +4 -0
- test_emotions_ood.py +51 -0
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torchvision import models, transforms
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# Modell laden
|
| 7 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 8 |
+
model = models.resnet18()
|
| 9 |
+
model.fc = torch.nn.Linear(model.fc.in_features, 9)
|
| 10 |
+
model.load_state_dict(torch.load("emotion_model.pt", map_location=device))
|
| 11 |
+
model = model.to(device)
|
| 12 |
+
model.eval()
|
| 13 |
+
|
| 14 |
+
# Labels
|
| 15 |
+
labels = ["happy", "sad", "angry", "surprised", "fear", "disgust", "neutral", "contempt", "unknown"]
|
| 16 |
+
|
| 17 |
+
# Transformation
|
| 18 |
+
transform = transforms.Compose([
|
| 19 |
+
transforms.Resize((224, 224)),
|
| 20 |
+
transforms.ToTensor()
|
| 21 |
+
])
|
| 22 |
+
|
| 23 |
+
def predict_emotion(image):
|
| 24 |
+
image = image.convert("RGB")
|
| 25 |
+
image = transform(image).unsqueeze(0).to(device)
|
| 26 |
+
|
| 27 |
+
with torch.no_grad():
|
| 28 |
+
outputs = model(image)
|
| 29 |
+
probs = torch.softmax(outputs, dim=1)
|
| 30 |
+
confidence, predicted = torch.max(probs, 1)
|
| 31 |
+
|
| 32 |
+
if confidence.item() < 0.7:
|
| 33 |
+
return "Unbekannte Emotion", f"{confidence.item()*100:.2f}%"
|
| 34 |
+
else:
|
| 35 |
+
return labels[predicted.item()], f"{confidence.item()*100:.2f}%"
|
| 36 |
+
|
| 37 |
+
# Gradio Interface
|
| 38 |
+
interface = gr.Interface(
|
| 39 |
+
fn=predict_emotion,
|
| 40 |
+
inputs=gr.Image(type="pil"),
|
| 41 |
+
outputs=["text", "text"],
|
| 42 |
+
title="Emotion Recognition App",
|
| 43 |
+
description="Lade ein Bild hoch und erkenne die Emotion."
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
interface.launch()
|
emotion_model .pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:71d4a41b5a1e1b3d14dd36e8db9e3773aa476891c9dfcc4c753912a25e7e6039
|
| 3 |
+
size 44802354
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
torch
|
| 3 |
+
torchvision
|
| 4 |
+
pillow
|
test_emotions_ood.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# test_emotions_ood.py
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
from torchvision import models, transforms
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Gerät wählen
|
| 9 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 10 |
+
|
| 11 |
+
# Modell definieren (gleiche Architektur wie beim Training)
|
| 12 |
+
model = models.resnet18()
|
| 13 |
+
model.fc = torch.nn.Linear(model.fc.in_features, 9) # 9 Emotionen
|
| 14 |
+
|
| 15 |
+
# Modellpfad und Laden
|
| 16 |
+
model_path = "emotion_model.pt"
|
| 17 |
+
model.load_state_dict(torch.load(model_path, map_location=device))
|
| 18 |
+
model = model.to(device)
|
| 19 |
+
model.eval()
|
| 20 |
+
|
| 21 |
+
# Label Mapping (Anpassen an deine Klassen)
|
| 22 |
+
labels = ["happy", "sad", "angry", "surprised", "fear", "disgust", "neutral", "contempt", "unknown"]
|
| 23 |
+
|
| 24 |
+
# Transformation für Bilder
|
| 25 |
+
transform = transforms.Compose([
|
| 26 |
+
transforms.Resize((224, 224)),
|
| 27 |
+
transforms.ToTensor()
|
| 28 |
+
])
|
| 29 |
+
|
| 30 |
+
def predict_image(image_path, threshold=0.7):
|
| 31 |
+
image = Image.open(image_path).convert("RGB")
|
| 32 |
+
image = transform(image).unsqueeze(0).to(device)
|
| 33 |
+
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
outputs = model(image)
|
| 36 |
+
probs = torch.softmax(outputs, dim=1)
|
| 37 |
+
confidence, predicted = torch.max(probs, 1)
|
| 38 |
+
|
| 39 |
+
if confidence.item() < threshold:
|
| 40 |
+
return "Unbekannte Emotion erkannt", confidence.item()
|
| 41 |
+
else:
|
| 42 |
+
return labels[predicted.item()], confidence.item()
|
| 43 |
+
|
| 44 |
+
# Beispielhafte Nutzung
|
| 45 |
+
test_image_path = "testbild.jpg" # <-- Dein Testbild hier
|
| 46 |
+
|
| 47 |
+
if os.path.exists(test_image_path):
|
| 48 |
+
emotion, conf = predict_image(test_image_path)
|
| 49 |
+
print(f"Erkannte Emotion: {emotion} (Confidence: {conf:.2f})")
|
| 50 |
+
else:
|
| 51 |
+
print(f"Bild {test_image_path} nicht gefunden.")
|