File size: 2,278 Bytes
05a860d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import numpy as np
import tensorflow as tf
from PIL import Image
import io

# Load your pre-trained model
model = tf.keras.models.load_model('model.keras')

# Define the image preprocessing function
# Define the image preprocessing function
def preprocess_image(image):
    # Convert image to RGB if it's grayscale
    if image.mode != 'RGB':
        image = image.convert('RGB')
    
    # Resize and preprocess the image
    image = image.resize((224, 224))  # Adjust the size as per your model's requirement
    image_array = np.array(image) / 255.0  # Normalize the image
    image_array = np.expand_dims(image_array, axis=0)  # Add batch dimension
    return image_array

# Define the class labels
class_labels = ['Atelectasis', 'Cardiomegaly', 'Consolidation', 'Edema', 'Effusion', 
                 'Emphysema', 'Fibrosis', 'Infiltration', 'Mass', 
                 'Nodule', 'Pleural_Thickening', 'Pneumothorax']
# class_labels = ["Class1", "Class2", "Class3", "Class4", "Class5"]  # Update with actual class names

# Streamlit app
st.title("Image Classification with Grad-CAM")

st.write("Upload an image to get predictions:")

# Upload image
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])

if uploaded_file is not None:
    # Read and display the image
    image = Image.open(uploaded_file)
    st.image(image, caption='Uploaded Image', use_column_width=True)
    
    # Preprocess the image
    preprocessed_image = preprocess_image(image)
    
    # Make predictions
    predictions = model.predict(preprocessed_image)[0]
    
    # Get top 3 predictions with probability greater than 0.5
    top_predictions = [(label, prob) for label, prob in zip(class_labels, predictions) if prob > 0.5]
    top_predictions = sorted(top_predictions, key=lambda x: x[1], reverse=True)[:3]
    
    # Display results
    st.write("Predictions with probability greater than 0.5:")
    for label, prob in top_predictions:
        st.write(f"{label}: {prob*100:.2f}%")
        percentage = int(prob * 100)
        # st.progress(prob)
        st.progress(percentage)

    if not top_predictions:
        st.write("No predictions with probability greater than 0.5.")