Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +213 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,215 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import av
|
| 2 |
+
import cv2
|
| 3 |
+
import torch
|
| 4 |
import streamlit as st
|
| 5 |
+
from PIL import Image, ImageEnhance
|
| 6 |
+
from facenet_pytorch import MTCNN
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
from collections import deque, Counter
|
| 9 |
+
from streamlit_webrtc import webrtc_streamer, VideoProcessorBase
|
| 10 |
|
| 11 |
+
# =========================
|
| 12 |
+
# CONFIG
|
| 13 |
+
# =========================
|
| 14 |
+
MODEL_NAME = "trpakov/vit-face-expression"
|
| 15 |
+
|
| 16 |
+
FACE_CONFIDENCE = 0.90
|
| 17 |
+
EMOTION_CONFIDENCE = 0.25
|
| 18 |
+
PROCESS_EVERY_N_FRAMES = 2
|
| 19 |
+
SMOOTHING_FRAMES = 10
|
| 20 |
+
|
| 21 |
+
SAD_MIN_SCORE = 0.18
|
| 22 |
+
SAD_NEUTRAL_MARGIN = 0.15
|
| 23 |
+
|
| 24 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# =========================
|
| 28 |
+
# STREAMLIT UI
|
| 29 |
+
# =========================
|
| 30 |
+
st.set_page_config(page_title="Face Emotion Detection", page_icon="😊", layout="wide")
|
| 31 |
+
|
| 32 |
+
st.title("Real-Time Face Emotion Detection")
|
| 33 |
+
st.write("Detects face emotion: Happy, Sad or Neutral")
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# =========================
|
| 37 |
+
# CACHE MODELS
|
| 38 |
+
# =========================
|
| 39 |
+
@st.cache_resource
|
| 40 |
+
def load_models():
|
| 41 |
+
face_detector = MTCNN(
|
| 42 |
+
keep_all=True,
|
| 43 |
+
device=device,
|
| 44 |
+
min_face_size=60,
|
| 45 |
+
thresholds=[0.6, 0.7, 0.7]
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
emotion_model = pipeline(
|
| 49 |
+
"image-classification",
|
| 50 |
+
model=MODEL_NAME,
|
| 51 |
+
top_k=None,
|
| 52 |
+
device=0 if torch.cuda.is_available() else -1
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
return face_detector, emotion_model
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
face_detector, emotion_model = load_models()
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# =========================
|
| 62 |
+
# VIDEO PROCESSOR
|
| 63 |
+
# =========================
|
| 64 |
+
class EmotionProcessor(VideoProcessorBase):
|
| 65 |
+
def __init__(self):
|
| 66 |
+
self.history = deque(maxlen=SMOOTHING_FRAMES)
|
| 67 |
+
self.frame_count = 0
|
| 68 |
+
self.last_box = None
|
| 69 |
+
self.last_label = "No Face"
|
| 70 |
+
self.last_score = 0.0
|
| 71 |
+
|
| 72 |
+
def recv(self, frame):
|
| 73 |
+
img = frame.to_ndarray(format="bgr24")
|
| 74 |
+
|
| 75 |
+
img = cv2.flip(img, 1)
|
| 76 |
+
display = img.copy()
|
| 77 |
+
|
| 78 |
+
h, w, _ = img.shape
|
| 79 |
+
self.frame_count += 1
|
| 80 |
+
|
| 81 |
+
if self.frame_count % PROCESS_EVERY_N_FRAMES == 0:
|
| 82 |
+
rgb_frame = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 83 |
+
pil_frame = Image.fromarray(rgb_frame)
|
| 84 |
+
|
| 85 |
+
boxes, probs = face_detector.detect(pil_frame)
|
| 86 |
+
|
| 87 |
+
if boxes is None or probs is None:
|
| 88 |
+
self.last_box = None
|
| 89 |
+
self.last_label = "No Face"
|
| 90 |
+
self.last_score = 0.0
|
| 91 |
+
self.history.clear()
|
| 92 |
+
|
| 93 |
+
else:
|
| 94 |
+
valid_faces = []
|
| 95 |
+
|
| 96 |
+
for box, prob in zip(boxes, probs):
|
| 97 |
+
if prob >= FACE_CONFIDENCE:
|
| 98 |
+
x1, y1, x2, y2 = box.astype(int)
|
| 99 |
+
|
| 100 |
+
x1 = max(0, x1)
|
| 101 |
+
y1 = max(0, y1)
|
| 102 |
+
x2 = min(w, x2)
|
| 103 |
+
y2 = min(h, y2)
|
| 104 |
+
|
| 105 |
+
area = (x2 - x1) * (y2 - y1)
|
| 106 |
+
|
| 107 |
+
if area > 0:
|
| 108 |
+
valid_faces.append((area, x1, y1, x2, y2, prob))
|
| 109 |
+
|
| 110 |
+
if not valid_faces:
|
| 111 |
+
self.last_box = None
|
| 112 |
+
self.last_label = "No Face"
|
| 113 |
+
self.last_score = 0.0
|
| 114 |
+
self.history.clear()
|
| 115 |
+
|
| 116 |
+
else:
|
| 117 |
+
valid_faces.sort(reverse=True)
|
| 118 |
+
_, x1, y1, x2, y2, face_prob = valid_faces[0]
|
| 119 |
+
|
| 120 |
+
pad_x = int((x2 - x1) * 0.20)
|
| 121 |
+
pad_y = int((y2 - y1) * 0.25)
|
| 122 |
+
|
| 123 |
+
x1p = max(0, x1 - pad_x)
|
| 124 |
+
y1p = max(0, y1 - pad_y)
|
| 125 |
+
x2p = min(w, x2 + pad_x)
|
| 126 |
+
y2p = min(h, y2 + pad_y)
|
| 127 |
+
|
| 128 |
+
face_crop = rgb_frame[y1p:y2p, x1p:x2p]
|
| 129 |
+
|
| 130 |
+
if face_crop.size != 0:
|
| 131 |
+
face_img = Image.fromarray(face_crop).resize((224, 224))
|
| 132 |
+
|
| 133 |
+
face_img = ImageEnhance.Contrast(face_img).enhance(1.25)
|
| 134 |
+
face_img = ImageEnhance.Sharpness(face_img).enhance(1.15)
|
| 135 |
+
|
| 136 |
+
preds = emotion_model(face_img)
|
| 137 |
+
|
| 138 |
+
scores = {
|
| 139 |
+
p["label"].lower(): float(p["score"])
|
| 140 |
+
for p in preds
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
happy = scores.get("happy", 0.0)
|
| 144 |
+
sad = scores.get("sad", 0.0)
|
| 145 |
+
neutral = scores.get("neutral", 0.0)
|
| 146 |
+
|
| 147 |
+
required_scores = {
|
| 148 |
+
"happy": happy,
|
| 149 |
+
"sad": sad,
|
| 150 |
+
"neutral": neutral
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
label = max(required_scores, key=required_scores.get)
|
| 154 |
+
score = required_scores[label]
|
| 155 |
+
|
| 156 |
+
if sad >= SAD_MIN_SCORE and sad >= neutral - SAD_NEUTRAL_MARGIN:
|
| 157 |
+
label = "sad"
|
| 158 |
+
score = sad
|
| 159 |
+
|
| 160 |
+
self.history.append(label)
|
| 161 |
+
final_label = Counter(self.history).most_common(1)[0][0]
|
| 162 |
+
|
| 163 |
+
self.last_box = (x1p, y1p, x2p, y2p)
|
| 164 |
+
self.last_label = final_label
|
| 165 |
+
self.last_score = score
|
| 166 |
+
|
| 167 |
+
if self.last_box is not None:
|
| 168 |
+
x1, y1, x2, y2 = self.last_box
|
| 169 |
+
|
| 170 |
+
if self.last_label == "happy":
|
| 171 |
+
color = (0, 255, 0)
|
| 172 |
+
elif self.last_label == "sad":
|
| 173 |
+
color = (255, 0, 0)
|
| 174 |
+
elif self.last_label == "neutral":
|
| 175 |
+
color = (255, 255, 0)
|
| 176 |
+
else:
|
| 177 |
+
color = (0, 255, 255)
|
| 178 |
+
|
| 179 |
+
cv2.rectangle(display, (x1, y1), (x2, y2), color, 2)
|
| 180 |
+
|
| 181 |
+
cv2.putText(
|
| 182 |
+
display,
|
| 183 |
+
f"{self.last_label.upper()} ({self.last_score:.2f})",
|
| 184 |
+
(x1, max(30, y1 - 10)),
|
| 185 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
| 186 |
+
0.75,
|
| 187 |
+
color,
|
| 188 |
+
2
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
else:
|
| 192 |
+
cv2.putText(
|
| 193 |
+
display,
|
| 194 |
+
"NO FACE DETECTED",
|
| 195 |
+
(30, 50),
|
| 196 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
| 197 |
+
0.9,
|
| 198 |
+
(0, 0, 255),
|
| 199 |
+
2
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
return av.VideoFrame.from_ndarray(display, format="bgr24")
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
# =========================
|
| 206 |
+
# START CAMERA
|
| 207 |
+
# =========================
|
| 208 |
+
webrtc_streamer(
|
| 209 |
+
key="emotion-detection",
|
| 210 |
+
video_processor_factory=EmotionProcessor,
|
| 211 |
+
media_stream_constraints={
|
| 212 |
+
"video": True,
|
| 213 |
+
"audio": False
|
| 214 |
+
}
|
| 215 |
+
)
|