CoolFace
Apppublic

Vidit123/Emotion-recognition

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py353 linesDownload Raw Back to root
1import os2os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'python'3 4import streamlit as st5import cv26import numpy as np7from deepface import DeepFace8from PIL import Image, ImageDraw, ImageFont9import pandas as pd10import time11 12# Page config13st.set_page_config(14    page_title="๐ŸŽญ Real-Time Emotion Detection", 15    page_icon="๐ŸŽญ",16    layout="wide"17)18 19st.title("๐ŸŽญ Real-Time Emotion Detection")20st.markdown("Upload an image or use your camera to detect emotions in faces!")21 22# Load face cascade23@st.cache_resource24def load_face_cascade():25    return cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')26 27face_cascade = load_face_cascade()28 29# Emotion colors for visualization30emotion_colors = {31    'angry': (255, 0, 0),32    'disgust': (0, 128, 0), 33    'fear': (128, 0, 128),34    'happy': (0, 255, 0),35    'sad': (0, 0, 255),36    'surprise': (255, 255, 0),37    'neutral': (128, 128, 128)38}39 40def detect_emotions_in_image(image):41    """Detect emotions in uploaded image"""42    # Convert PIL to OpenCV format43    opencv_image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)44    gray = cv2.cvtColor(opencv_image, cv2.COLOR_BGR2GRAY)45    46    # Detect faces47    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))48    49    results = []50    annotated_image = opencv_image.copy()51    52    for (x, y, w, h) in faces:53        # Extract face ROI54        face_roi = opencv_image[y:y + h, x:x + w]55        rgb_roi = cv2.cvtColor(face_roi, cv2.COLOR_BGR2RGB)56        57        try:58            # Analyze emotion59            result = DeepFace.analyze(rgb_roi, actions=['emotion'], enforce_detection=False)60            emotion = result[0]['dominant_emotion']61            confidence = result[0]['emotion'][emotion]62            63            # Store results64            results.append({65                'Face': len(results) + 1,66                'Emotion': emotion.title(),67                'Confidence': f"{confidence:.1f}%"68            })69            70            # Draw rectangle and text71            color = emotion_colors.get(emotion, (255, 0, 0))72            cv2.rectangle(annotated_image, (x, y), (x + w, y + h), color, 2)73            cv2.putText(annotated_image, f"{emotion} ({confidence:.1f}%)", 74                       (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)75            76        except Exception as e:77            st.warning(f"Could not analyze face {len(results) + 1}: {str(e)}")78    79    # Convert back to RGB for display80    final_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)81    return Image.fromarray(final_image), results82 83def detect_emotions_realtime_frame(frame):84    """Detect emotions in a single frame for real-time processing"""85    # Mirror the frame horizontally for selfie view86    frame = cv2.flip(frame, 1)87    88    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)89    90    # Detect faces91    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))92    93    results = []94    annotated_frame = frame.copy()95    96    for (x, y, w, h) in faces:97        # Extract face ROI98        face_roi = frame[y:y + h, x:x + w]99        rgb_roi = cv2.cvtColor(face_roi, cv2.COLOR_BGR2RGB)100        101        try:102            # Analyze emotion103            result = DeepFace.analyze(rgb_roi, actions=['emotion'], enforce_detection=False)104            emotion = result[0]['dominant_emotion']105            confidence = result[0]['emotion'][emotion]106            107            # Store results108            results.append({109                'emotion': emotion,110                'confidence': confidence,111                'bbox': (x, y, w, h)112            })113            114            # Draw thick red rectangle around face115            cv2.rectangle(annotated_frame, (x, y), (x + w, y + h), (0, 0, 255), 3)116            117            # Draw emotion text above the rectangle118            font = cv2.FONT_HERSHEY_SIMPLEX119            font_scale = 1.2120            font_thickness = 2121            text_color = (0, 0, 255)  # Red color122            123            # Get text size to position it properly124            text = emotion125            text_size = cv2.getTextSize(text, font, font_scale, font_thickness)[0]126            text_x = x127            text_y = y - 15128            129            # Draw text background for better visibility130            cv2.rectangle(annotated_frame, (text_x, text_y - text_size[1] - 10), 131                         (text_x + text_size[0] + 10, text_y + 5), (255, 255, 255), -1)132            133            # Draw the emotion text134            cv2.putText(annotated_frame, text, (text_x + 5, text_y - 5), 135                       font, font_scale, text_color, font_thickness)136            137        except Exception:138            # Skip failed detections in real-time to maintain performance139            pass140    141    return annotated_frame, results142 143# Main interface with tabs144tab1, tab2, tab3 = st.tabs(["๐Ÿ“ Upload Image", "๐Ÿ“ธ Camera", "๐ŸŽฅ Real-time Webcam"])145 146with tab1:147    st.subheader("Upload an Image")148    uploaded_file = st.file_uploader(149        "Choose an image file", 150        type=['jpg', 'jpeg', 'png'],151        help="Upload an image with faces to detect emotions"152    )153    154    if uploaded_file is not None:155        # Load and display original image156        image = Image.open(uploaded_file).convert('RGB')157        158        col1, col2 = st.columns(2)159        160        with col1:161            st.subheader("Original Image")162            st.image(image, use_column_width=True)163        164        with col2:165            st.subheader("Emotion Detection Results")166            with st.spinner("๐Ÿ” Analyzing emotions..."):167                result_image, emotions = detect_emotions_in_image(image)168            169            st.image(result_image, use_column_width=True)170        171        # Display results table172        if emotions:173            st.subheader("๐Ÿ“Š Detection Summary")174            df = pd.DataFrame(emotions)175            st.dataframe(df, use_container_width=True)176        else:177            st.info("No faces detected in the image. Try uploading an image with clear faces.")178 179with tab2:180    st.subheader("Camera Capture")181    st.info("๐Ÿ“ธ Take a photo using your device camera")182    183    camera_image = st.camera_input("Take a picture")184    185    if camera_image is not None:186        image = Image.open(camera_image).convert('RGB')187        188        col1, col2 = st.columns(2)189        190        with col1:191            st.subheader("Captured Image")192            st.image(image, use_column_width=True)193        194        with col2:195            st.subheader("Emotion Detection Results")196            with st.spinner("๐Ÿ” Analyzing emotions..."):197                result_image, emotions = detect_emotions_in_image(image)198            199            st.image(result_image, use_column_width=True)200        201        # Display results202        if emotions:203            st.subheader("๐Ÿ“Š Detection Summary")204            df = pd.DataFrame(emotions)205            st.dataframe(df, use_container_width=True)206 207with tab3:208    st.subheader("๐ŸŽฅ Real-time Emotion Detection")209    210    # Use Streamlit's camera input with continuous refresh for real-time feel211    st.markdown("๐Ÿ“ฑ **Live Camera Feed** - Take photos to detect emotions accurately")212    213    # Camera input with unique key that changes to force refresh214    if 'photo_count' not in st.session_state:215        st.session_state.photo_count = 0216    217    # Auto-refresh button218    col1, col2 = st.columns([1, 3])219    with col1:220        if st.button("๐Ÿ”„ Refresh Feed", type="primary"):221            st.session_state.photo_count += 1222            st.rerun()223    224    with col2:225        auto_refresh = st.checkbox("Auto-refresh every 3 seconds", value=False)226    227    # Camera input228    camera_image = st.camera_input(229        "Live Emotion Detection", 230        key=f"realtime_cam_{st.session_state.photo_count}",231        help="Take a photo to detect emotions with high accuracy"232    )233    234    if camera_image is not None:235        # Convert to PIL Image236        image = Image.open(camera_image).convert('RGB')237        238        # Convert to OpenCV format for processing239        opencv_image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)240        241        # Process with actual emotion detection242        processed_frame, emotion_results = detect_emotions_realtime_frame(opencv_image)243        244        # Convert back to RGB for display245        result_image = cv2.cvtColor(processed_frame, cv2.COLOR_BGR2RGB)246        247        # Display the processed image with emotions248        st.image(result_image, use_column_width=True, caption="๐ŸŽฏ Accurate Emotion Detection Results")249        250        # Show detailed emotion analysis251        if emotion_results:252            st.success(f"โœ… Detected {len(emotion_results)} face(s)")253            254            # Create columns for each detected face255            cols = st.columns(min(len(emotion_results), 3))256            for i, emotion_data in enumerate(emotion_results):257                with cols[i % 3]:258                    emotion = emotion_data['emotion']259                    confidence = emotion_data['confidence']260                    261                    # Emotion emoji mapping262                    emotion_emojis = {263                        'happy': '๐Ÿ˜Š',264                        'sad': '๐Ÿ˜ข', 265                        'angry': '๐Ÿ˜ ',266                        'fear': '๐Ÿ˜จ',267                        'surprise': '๐Ÿ˜ฒ',268                        'disgust': '๐Ÿคข',269                        'neutral': '๐Ÿ˜'270                    }271                    272                    emoji = emotion_emojis.get(emotion, '๐Ÿ˜')273                    274                    # Display emotion with confidence275                    st.metric(276                        label=f"{emoji} Face {i+1}",277                        value=emotion.upper(),278                        delta=f"{confidence:.1f}% confident"279                    )280                    281                    # Color code based on confidence282                    if confidence > 80:283                        st.success("High confidence")284                    elif confidence > 60:285                        st.warning("Medium confidence") 286                    else:287                        st.info("Low confidence")288        else:289            st.warning("๐Ÿ‘ค No faces detected. Please ensure your face is clearly visible and well-lit.")290            st.info("**Tips for better detection:**\n- Face the camera directly\n- Ensure good lighting\n- Remove glasses if possible\n- Make sure face is not too close or far")291    292    # Auto-refresh logic293    if auto_refresh:294        time.sleep(3)295        st.session_state.photo_count += 1296        st.rerun()297    298    # Instructions for accurate detection299    with st.expander("๐Ÿ“‹ Tips for Accurate Emotion Detection"):300        st.markdown("""301        **For best accuracy:**302        303        ๐ŸŽฏ **Camera Setup:**304        - Use good lighting (natural light works best)305        - Position camera at eye level306        - Keep face centered in frame307        - Maintain 1-2 feet distance from camera308        309        ๐Ÿ“ธ **Taking Photos:**310        - Look directly at camera311        - Remove sunglasses if wearing312        - Try different expressions to test accuracy313        - Use 'Refresh Feed' button for new analysis314        315        ๐Ÿค– **AI Analysis:**316        - Uses DeepFace AI for high accuracy317        - Analyzes 7 different emotions318        - Shows confidence scores for reliability319        - Works best with clear, front-facing photos320        """)321    322    # Performance info323    st.info("๐Ÿš€ **Real-time Mode:** Click 'Refresh Feed' frequently or enable auto-refresh for continuous emotion monitoring!")324 325# Sidebar info326with st.sidebar:327    st.markdown("### ๐ŸŽญ About This App")328    st.info("""329    This app detects emotions in human faces using AI:330    331    **Features:**332    - Upload images or use camera333    - Real-time webcam emotion detection334    - Detects 7 emotions: Happy, Sad, Angry, Fear, Surprise, Disgust, Neutral335    - Shows confidence scores336    - Works with multiple faces337    338    **Powered by:**339    - DeepFace AI library340    - OpenCV face detection341    - Streamlit web framework342    """)343    344    st.markdown("### ๐Ÿš€ How to Use")345    st.markdown("""346    1. Choose **Upload Image**, **Camera**, or **Real-time Webcam** tab347    2. For real-time: webcam starts automatically348    3. For others: upload a photo or take a picture349    4. View detected emotions with confidence scores350    """)351    352    st.markdown("---")353    st.markdown("Made with โค๏ธ using Streamlit & DeepFace")