|
|
|
|
|
|
|
|
|
|
|
import gradio as gr |
|
from huggingface_hub import from_pretrained_keras |
|
import numpy as np |
|
from datetime import datetime |
|
import tensorflow as tf |
|
|
|
def process_image(image): |
|
try: |
|
model = from_pretrained_keras("dima806/facial_emotions_image_detection") |
|
|
|
|
|
img = tf.image.resize(image, (48, 48)) |
|
img = tf.image.rgb_to_grayscale(img) |
|
img = img / 255.0 |
|
img = tf.expand_dims(img, 0) |
|
|
|
|
|
pred = model.predict(img) |
|
|
|
emotions = { |
|
'happy': float(pred[0][0]), |
|
'angry': float(pred[0][1]), |
|
'surprise': float(pred[0][2]), |
|
'fear': float(pred[0][3]), |
|
'neutral': float(pred[0][4]) |
|
} |
|
|
|
|
|
analysis = generate_analysis(emotions) |
|
report = generate_report(image, analysis) |
|
|
|
return emotions, analysis['detailed_analysis'], report |
|
except Exception as e: |
|
return {"error": str(e)}, "Error in processing", "Error generating report" |
|
|
|
def generate_analysis(emotion_scores): |
|
emotions = { |
|
'happy': 'indicates joy, satisfaction, or positive mood', |
|
'angry': 'suggests frustration, irritation, or displeasure', |
|
'surprise': 'shows astonishment or unexpected reaction', |
|
'fear': 'reflects anxiety, worry, or concern', |
|
'neutral': 'displays a balanced or unemotional state' |
|
} |
|
|
|
top_emotion = max(emotion_scores.items(), key=lambda x: x[1]) |
|
|
|
return { |
|
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), |
|
"primary_emotion": top_emotion[0], |
|
"confidence_scores": emotion_scores, |
|
"detailed_analysis": f"Primary emotion: {top_emotion[0]} ({top_emotion[1]:.1f}% confidence)", |
|
"secondary_indicators": [f"{k} ({v:.1f}%)" for k, v in emotion_scores.items() if v > 20 and k != top_emotion[0]] |
|
} |
|
|
|
def generate_report(image, analysis): |
|
report = f"""Emotion Analysis Report |
|
Generated: {analysis['timestamp']} |
|
|
|
Primary Emotion: {analysis['primary_emotion'].upper()} |
|
Confidence: {analysis['confidence_scores'][analysis['primary_emotion']]:.1f}% |
|
|
|
Secondary Emotions: |
|
{', '.join(analysis['secondary_indicators']) if analysis['secondary_indicators'] else 'None significant'} |
|
|
|
All Scores: |
|
""" + '\n'.join(f"- {k}: {v:.1f}%" for k, v in sorted( |
|
analysis['confidence_scores'].items(), |
|
key=lambda x: x[1], |
|
reverse=True)) |
|
|
|
return report |
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft()) as app: |
|
gr.Markdown("# Facial Emotion Analysis System") |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
input_image = gr.Image(label="Upload Image") |
|
submit_btn = gr.Button("Analyze", variant="primary") |
|
|
|
with gr.Column(scale=1): |
|
emotion_scores = gr.Label(label="Emotion Confidence Scores") |
|
analysis_text = gr.Textbox(label="Detailed Analysis", lines=3) |
|
report_text = gr.Textbox(label="Full Report", lines=10) |
|
download_btn = gr.Button("Download Report") |
|
|
|
submit_btn.click( |
|
fn=process_image, |
|
inputs=[input_image], |
|
outputs=[emotion_scores, analysis_text, report_text] |
|
) |
|
|
|
download_btn.click( |
|
fn=lambda x: x, |
|
inputs=[report_text], |
|
outputs=[gr.File(label="Download Report")] |
|
) |
|
|
|
app.launch() |