Spaces:
Build error
Build error
| 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.") | |