File size: 1,917 Bytes
efb2d12
 
 
 
 
 
 
 
 
 
 
 
 
 
2805dad
 
 
 
efb2d12
 
 
 
 
5b13053
 
b1a6758
 
 
5b13053
 
 
 
 
 
 
 
 
 
 
efb2d12
 
 
5b13053
 
efb2d12
 
5b13053
efb2d12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
import streamlit as st
from PIL import Image
import torch
from torchvision import models, transforms

# Load the pre-trained model
model = models.densenet121(pretrained=True)
model.eval()

# Define the image transformations
transform = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225]
    ),
])

# Define the class labels
class_labels = ['Normal', 'Pneumonia']

# Create a function to preprocess the image
def preprocess_image(image):
    # Convert the image to RGB
    image = image.convert('RGB')

    # Resize the image to match the model's input shape
    image = image.resize((224, 224))

    # Convert the image to a tensor
    image_tensor = transform(image)

    # Add a batch dimension
    image_tensor = image_tensor.unsqueeze(0)

    return image_tensor

# Create a function to make predictions
def predict(image):
    # Preprocess the image
    preprocessed_image = preprocess_image(image)

    # Make the prediction
    with torch.no_grad():
        output = model(preprocessed_image)
        _, predicted_idx = torch.max(output, 1)
        predicted_label = class_labels[predicted_idx.item()]
    
    return predicted_label

# Create the Streamlit app
def main():
    st.title("Pneumonia Detection")
    st.write("Upload an image and the app will predict if it has pneumonia or not.")
    
    # Upload and display the image
    uploaded_image = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
    
    if uploaded_image is not None:
        image = Image.open(uploaded_image)
        st.image(image, caption="Uploaded Image", use_column_width=True)
        
        # Make a prediction
        predicted_label = predict(image)
        st.write("Prediction:", predicted_label)

# Run the app
if __name__ == '__main__':
    main()