Spaces:
Sleeping
Sleeping
import os | |
import streamlit as st | |
import tensorflow as tf | |
import numpy as np | |
# Loading the saved model | |
model = tf.keras.models.load_model('model.h5') | |
def predict(input_image): | |
try: | |
# Preprocessing | |
input_image = tf.convert_to_tensor(input_image) | |
input_image = tf.image.resize(input_image, [224, 224]) | |
input_image = tf.expand_dims(input_image, 0) / 255.0 | |
# Prediction | |
predictions = model.predict(input_image) | |
labels = ['Cataract', 'Conjunctivitis', 'Glaucoma', 'Normal'] | |
# Get confidence score for each class | |
disease_confidence = {label: np.round(predictions[0][idx] * 100, 3) for idx, label in enumerate(labels)} | |
# Get confidence percentage for the "Normal" class | |
normal_confidence = disease_confidence['Normal'] | |
# Check if Normal confidence is greater than 50% | |
if normal_confidence > 50: | |
return f"""Congrats! no disease detected | |
Normal with confidence: {normal_confidence}%""" | |
output_lines = [f"\n{disease}: {confidence}%" for disease, confidence in disease_confidence.items()] | |
output_string = "\n".join(output_lines[:-1]) | |
return output_string | |
except Exception as e: | |
return f"An error occurred: {e}" | |
# Example images directory | |
examples = [os.path.join("example", file) for file in os.listdir("example")] | |
# Streamlit app | |
st.title("ποΈ Eye Disease Detection") | |
st.write("This model identifies common eye diseases such as Cataract, Conjunctivitis, and Glaucoma. Upload an eye image to see how the model classifies its condition.") | |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png"]) | |
if uploaded_file is not None: | |
# Display the uploaded image | |
image = tf.image.decode_image(uploaded_file.read(), channels=3) | |
image_np = image.numpy() | |
st.image(image_np, caption='Uploaded Image.', use_column_width=True) | |
# Perform prediction | |
prediction = predict(image_np) | |
st.write("Prediction : ") | |
st.write(prediction) | |
# Display examples images | |
st.write("Examples:") | |
cols = st.columns(len(examples)) | |
for idx, example in enumerate(examples): | |
cols[idx].image(example, caption=os.path.basename(example)) |