File size: 1,869 Bytes
8bf3411
 
 
51d6f5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8bf3411
 
51d6f5f
 
 
 
 
 
 
 
8bf3411
51d6f5f
 
 
8bf3411
51d6f5f
 
8bf3411
51d6f5f
 
8bf3411
51d6f5f
 
 
8bf3411
51d6f5f
 
 
 
8bf3411
51d6f5f
 
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
import streamlit as st
import cv2
from deepface import DeepFace
import numpy as np

# Streamlit app title
st.title("Face Emotion Detection App")

# Upload an image or video
uploaded_file = st.file_uploader("Upload an image or video", type=["jpg", "jpeg", "png", "mp4"])

# Product suggestions based on emotions
emotion_to_product = {
    "happy": "Product A - Happiness Booster",
    "sad": "Product B - Comfort Blanket",
    "angry": "Product C - Stress Relief Ball",
    "surprise": "Product D - Mystery Box",
    "fear": "Product E - Confidence Potion",
    "disgust": "Product F - Aroma Diffuser",
    "neutral": "Product G - Everyday Essentials",
    "Unknown": "Product H - General Item"
}

if uploaded_file is not None:
    # If the uploaded file is an image
    if uploaded_file.type in ["image/jpeg", "image/png"]:
        image = np.array(bytearray(uploaded_file.read()), dtype=np.uint8)
        image = cv2.imdecode(image, cv2.IMREAD_COLOR)
        
        # Convert frame to RGB for DeepFace
        rgb_frame = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        # Analyze emotions using DeepFace
        result = DeepFace.analyze(rgb_frame, actions=['emotion'], enforce_detection=False)
        emotion = result[0]['dominant_emotion']  # Extract dominant emotion

        # Display the uploaded image
        st.image(rgb_frame, channels="RGB")

        # Display the detected emotion
        st.subheader(f"Detected Emotion: {emotion}")

        # Display the suggested product
        product = emotion_to_product.get(emotion, "Product H - General Item")
        st.subheader(f"Recommended Product: {product}")

    # If the uploaded file is a video
    elif uploaded_file.type == "video/mp4":
        st.video(uploaded_file)
        # You can add code to analyze frames from the video if needed

else:
    st.info("Please upload an image or video file.")