Fix: switch to live webcam streaming with gr.Blocks
Browse files
app.py
CHANGED
|
@@ -15,29 +15,43 @@ emotion_labels = {
|
|
| 15 |
4: "Surprised"
|
| 16 |
}
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
def detect_emotion(
|
| 22 |
"""
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
"""
|
| 26 |
-
if
|
| 27 |
return None
|
| 28 |
|
| 29 |
-
# Gradio
|
| 30 |
-
|
| 31 |
-
gray
|
| 32 |
|
| 33 |
faces = face_cascade.detectMultiScale(
|
| 34 |
-
gray,
|
|
|
|
|
|
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
if len(faces) == 0:
|
| 38 |
-
# No face detected β return original image with a message
|
| 39 |
cv2.putText(
|
| 40 |
-
|
| 41 |
"No face detected",
|
| 42 |
(20, 40),
|
| 43 |
cv2.FONT_HERSHEY_SIMPLEX,
|
|
@@ -47,48 +61,67 @@ def detect_emotion(image):
|
|
| 47 |
)
|
| 48 |
else:
|
| 49 |
for (x, y, w, h) in faces:
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
|
|
|
| 57 |
confidence = float(np.max(prediction)) * 100
|
|
|
|
| 58 |
|
| 59 |
# Draw bounding box
|
| 60 |
-
cv2.rectangle(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
# Draw
|
| 63 |
-
label = f"{emotion} ({confidence:.1f}%)"
|
| 64 |
cv2.putText(
|
| 65 |
-
|
| 66 |
label,
|
| 67 |
-
(x, y -
|
| 68 |
cv2.FONT_HERSHEY_SIMPLEX,
|
| 69 |
-
0.
|
| 70 |
-
(0,
|
| 71 |
2
|
| 72 |
)
|
| 73 |
|
| 74 |
-
# Convert back to RGB for Gradio
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
"
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
| 94 |
demo.launch()
|
|
|
|
| 15 |
4: "Surprised"
|
| 16 |
}
|
| 17 |
|
| 18 |
+
# Emotion colors (BGR) β each emotion gets a distinct color
|
| 19 |
+
emotion_colors = {
|
| 20 |
+
"Angry": (0, 0, 255), # Red
|
| 21 |
+
"Happy": (0, 255, 0), # Green
|
| 22 |
+
"Neutral": (255, 255, 0), # Cyan
|
| 23 |
+
"Sad": (255, 0, 0), # Blue
|
| 24 |
+
"Surprised": (0, 165, 255) # Orange
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
# Load face detector via OpenCV built-in path
|
| 28 |
+
face_cascade = cv2.CascadeClassifier(
|
| 29 |
+
cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
|
| 30 |
+
)
|
| 31 |
|
| 32 |
+
def detect_emotion(frame):
|
| 33 |
"""
|
| 34 |
+
Called on every webcam frame by Gradio's streaming interface.
|
| 35 |
+
Detects faces, predicts emotion, draws bounding box + label.
|
| 36 |
+
Returns the annotated frame (RGB).
|
| 37 |
"""
|
| 38 |
+
if frame is None:
|
| 39 |
return None
|
| 40 |
|
| 41 |
+
# Gradio streams frames as RGB numpy arrays β convert to BGR for OpenCV
|
| 42 |
+
frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
| 43 |
+
gray = cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2GRAY)
|
| 44 |
|
| 45 |
faces = face_cascade.detectMultiScale(
|
| 46 |
+
gray,
|
| 47 |
+
scaleFactor=1.3,
|
| 48 |
+
minNeighbors=5,
|
| 49 |
+
minSize=(30, 30)
|
| 50 |
)
|
| 51 |
|
| 52 |
if len(faces) == 0:
|
|
|
|
| 53 |
cv2.putText(
|
| 54 |
+
frame_bgr,
|
| 55 |
"No face detected",
|
| 56 |
(20, 40),
|
| 57 |
cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
| 61 |
)
|
| 62 |
else:
|
| 63 |
for (x, y, w, h) in faces:
|
| 64 |
+
# Crop and preprocess face
|
| 65 |
+
face_roi = gray[y:y+h, x:x+w]
|
| 66 |
+
face_roi = cv2.resize(face_roi, (48, 48))
|
| 67 |
+
face_roi = face_roi / 255.0
|
| 68 |
+
face_roi = face_roi.reshape(1, 48, 48, 1)
|
| 69 |
|
| 70 |
+
# Predict
|
| 71 |
+
prediction = model.predict(face_roi, verbose=0)
|
| 72 |
+
emotion = emotion_labels[np.argmax(prediction)]
|
| 73 |
confidence = float(np.max(prediction)) * 100
|
| 74 |
+
color = emotion_colors[emotion]
|
| 75 |
|
| 76 |
# Draw bounding box
|
| 77 |
+
cv2.rectangle(frame_bgr, (x, y), (x+w, y+h), color, 2)
|
| 78 |
+
|
| 79 |
+
# Draw filled label background for readability
|
| 80 |
+
label = f"{emotion} {confidence:.1f}%"
|
| 81 |
+
(tw, th), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.8, 2)
|
| 82 |
+
cv2.rectangle(frame_bgr, (x, y - th - 12), (x + tw + 6, y), color, -1)
|
| 83 |
|
| 84 |
+
# Draw label text in black over the colored background
|
|
|
|
| 85 |
cv2.putText(
|
| 86 |
+
frame_bgr,
|
| 87 |
label,
|
| 88 |
+
(x + 3, y - 5),
|
| 89 |
cv2.FONT_HERSHEY_SIMPLEX,
|
| 90 |
+
0.8,
|
| 91 |
+
(0, 0, 0),
|
| 92 |
2
|
| 93 |
)
|
| 94 |
|
| 95 |
+
# Convert back to RGB for Gradio
|
| 96 |
+
return cv2.cvtColor(frame_bgr, cv2.COLOR_BGR2RGB)
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
# Gradio Streaming Interface
|
| 100 |
+
with gr.Blocks(title="Real-Time Emotion Detection") as demo:
|
| 101 |
+
|
| 102 |
+
gr.Markdown(
|
| 103 |
+
"""
|
| 104 |
+
# π Real-Time Emotion Detection
|
| 105 |
+
Allow camera access and click **Start** to begin live detection.
|
| 106 |
+
Detectable emotions: π Angry | π Happy | π Neutral | π’ Sad | π² Surprised
|
| 107 |
+
"""
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
with gr.Row():
|
| 111 |
+
input_cam = gr.Image(sources=["webcam"], streaming=True, label="π· Live Webcam")
|
| 112 |
+
output_img = gr.Image(label="π― Detected Emotion")
|
| 113 |
+
|
| 114 |
+
gr.Markdown(
|
| 115 |
+
"""
|
| 116 |
+
---
|
| 117 |
+
**How it works:** Each webcam frame is processed by a CNN model trained on 48Γ48 grayscale face images.
|
| 118 |
+
Haar Cascade detects faces, then the model predicts the emotion with confidence score.
|
| 119 |
+
"""
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
# Stream every frame from webcam β detect_emotion β output
|
| 123 |
+
input_cam.stream(fn=detect_emotion, inputs=input_cam, outputs=output_img)
|
| 124 |
+
|
| 125 |
|
| 126 |
if __name__ == "__main__":
|
| 127 |
demo.launch()
|