Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,23 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
-
import numpy as np
|
| 4 |
-
import time
|
| 5 |
|
| 6 |
-
|
| 7 |
-
model = tf.keras.models.load_model("model/keras_model.h5")
|
| 8 |
|
| 9 |
-
# Function to load labels
|
| 10 |
def load_labels(filename):
|
| 11 |
with open(filename, "r") as file:
|
| 12 |
return [line.strip() for line in file.readlines()]
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
# Function to preprocess image and predict emotion
|
| 17 |
def model_prediction(test_image):
|
|
|
|
| 18 |
image = tf.keras.preprocessing.image.load_img(test_image, target_size=(48, 48), color_mode="grayscale")
|
| 19 |
input_arr = tf.keras.preprocessing.image.img_to_array(image) / 255.0
|
| 20 |
input_arr = np.expand_dims(input_arr, axis=0)
|
| 21 |
-
|
| 22 |
-
# Measure prediction time
|
| 23 |
-
start_time = time.time()
|
| 24 |
predictions = model.predict(input_arr)
|
| 25 |
-
print("Prediction time:", time.time() - start_time)
|
| 26 |
return predictions
|
| 27 |
|
| 28 |
-
|
|
|
|
| 29 |
def get_color(value):
|
| 30 |
if value >= 75:
|
| 31 |
return "label_red"
|
|
@@ -34,26 +26,24 @@ def get_color(value):
|
|
| 34 |
else:
|
| 35 |
return "label_green"
|
| 36 |
|
| 37 |
-
# Create colored box for percentage
|
| 38 |
def create_colored_box(value):
|
| 39 |
color_class = get_color(value)
|
| 40 |
return gr.Label(value=f"{value:.2f}%", elem_id=color_class)
|
| 41 |
|
| 42 |
-
# Main prediction function
|
| 43 |
def predict_emotion(test_image):
|
| 44 |
predictions = model_prediction(test_image)
|
| 45 |
emotion_index = np.argmax(predictions)
|
| 46 |
emotion_probability = predictions[0][emotion_index] * 100
|
| 47 |
emotion_name = labels[emotion_index]
|
| 48 |
|
| 49 |
-
#
|
| 50 |
danger_level = 0
|
| 51 |
if emotion_name == "sad":
|
| 52 |
danger_level = min(100, emotion_probability * 1.2)
|
| 53 |
elif emotion_name == "fearful":
|
| 54 |
danger_level = min(100, emotion_probability * 1.5)
|
| 55 |
elif emotion_name == "fake_expression":
|
| 56 |
-
danger_level = 70 #
|
| 57 |
|
| 58 |
return (
|
| 59 |
emotion_name,
|
|
@@ -61,7 +51,6 @@ def predict_emotion(test_image):
|
|
| 61 |
create_colored_box(danger_level)
|
| 62 |
)
|
| 63 |
|
| 64 |
-
# Home page content
|
| 65 |
def home():
|
| 66 |
return (
|
| 67 |
"# Welcome to Emotion Analysis for Women Safety! 🌟\n\n"
|
|
@@ -77,20 +66,19 @@ def home():
|
|
| 77 |
"3. Take necessary actions based on insights."
|
| 78 |
)
|
| 79 |
|
| 80 |
-
# Gradio interface
|
| 81 |
with gr.Blocks(css="""
|
| 82 |
.label_green { background-color: green; color: white; }
|
| 83 |
.label_yellow { background-color: yellow; color: black; }
|
| 84 |
.label_red { background-color: red; color: white; }
|
| 85 |
""") as demo:
|
| 86 |
gr.Markdown("# Emotion Analysis for Women Safety")
|
| 87 |
-
gr.Image("logo.webp")
|
| 88 |
with gr.Tabs():
|
| 89 |
with gr.TabItem("Home"):
|
| 90 |
gr.Markdown(home())
|
| 91 |
with gr.TabItem("Emotion Analysis"):
|
| 92 |
gr.Markdown("## Upload a facial image for emotion analysis:")
|
| 93 |
-
test_image = gr.Image(type="filepath", label="Upload Image"
|
| 94 |
|
| 95 |
with gr.Row():
|
| 96 |
predict_btn = gr.Button("Analyze Emotion")
|
|
@@ -98,11 +86,10 @@ with gr.Blocks(css="""
|
|
| 98 |
probability_label = gr.Label(label="Emotion Probability")
|
| 99 |
danger_label = gr.Label(label="Danger Level (%)")
|
| 100 |
|
| 101 |
-
predict_btn.click(
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
)
|
| 106 |
|
| 107 |
-
|
| 108 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
import numpy as np
|
|
|
|
| 5 |
|
|
|
|
| 6 |
def load_labels(filename):
|
| 7 |
with open(filename, "r") as file:
|
| 8 |
return [line.strip() for line in file.readlines()]
|
| 9 |
|
| 10 |
+
# Function to load the trained model and predict emotion
|
|
|
|
|
|
|
| 11 |
def model_prediction(test_image):
|
| 12 |
+
model = tf.keras.models.load_model("model/keras_model.h5")
|
| 13 |
image = tf.keras.preprocessing.image.load_img(test_image, target_size=(48, 48), color_mode="grayscale")
|
| 14 |
input_arr = tf.keras.preprocessing.image.img_to_array(image) / 255.0
|
| 15 |
input_arr = np.expand_dims(input_arr, axis=0)
|
|
|
|
|
|
|
|
|
|
| 16 |
predictions = model.predict(input_arr)
|
|
|
|
| 17 |
return predictions
|
| 18 |
|
| 19 |
+
labels = load_labels("labels.txt")
|
| 20 |
+
|
| 21 |
def get_color(value):
|
| 22 |
if value >= 75:
|
| 23 |
return "label_red"
|
|
|
|
| 26 |
else:
|
| 27 |
return "label_green"
|
| 28 |
|
|
|
|
| 29 |
def create_colored_box(value):
|
| 30 |
color_class = get_color(value)
|
| 31 |
return gr.Label(value=f"{value:.2f}%", elem_id=color_class)
|
| 32 |
|
|
|
|
| 33 |
def predict_emotion(test_image):
|
| 34 |
predictions = model_prediction(test_image)
|
| 35 |
emotion_index = np.argmax(predictions)
|
| 36 |
emotion_probability = predictions[0][emotion_index] * 100
|
| 37 |
emotion_name = labels[emotion_index]
|
| 38 |
|
| 39 |
+
# Predict level of danger based on emotion
|
| 40 |
danger_level = 0
|
| 41 |
if emotion_name == "sad":
|
| 42 |
danger_level = min(100, emotion_probability * 1.2)
|
| 43 |
elif emotion_name == "fearful":
|
| 44 |
danger_level = min(100, emotion_probability * 1.5)
|
| 45 |
elif emotion_name == "fake_expression":
|
| 46 |
+
danger_level = 70 # Example fixed percentage for fake expression
|
| 47 |
|
| 48 |
return (
|
| 49 |
emotion_name,
|
|
|
|
| 51 |
create_colored_box(danger_level)
|
| 52 |
)
|
| 53 |
|
|
|
|
| 54 |
def home():
|
| 55 |
return (
|
| 56 |
"# Welcome to Emotion Analysis for Women Safety! 🌟\n\n"
|
|
|
|
| 66 |
"3. Take necessary actions based on insights."
|
| 67 |
)
|
| 68 |
|
|
|
|
| 69 |
with gr.Blocks(css="""
|
| 70 |
.label_green { background-color: green; color: white; }
|
| 71 |
.label_yellow { background-color: yellow; color: black; }
|
| 72 |
.label_red { background-color: red; color: white; }
|
| 73 |
""") as demo:
|
| 74 |
gr.Markdown("# Emotion Analysis for Women Safety")
|
| 75 |
+
gr.Image("logo.webp")
|
| 76 |
with gr.Tabs():
|
| 77 |
with gr.TabItem("Home"):
|
| 78 |
gr.Markdown(home())
|
| 79 |
with gr.TabItem("Emotion Analysis"):
|
| 80 |
gr.Markdown("## Upload a facial image for emotion analysis:")
|
| 81 |
+
test_image = gr.Image(type="filepath", label="Upload Image")
|
| 82 |
|
| 83 |
with gr.Row():
|
| 84 |
predict_btn = gr.Button("Analyze Emotion")
|
|
|
|
| 86 |
probability_label = gr.Label(label="Emotion Probability")
|
| 87 |
danger_label = gr.Label(label="Danger Level (%)")
|
| 88 |
|
| 89 |
+
predict_btn.click(predict_emotion, inputs=test_image, outputs=[
|
| 90 |
+
emotion_label,
|
| 91 |
+
probability_label,
|
| 92 |
+
danger_label
|
| 93 |
+
])
|
| 94 |
|
| 95 |
+
demo.launch()
|
|
|