-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
42 lines (36 loc) · 1.61 KB
/
app.py
File metadata and controls
42 lines (36 loc) · 1.61 KB
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
import streamlit as st
import torchxrayvision as xrv
import torch
from PIL import Image
import numpy as np
import tensorflow as tf
# Load your pneumonia model
model = tf.keras.models.load_model('model.keras')
class_names = ['Normal', 'Pneumonia']
# Load MobileNetV2 for general image classification
mobilenet = tf.keras.applications.MobileNetV2(weights='imagenet')
imagenet_labels = dict(enumerate(open(tf.keras.utils.get_file(
'ImageNetLabels.txt',
'https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt'
)).read().splitlines()))
def preprocess_image(image):
img = image.convert('RGB')
img = img.resize((224, 224))
img_array = np.array(img)
img_array = tf.keras.applications.vgg16.preprocess_input(img_array)
img_array = np.expand_dims(img_array, axis=0)
return img_array
st.title('Pneumonia Detection from Chest X-ray')
st.write('Upload a chest X-ray image to predict if it is Normal or Pneumonia.')
uploaded_file = st.file_uploader('Choose an X-ray image...', type=['jpg', 'jpeg', 'png'])
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption='Uploaded Image', use_container_width=True)
st.write('Classifying...')
img_array = preprocess_image(image)
prediction = model.predict(img_array)[0][0]
label = class_names[1] if prediction > 0.5 else class_names[0]
st.write(f'**Prediction:** {label}')
st.write(f'Confidence: {prediction:.2f} (closer to 1 = Pneumonia, closer to 0 = Normal)')
if 0.4 < prediction < 0.6:
st.warning('This image may not be a valid chest X-ray. Please upload only chest X-ray images.')