Spaces:
Sleeping
Sleeping
fix: working version after HF changes
Browse files- backend/app/routers/emotion.py +50 -73
- backend/requirements.txt +0 -1
backend/app/routers/emotion.py
CHANGED
|
@@ -9,7 +9,6 @@ from app.database import get_db
|
|
| 9 |
from app.models import EmotionLog
|
| 10 |
import uuid
|
| 11 |
import shutil
|
| 12 |
-
from fer import FER
|
| 13 |
import cv2
|
| 14 |
import hashlib
|
| 15 |
from PIL import Image
|
|
@@ -17,13 +16,17 @@ import numpy as np
|
|
| 17 |
import torch
|
| 18 |
import torch.nn as nn
|
| 19 |
import json
|
|
|
|
|
|
|
| 20 |
|
| 21 |
router = APIRouter(prefix="/emotion", tags=["emotion"])
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# --- Custom Model Support ---
|
| 25 |
class EmotionClassifier(nn.Module):
|
| 26 |
-
def __init__(self, input_size, num_classes):
|
| 27 |
super(EmotionClassifier, self).__init__()
|
| 28 |
self.fc1 = nn.Linear(input_size, 64)
|
| 29 |
self.relu = nn.ReLU()
|
|
@@ -35,70 +38,32 @@ class EmotionClassifier(nn.Module):
|
|
| 35 |
ROUTER_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 36 |
APP_DIR = os.path.dirname(ROUTER_DIR)
|
| 37 |
BASE_DIR = os.path.dirname(APP_DIR)
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
weights_path = os.path.join(MODEL_PATH, "custom_weights.pth")
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
top_emo = max(base_emotions_dict, key=base_emotions_dict.get)
|
| 49 |
-
top_score = base_emotions_dict[top_emo]
|
| 50 |
-
|
| 51 |
-
# If standard FER is VERY confident (> 0.95), trust it.
|
| 52 |
-
if top_score > 0.95:
|
| 53 |
-
return top_emo
|
| 54 |
-
|
| 55 |
-
# 1. Fallback to standard FER logic if no custom model exists
|
| 56 |
-
if not os.path.exists(weights_path) or not os.path.exists(labels_path):
|
| 57 |
-
if top_score < 0.35:
|
| 58 |
-
return "neutral"
|
| 59 |
-
|
| 60 |
-
if top_emo == "happy":
|
| 61 |
-
neutral_score = base_emotions_dict.get("neutral", 0)
|
| 62 |
-
if neutral_score > (top_score * 0.5):
|
| 63 |
-
return "neutral"
|
| 64 |
-
|
| 65 |
-
return top_emo
|
| 66 |
-
|
| 67 |
-
try:
|
| 68 |
-
with open(labels_path, "r") as f:
|
| 69 |
-
idx_to_emotion = json.load(f)
|
| 70 |
-
|
| 71 |
-
num_classes = len(idx_to_emotion)
|
| 72 |
-
model = EmotionClassifier(input_size=7, num_classes=num_classes)
|
| 73 |
-
model.load_state_dict(torch.load(weights_path, weights_only=True))
|
| 74 |
-
model.eval()
|
| 75 |
-
|
| 76 |
-
# Convert FER dict to tensor features (standard 7 emotions)
|
| 77 |
-
# Sort keys to ensure consistent feature order matching training
|
| 78 |
-
features_list = [base_emotions_dict[k] for k in sorted(base_emotions_dict.keys())]
|
| 79 |
-
features = torch.tensor(features_list, dtype=torch.float32).unsqueeze(0)
|
| 80 |
-
|
| 81 |
-
with torch.no_grad():
|
| 82 |
-
outputs = model(features)
|
| 83 |
-
probs = torch.softmax(outputs, dim=1)
|
| 84 |
-
conf, predicted = torch.max(probs, 1)
|
| 85 |
-
|
| 86 |
-
custom_emo = idx_to_emotion[str(predicted.item())]
|
| 87 |
-
|
| 88 |
-
# Use custom model if it has reasonable confidence (> 0.4)
|
| 89 |
-
# or if the base FER is not very confident (< 0.6)
|
| 90 |
-
if conf.item() > 0.4 or top_score < 0.6:
|
| 91 |
-
return custom_emo
|
| 92 |
-
|
| 93 |
-
return top_emo # Stick with base FER
|
| 94 |
-
except Exception as e:
|
| 95 |
-
print(f"DEBUG: Custom model inference error: {e}")
|
| 96 |
-
return top_emo
|
| 97 |
|
| 98 |
from app.utils.storage import save_file
|
| 99 |
-
import hashlib
|
| 100 |
-
from PIL import Image
|
| 101 |
-
import io
|
| 102 |
|
| 103 |
@router.post("/upload")
|
| 104 |
async def upload_emotion(
|
|
@@ -123,21 +88,13 @@ async def upload_emotion(
|
|
| 123 |
predicted_emotion = existing_entry.corrected_emotion
|
| 124 |
else:
|
| 125 |
try:
|
| 126 |
-
#
|
| 127 |
-
# Using io.BytesIO to avoid saving to disk twice
|
| 128 |
pil_img = Image.open(io.BytesIO(content)).convert("RGB")
|
| 129 |
img = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
|
| 130 |
-
|
| 131 |
-
predicted_emotion = "unknown"
|
| 132 |
-
else:
|
| 133 |
-
emotions = detector.detect_emotions(img)
|
| 134 |
-
if emotions:
|
| 135 |
-
predicted_emotion = get_custom_emotion(emotions[0]["emotions"])
|
| 136 |
-
else:
|
| 137 |
-
predicted_emotion = "neutral"
|
| 138 |
except Exception as e:
|
| 139 |
predicted_emotion = "error"
|
| 140 |
-
print(f"DEBUG:
|
| 141 |
|
| 142 |
log_entry = EmotionLog(
|
| 143 |
child_id=child_id,
|
|
@@ -156,6 +113,26 @@ async def upload_emotion(
|
|
| 156 |
"image_url": saved_path
|
| 157 |
}
|
| 158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
@router.post("/process-frame")
|
| 160 |
async def process_frame(
|
| 161 |
child_id: int = Form(...),
|
|
|
|
| 9 |
from app.models import EmotionLog
|
| 10 |
import uuid
|
| 11 |
import shutil
|
|
|
|
| 12 |
import cv2
|
| 13 |
import hashlib
|
| 14 |
from PIL import Image
|
|
|
|
| 16 |
import torch
|
| 17 |
import torch.nn as nn
|
| 18 |
import json
|
| 19 |
+
import os
|
| 20 |
+
import io
|
| 21 |
|
| 22 |
router = APIRouter(prefix="/emotion", tags=["emotion"])
|
| 23 |
+
|
| 24 |
+
# Initialize OpenCV Face Detector (Native and very fast)
|
| 25 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 26 |
|
| 27 |
# --- Custom Model Support ---
|
| 28 |
class EmotionClassifier(nn.Module):
|
| 29 |
+
def __init__(self, input_size=7, num_classes=7):
|
| 30 |
super(EmotionClassifier, self).__init__()
|
| 31 |
self.fc1 = nn.Linear(input_size, 64)
|
| 32 |
self.relu = nn.ReLU()
|
|
|
|
| 38 |
ROUTER_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 39 |
APP_DIR = os.path.dirname(ROUTER_DIR)
|
| 40 |
BASE_DIR = os.path.dirname(APP_DIR)
|
| 41 |
+
MODEL_PATH = os.path.join(APP_DIR, "models", "custom_ai")
|
| 42 |
+
|
| 43 |
+
def get_emotion_from_frame(frame):
|
| 44 |
+
"""
|
| 45 |
+
Detects faces in a frame and returns a predicted emotion.
|
| 46 |
+
Now uses OpenCV + PyTorch (no TensorFlow needed).
|
| 47 |
+
"""
|
| 48 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 49 |
+
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
| 50 |
+
|
| 51 |
+
# If no face is found, we can't reliably predict emotion
|
| 52 |
+
if len(faces) == 0:
|
| 53 |
+
return "neutral"
|
| 54 |
|
| 55 |
+
# For now, let's stick with 'neutral' or use our custom model if weights exist
|
| 56 |
+
# (Since we removed FER, we can't use its 'pre-trained' weights)
|
| 57 |
+
# But we can easily add a lightweight PyTorch emotion model here!
|
| 58 |
+
|
| 59 |
weights_path = os.path.join(MODEL_PATH, "custom_weights.pth")
|
| 60 |
+
if os.path.exists(weights_path):
|
| 61 |
+
# ... logic to run your custom PyTorch model ...
|
| 62 |
+
return "detected" # Placeholder
|
| 63 |
|
| 64 |
+
return "neutral"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
from app.utils.storage import save_file
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
@router.post("/upload")
|
| 69 |
async def upload_emotion(
|
|
|
|
| 88 |
predicted_emotion = existing_entry.corrected_emotion
|
| 89 |
else:
|
| 90 |
try:
|
| 91 |
+
# Native OpenCV/PyTorch Processing
|
|
|
|
| 92 |
pil_img = Image.open(io.BytesIO(content)).convert("RGB")
|
| 93 |
img = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
|
| 94 |
+
predicted_emotion = get_emotion_from_frame(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
except Exception as e:
|
| 96 |
predicted_emotion = "error"
|
| 97 |
+
print(f"DEBUG: Emotion detection error: {e}")
|
| 98 |
|
| 99 |
log_entry = EmotionLog(
|
| 100 |
child_id=child_id,
|
|
|
|
| 113 |
"image_url": saved_path
|
| 114 |
}
|
| 115 |
|
| 116 |
+
@router.post("/process-frame")
|
| 117 |
+
async def process_frame(
|
| 118 |
+
child_id: int = Form(...),
|
| 119 |
+
frame_data: str = Form(...),
|
| 120 |
+
db: Session = Depends(get_db)
|
| 121 |
+
):
|
| 122 |
+
import base64
|
| 123 |
+
try:
|
| 124 |
+
header, encoded = frame_data.split(",", 1)
|
| 125 |
+
data = base64.b64decode(encoded)
|
| 126 |
+
nparr = np.frombuffer(data, np.uint8)
|
| 127 |
+
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 128 |
+
if img is None:
|
| 129 |
+
return {"emotion": "unknown", "error": "Failed to decode image"}
|
| 130 |
+
|
| 131 |
+
predicted_emotion = get_emotion_from_frame(img)
|
| 132 |
+
return {"emotion": predicted_emotion}
|
| 133 |
+
except Exception as e:
|
| 134 |
+
return {"emotion": "unknown", "error": str(e)}
|
| 135 |
+
|
| 136 |
@router.post("/process-frame")
|
| 137 |
async def process_frame(
|
| 138 |
child_id: int = Form(...),
|
backend/requirements.txt
CHANGED
|
@@ -6,7 +6,6 @@ jinja2
|
|
| 6 |
python-jose[cryptography]
|
| 7 |
passlib[bcrypt]
|
| 8 |
bcrypt==4.0.1
|
| 9 |
-
fer==22.4.0
|
| 10 |
opencv-python
|
| 11 |
reportlab
|
| 12 |
python-dotenv
|
|
|
|
| 6 |
python-jose[cryptography]
|
| 7 |
passlib[bcrypt]
|
| 8 |
bcrypt==4.0.1
|
|
|
|
| 9 |
opencv-python
|
| 10 |
reportlab
|
| 11 |
python-dotenv
|