rohitashva commited on
Commit
6bd9182
·
verified ·
1 Parent(s): 5f41b32

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -62
app.py CHANGED
@@ -1,80 +1,50 @@
1
  import streamlit as st
2
- import numpy as np
3
  import tensorflow as tf
 
4
  from PIL import Image
5
- import nibabel as nib
6
- from scipy import ndimage
 
 
 
 
 
 
 
 
7
 
8
  # Load the trained model
9
- @st.cache_resource()
10
  def load_model():
11
- return tf.keras.models.load_model("model.h5")
 
12
 
13
  model = load_model()
14
 
15
- # Preprocessing functions
16
- def read_nifti_file(filepath):
17
- """Read and load volume"""
18
- scan = nib.load(filepath)
19
- scan = scan.get_fdata()
20
- return scan
21
-
22
- def normalize(volume):
23
- """Normalize the volume"""
24
- volume = volume.astype(np.float32) # Ensure float32 before operations
25
- min_val = -1000
26
- max_val = 400
27
- volume[volume < min_val] = min_val
28
- volume[volume > max_val] = max_val
29
- volume = (volume - min_val) / (max_val - min_val)
30
- return volume
31
-
32
-
33
- def resize_volume(img):
34
- """Resize across z-axis"""
35
- desired_depth = 64
36
- desired_width = 128
37
- desired_height = 128
38
- current_depth = img.shape[-1]
39
- current_width = img.shape[0]
40
- current_height = img.shape[1]
41
- depth = current_depth / desired_depth
42
- width = current_width / desired_width
43
- height = current_height / desired_height
44
- depth_factor = 1 / depth
45
- width_factor = 1 / width
46
- height_factor = 1 / height
47
- img = ndimage.rotate(img, 90, reshape=False)
48
- img = ndimage.zoom(img, (width_factor, height_factor, depth_factor), order=1)
49
- return img
50
-
51
-
52
- def process_scan(image):
53
- """Process the image"""
54
- volume = np.array(image)
55
- volume = normalize(volume)
56
- volume = resize_volume(volume)
57
- volume = np.expand_dims(volume, axis=-1) # Add channel dimension
58
- return volume
59
 
60
- # Define the Streamlit app
61
- st.title("Pneumonia CT Scan")
62
- st.write("Upload an image to classify it using the trained model.")
63
 
64
- uploaded_file = st.file_uploader("Choose an image...")
65
 
66
  if uploaded_file is not None:
67
  image = Image.open(uploaded_file)
68
  st.image(image, caption="Uploaded Image", use_column_width=True)
69
 
70
- # Preprocess the image
71
- img_array = process_scan(image) # Apply preprocessing pipeline
72
- img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
73
 
74
- # Make a prediction
75
- prediction = model.predict(img_array)
76
- predicted_class = np.argmax(prediction, axis=1)[0]
77
- confidence = np.max(prediction)
78
 
79
- st.write(f"### Prediction: Class {predicted_class}")
80
- st.write(f"### Confidence: {confidence:.2f}")
 
1
  import streamlit as st
 
2
  import tensorflow as tf
3
+ import numpy as np
4
  from PIL import Image
5
+ from tensorflow.keras.applications.densenet import preprocess_input
6
+
7
+ # Define class labels
8
+ CLASS_LABELS = [
9
+ "Bacterial Pneumonia",
10
+ "Corona Virus Disease",
11
+ "Normal",
12
+ "Tuberculosis",
13
+ "Viral Pneumonia"
14
+ ]
15
 
16
  # Load the trained model
17
+ @st.cache_resource
18
  def load_model():
19
+ model = tf.keras.models.load_model("lung_disease_model.h5") # Update filename if needed
20
+ return model
21
 
22
  model = load_model()
23
 
24
+ # Function to preprocess image
25
+ def preprocess_image(image):
26
+ image = image.resize((224, 224)) # Match your model's input size
27
+ image = np.array(image) # Convert to NumPy array
28
+ image = np.expand_dims(image, axis=0) # Add batch dimension
29
+ image = preprocess_input(image) # Apply DenseNet preprocessing
30
+ return image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
+ # Streamlit UI
33
+ st.title("Lung Disease Classification")
 
34
 
35
+ uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
36
 
37
  if uploaded_file is not None:
38
  image = Image.open(uploaded_file)
39
  st.image(image, caption="Uploaded Image", use_column_width=True)
40
 
41
+ # Preprocess and predict
42
+ processed_image = preprocess_image(image)
43
+ prediction = model.predict(processed_image)
44
 
45
+ # Get the predicted class index
46
+ predicted_class = np.argmax(prediction)
47
+ predicted_label = CLASS_LABELS[predicted_class]
 
48
 
49
+ # Display result
50
+ st.write(f"### Prediction: **{predicted_label}**")