Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import tensorflow as tf | |
| import numpy as np | |
| # Load the labels | |
| def load_labels(filename): | |
| with open(filename, "r") as file: | |
| labels = [line.strip().split(" ", 1)[1] for line in file.readlines()] | |
| return labels | |
| # Load the model | |
| def load_model(): | |
| return tf.keras.models.load_model("model/keras_model.h5") | |
| # Prediction function | |
| def predict_expression(image): | |
| model = load_model() | |
| img = tf.keras.preprocessing.image.load_img(image, target_size=(48, 48), color_mode="grayscale") | |
| img_array = tf.keras.preprocessing.image.img_to_array(img) / 255.0 | |
| img_array = np.expand_dims(img_array, axis=0) | |
| predictions = model.predict(img_array) | |
| label_index = np.argmax(predictions) | |
| confidence = predictions[0][label_index] * 100 | |
| return label_index, confidence | |
| # Safety management logic | |
| def get_safety_measures(label_index, confidence): | |
| safety_responses = [ | |
| "Alert nearby people and take immediate action if you sense danger.", | |
| "Show calmness and maintain distance from the source of discomfort.", | |
| "Encourage communication and provide support to reduce fear.", | |
| "No action needed, but stay vigilant to avoid any potential threats.", | |
| "Check for signs of discomfort or untruthfulness. Handle the situation carefully.", | |
| "Provide emotional support and ensure safety from external threats.", | |
| "Reassure the person and guide them to a safe environment." | |
| ] | |
| safety_action = safety_responses[label_index] | |
| return f"Expression: {labels[label_index]} (Confidence: {confidence:.2f}%)\n\nRecommended Action:\n{safety_action}" | |
| # Load labels from file | |
| labels = load_labels("labels.txt") | |
| # Gradio interface | |
| def analyze_expression(image): | |
| label_index, confidence = predict_expression(image) | |
| result = get_safety_measures(label_index, confidence) | |
| return result | |
| # Define the Gradio app | |
| with gr.Blocks() as app: | |
| gr.Markdown("# Woman Safety Management System 🌸\nUpload an image to analyze expressions and take appropriate safety actions.") | |
| with gr.Row(): | |
| image_input = gr.Image(type="filepath", label="Upload Image") | |
| analyze_btn = gr.Button("Analyze") | |
| result_output = gr.Textbox(label="Analysis Result", lines=8) | |
| analyze_btn.click(analyze_expression, inputs=image_input, outputs=result_output) | |
| # Launch the app | |
| if __name__ == "__main__": | |
| app.launch() | |