File size: 2,715 Bytes
74315dc
 
 
999d54c
201eb03
89b169e
74315dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import streamlit as st
from transformers import pipeline
from PIL import Image
import cv2
import numpy as np
import torch

# --- App Title and Description ---
st.title("Real-Time Emotion Detection App")
st.write("""
This app uses a lightweight, pre-trained emotion detection model from Hugging Face to predict emotions 
from faces in an image. You can either upload an image or use your webcam to capture an image.
""")

# --- Load the Emotion Detection Model ---
# Cache the model loading so it isn’t reloaded on every app interaction.
@st.cache_resource(show_spinner=False)
def load_emotion_detector():
    # Loads the Hugging Face image-classification pipeline with the specified model.
    classifier = pipeline("image-classification", model="dima806/facial_emotions_image_detection")
    return classifier

classifier = load_emotion_detector()

# --- Sidebar: Select Input Method ---
st.sidebar.header("Select Input Method")
input_method = st.sidebar.radio("Choose one:", ["Upload an Image", "Capture with Webcam"])

# --- Process Image and Perform Inference ---
def predict_emotion(image: Image.Image):
    # Optionally, you can perform additional preprocessing (e.g., face detection or cropping) here.
    results = classifier(image)
    # The pipeline returns a list of dictionaries sorted by score.
    top_prediction = results[0]
    return top_prediction

# --- Main Section: Handling Input Methods ---

if input_method == "Upload an Image":
    uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
    if uploaded_file is not None:
        # Open the image file with PIL.
        image = Image.open(uploaded_file).convert("RGB")
        st.image(image, caption="Uploaded Image", use_column_width=True)
        prediction = predict_emotion(image)
        st.subheader("Prediction:")
        st.write(f"**Emotion:** {prediction['label']}")
        st.write(f"**Confidence:** {prediction['score']:.2f}")
        
elif input_method == "Capture with Webcam":
    # st.camera_input returns an image file-like object when a picture is taken.
    picture = st.camera_input("Capture an Image")
    if picture is not None:
        # Load image from the captured file.
        image = Image.open(picture).convert("RGB")
        st.image(image, caption="Captured Image", use_column_width=True)
        prediction = predict_emotion(image)
        st.subheader("Prediction:")
        st.write(f"**Emotion:** {prediction['label']}")
        st.write(f"**Confidence:** {prediction['score']:.2f}")

# --- Optional: Additional Instructions ---
st.write("""
*Note: For best results in real-time detection, consider focusing the camera on your face or uploading a clear face image.*
""")