| import gradio as gr |
| import tensorflow as tf |
|
|
| import numpy as np |
|
|
| def load_labels(filename): |
| with open(filename, "r") as file: |
| return [line.strip() for line in file.readlines()] |
|
|
| |
| def model_prediction(test_image): |
| model = tf.keras.models.load_model("model/keras_model.h5") |
| image = tf.keras.preprocessing.image.load_img(test_image, target_size=(48, 48), color_mode="grayscale") |
| input_arr = tf.keras.preprocessing.image.img_to_array(image) / 255.0 |
| input_arr = np.expand_dims(input_arr, axis=0) |
| predictions = model.predict(input_arr) |
| return predictions |
|
|
| labels = load_labels("labels.txt") |
|
|
| def get_color(value): |
| if value >= 75: |
| return "label_red" |
| elif value >= 50: |
| return "label_yellow" |
| else: |
| return "label_green" |
|
|
| def create_colored_box(value): |
| color_class = get_color(value) |
| return gr.Label(value=f"{value:.2f}%", elem_id=color_class) |
|
|
| def predict_emotion(test_image): |
| predictions = model_prediction(test_image) |
| emotion_index = np.argmax(predictions) |
| emotion_probability = predictions[0][emotion_index] * 100 |
| emotion_name = labels[emotion_index] |
|
|
| |
| danger_level = 0 |
| if emotion_name == "sad": |
| danger_level = min(100, emotion_probability * 1.2) |
| elif emotion_name == "fearful": |
| danger_level = min(100, emotion_probability * 1.5) |
| elif emotion_name == "fake_expression": |
| danger_level = 70 |
|
|
| return ( |
| emotion_name, |
| create_colored_box(emotion_probability), |
| create_colored_box(danger_level) |
| ) |
|
|
| def home(): |
| return ( |
| "# Welcome to Emotion Analysis for Women Safety! 🌟\n\n" |
| "Our mission is to analyze facial emotions and assess the level of potential danger based on detected expressions. " |
| "This tool is designed to assist in identifying situations where immediate action might be needed to ensure safety.\n\n" |
| "### Features\n" |
| "- Detect facial emotions with high accuracy.\n" |
| "- Predict potential danger levels based on emotion analysis.\n" |
| "- User-friendly interface for seamless experience.\n\n" |
| "### How to Use\n" |
| "1. Upload an image showing the face.\n" |
| "2. View detected emotions and danger percentage.\n" |
| "3. Take necessary actions based on insights." |
| ) |
|
|
| with gr.Blocks(css=""" |
| .label_green { background-color: green; color: white; } |
| .label_yellow { background-color: yellow; color: black; } |
| .label_red { background-color: red; color: white; } |
| """) as demo: |
| gr.Markdown("# Emotion Analysis for Women Safety") |
| gr.Image("logo.webp") |
| with gr.Tabs(): |
| with gr.TabItem("Home"): |
| gr.Markdown(home()) |
| with gr.TabItem("Emotion Analysis"): |
| gr.Markdown("## Upload a facial image for emotion analysis:") |
| test_image = gr.Image(type="filepath", label="Upload Image") |
|
|
| with gr.Row(): |
| predict_btn = gr.Button("Analyze Emotion") |
| emotion_label = gr.Label(label="Detected Emotion") |
| probability_label = gr.Label(label="Emotion Probability") |
| danger_label = gr.Label(label="Danger Level (%)") |
|
|
| predict_btn.click(predict_emotion, inputs=test_image, outputs=[ |
| emotion_label, |
| probability_label, |
| danger_label |
| ]) |
|
|
| demo.launch() |