File size: 2,224 Bytes
bde3177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d886e9a
bde3177
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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))