Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# Load the labels
|
| 6 |
+
def load_labels(filename):
|
| 7 |
+
with open(filename, "r") as file:
|
| 8 |
+
labels = [line.strip().split(" ", 1)[1] for line in file.readlines()]
|
| 9 |
+
return labels
|
| 10 |
+
|
| 11 |
+
# Load the model
|
| 12 |
+
def load_model():
|
| 13 |
+
return tf.keras.models.load_model("model/keras_model.h5")
|
| 14 |
+
|
| 15 |
+
# Prediction function
|
| 16 |
+
def predict_expression(image):
|
| 17 |
+
model = load_model()
|
| 18 |
+
img = tf.keras.preprocessing.image.load_img(image, target_size=(48, 48), color_mode="grayscale")
|
| 19 |
+
img_array = tf.keras.preprocessing.image.img_to_array(img) / 255.0
|
| 20 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 21 |
+
|
| 22 |
+
predictions = model.predict(img_array)
|
| 23 |
+
label_index = np.argmax(predictions)
|
| 24 |
+
confidence = predictions[0][label_index] * 100
|
| 25 |
+
return label_index, confidence
|
| 26 |
+
|
| 27 |
+
# Safety management logic
|
| 28 |
+
def get_safety_measures(label_index, confidence):
|
| 29 |
+
safety_responses = [
|
| 30 |
+
"Alert nearby people and take immediate action if you sense danger.",
|
| 31 |
+
"Show calmness and maintain distance from the source of discomfort.",
|
| 32 |
+
"Encourage communication and provide support to reduce fear.",
|
| 33 |
+
"No action needed, but stay vigilant to avoid any potential threats.",
|
| 34 |
+
"Check for signs of discomfort or untruthfulness. Handle the situation carefully.",
|
| 35 |
+
"Provide emotional support and ensure safety from external threats.",
|
| 36 |
+
"Reassure the person and guide them to a safe environment."
|
| 37 |
+
]
|
| 38 |
+
safety_action = safety_responses[label_index]
|
| 39 |
+
return f"Expression: {labels[label_index]} (Confidence: {confidence:.2f}%)\n\nRecommended Action:\n{safety_action}"
|
| 40 |
+
|
| 41 |
+
# Load labels from file
|
| 42 |
+
labels = load_labels("labels.txt")
|
| 43 |
+
|
| 44 |
+
# Gradio interface
|
| 45 |
+
def analyze_expression(image):
|
| 46 |
+
label_index, confidence = predict_expression(image)
|
| 47 |
+
result = get_safety_measures(label_index, confidence)
|
| 48 |
+
return result
|
| 49 |
+
|
| 50 |
+
# Define the Gradio app
|
| 51 |
+
with gr.Blocks() as app:
|
| 52 |
+
gr.Markdown("# Woman Safety Management System 🌸\nUpload an image to analyze expressions and take appropriate safety actions.")
|
| 53 |
+
with gr.Row():
|
| 54 |
+
image_input = gr.Image(type="filepath", label="Upload Image")
|
| 55 |
+
analyze_btn = gr.Button("Analyze")
|
| 56 |
+
result_output = gr.Textbox(label="Analysis Result", lines=8)
|
| 57 |
+
|
| 58 |
+
analyze_btn.click(analyze_expression, inputs=image_input, outputs=result_output)
|
| 59 |
+
|
| 60 |
+
# Launch the app
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
app.launch()
|