| import gradio as gr |
| from transformers import pipeline |
| from PIL import Image |
|
|
| |
| try: |
| classifier = pipeline("image-classification", model="dima806/facial_emotions_image_detection") |
| model_loaded = True |
| except Exception as e: |
| model_loaded = False |
| error_message = str(e) |
|
|
| |
| def predict_live_emotion(frame): |
| if not model_loaded: |
| return "Model failed to load. Please check your connection." |
| |
| if frame is None: |
| return "Waiting for live camera stream feed..." |
| |
| try: |
| |
| pil_img = Image.fromarray(frame) |
| |
| |
| results = classifier(pil_img) |
| |
| |
| top_prediction = results[0]['label'] |
| score = results[0]['score'] * 100 |
| |
| emoji_dict = { |
| "sad": "๐ข Sadness", |
| "disgust": "๐คข Disgust", |
| "angry": "๐ก Anger", |
| "fear": "๐ Fear / Anxiety", |
| "happy": "๐ Happiness / Joy", |
| "surprise": "๐ฎ Surprise", |
| "neutral": "๐ Neutral / Calm" |
| } |
| |
| detected_emotion = emoji_dict.get(top_prediction.lower(), top_prediction.capitalize()) |
| return f"Live Status: {detected_emotion} ({score:.2f}% Confidence)" |
| |
| except Exception as err: |
| return "Processing live stream... Keep looking at the camera." |
|
|
| |
| |
| with gr.Blocks() as demo: |
| gr.Markdown("# ๐๏ธ Live Automated Facial Emotion Detection AI") |
| gr.Markdown("This system establishes a continuous live video loop. It reads your facial expressions frame-by-frame instantly without requiring manual capture clicking.") |
| |
| with gr.Row(): |
| with gr.Column(): |
| |
| webcam_stream = gr.Image(sources=["webcam"], streaming=True, label="Live Front Camera Feed", type="numpy") |
| |
| with gr.Column(): |
| output_text = gr.Textbox(label="Real-Time AI Output:", interactive=False, placeholder="Awaiting camera transmission stream...") |
|
|
| |
| webcam_stream.stream( |
| fn=predict_live_emotion, |
| inputs=webcam_stream, |
| outputs=output_text |
| ) |
| |
| gr.Markdown("---") |
| gr.Markdown("_Fully Automated Video Stream Version built for Internship Submission._") |
|
|
| |
| demo.launch(theme=gr.themes.Soft()) |