Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,64 +11,54 @@ emotions = ['HAPPY', 'SAD', 'SURPRISED']
|
|
| 11 |
with open('model.pkl', 'rb') as f:
|
| 12 |
model = pickle.load(f)
|
| 13 |
|
| 14 |
-
def predict_emotion(image,
|
| 15 |
-
if image is None:
|
| 16 |
-
return {"Error": 1.0}, None, "⚠ Please upload an image."
|
| 17 |
-
|
| 18 |
# Convert PIL image to OpenCV format
|
| 19 |
img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 20 |
|
| 21 |
# Extract landmarks
|
| 22 |
-
face_landmarks = get_face_landmarks(img, draw=
|
| 23 |
|
| 24 |
if face_landmarks is None or len(face_landmarks) == 0:
|
| 25 |
return {"No face detected": 1.0}, None, "⚠ No face detected in the image."
|
| 26 |
|
| 27 |
-
# Predict emotion
|
| 28 |
output = model.predict([face_landmarks])
|
| 29 |
predicted_emotion = emotions[int(output[0])]
|
| 30 |
|
| 31 |
-
# Confidence scores
|
| 32 |
if hasattr(model, "predict_proba"):
|
| 33 |
probs = model.predict_proba([face_landmarks])[0]
|
| 34 |
confidence_dict = {emotions[i]: float(probs[i]) for i in range(len(emotions))}
|
| 35 |
else:
|
| 36 |
confidence_dict = {predicted_emotion: 1.0}
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
# annotated_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 44 |
-
# else:
|
| 45 |
-
# annotated_img = None # No annotation if user chooses "None"
|
| 46 |
|
| 47 |
-
|
| 48 |
|
| 49 |
# Example images
|
| 50 |
-
examples = [["examples/happy.png"], ["examples/sad.png"], ["examples/surprised.png"]]
|
| 51 |
-
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
draw_option.change(fn=predict_emotion, inputs=[image_input, draw_option], outputs=[label_output, image_output, status_output])
|
| 70 |
-
|
| 71 |
-
gr.Examples(examples=examples, inputs=[image_input])
|
| 72 |
|
| 73 |
if __name__ == "__main__":
|
| 74 |
demo.launch()
|
|
|
|
| 11 |
with open('model.pkl', 'rb') as f:
|
| 12 |
model = pickle.load(f)
|
| 13 |
|
| 14 |
+
def predict_emotion(image, show_landmarks):
|
|
|
|
|
|
|
|
|
|
| 15 |
# Convert PIL image to OpenCV format
|
| 16 |
img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 17 |
|
| 18 |
# Extract landmarks
|
| 19 |
+
face_landmarks = get_face_landmarks(img, draw=show_landmarks, static_image_mode=True)
|
| 20 |
|
| 21 |
if face_landmarks is None or len(face_landmarks) == 0:
|
| 22 |
return {"No face detected": 1.0}, None, "⚠ No face detected in the image."
|
| 23 |
|
| 24 |
+
# Predict emotion and probabilities
|
| 25 |
output = model.predict([face_landmarks])
|
| 26 |
predicted_emotion = emotions[int(output[0])]
|
| 27 |
|
|
|
|
| 28 |
if hasattr(model, "predict_proba"):
|
| 29 |
probs = model.predict_proba([face_landmarks])[0]
|
| 30 |
confidence_dict = {emotions[i]: float(probs[i]) for i in range(len(emotions))}
|
| 31 |
else:
|
| 32 |
confidence_dict = {predicted_emotion: 1.0}
|
| 33 |
|
| 34 |
+
# If user wants landmarks, `get_face_landmarks` draws them, otherwise original image
|
| 35 |
+
if show_landmarks:
|
| 36 |
+
annotated_img = img
|
| 37 |
+
else:
|
| 38 |
+
annotated_img = None # Don't show any image if landmarks not requested
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
return confidence_dict, cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB) if annotated_img is not None else None, f"✅ Detected emotion: {predicted_emotion}"
|
| 41 |
|
| 42 |
# Example images
|
| 43 |
+
examples = [["examples/happy.png", True], ["examples/sad.png", True], ["examples/surprised.png", True]]
|
| 44 |
+
|
| 45 |
+
# Gradio Interface
|
| 46 |
+
demo = gr.Interface(
|
| 47 |
+
fn=predict_emotion,
|
| 48 |
+
inputs=[
|
| 49 |
+
gr.Image(type="pil", label="Upload Image or Use Webcam", sources=["upload", "webcam"]),
|
| 50 |
+
gr.Checkbox(label="Show Landmarks", value=False)
|
| 51 |
+
],
|
| 52 |
+
outputs=[
|
| 53 |
+
gr.Label(num_top_classes=3, label="Predicted Emotion & Confidence"),
|
| 54 |
+
gr.Image(type="numpy", label="Landmark Image"), # Only shown if checkbox is True
|
| 55 |
+
gr.Textbox(label="Status", interactive=False)
|
| 56 |
+
],
|
| 57 |
+
title="Emotion Detector",
|
| 58 |
+
description="Upload an image or use webcam to detect emotions (HAPPY, SAD, SURPRISED). Check 'Show Landmarks' to visualize facial landmarks.",
|
| 59 |
+
examples=examples,
|
| 60 |
+
theme="default"
|
| 61 |
+
)
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|