itsanmolgupta commited on
Commit
5370e93
·
verified ·
1 Parent(s): 8b240a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -62
app.py CHANGED
@@ -1,63 +1,63 @@
1
- import streamlit as st
2
- import numpy as np
3
- import tensorflow as tf
4
- from PIL import Image
5
- import io
6
-
7
- # Load your pre-trained model
8
- model = tf.keras.models.load_model('model_2.keras')
9
-
10
- # Define the image preprocessing function
11
- # Define the image preprocessing function
12
- def preprocess_image(image):
13
- # Convert image to RGB if it's grayscale
14
- if image.mode != 'RGB':
15
- image = image.convert('RGB')
16
-
17
- # Resize and preprocess the image
18
- image = image.resize((224, 224)) # Adjust the size as per your model's requirement
19
- image_array = np.array(image) / 255.0 # Normalize the image
20
- image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
21
- return image_array
22
-
23
- # Define the class labels
24
- class_labels = ['Atelectasis', 'Cardiomegaly', 'Consolidation', 'Edema', 'Effusion',
25
- 'Emphysema', 'Fibrosis', 'Infiltration', 'Mass',
26
- 'Nodule', 'Pleural_Thickening', 'Pneumothorax']
27
- # class_labels = ["Class1", "Class2", "Class3", "Class4", "Class5"] # Update with actual class names
28
-
29
- # Streamlit app
30
- st.title("Chest X-ray Classification")
31
-
32
- # Upload image
33
- uploaded_file = st.file_uploader("Upload a Chest X-ray image...", type=["jpg", "jpeg", "png"])
34
-
35
- # Create two columns
36
- col1, col2 = st.columns(2)
37
-
38
- if uploaded_file is not None:
39
- # Read and display the image
40
- image = Image.open(uploaded_file)
41
- with col1:
42
- st.image(image, caption='Uploaded Image', use_column_width=True)
43
-
44
- # Preprocess the image
45
- preprocessed_image = preprocess_image(image)
46
-
47
- # Make predictions
48
- predictions = model.predict(preprocessed_image)[0]
49
-
50
- # Get top 3 predictions with probability greater than 0.5
51
- top_predictions = [(label, prob) for label, prob in zip(class_labels, predictions) if prob > 0.5]
52
- top_predictions = sorted(top_predictions, key=lambda x: x[1], reverse=True)[:3]
53
-
54
- with col2:
55
- # Display results
56
- if not top_predictions:
57
- st.write("No any diseases found with probability greater than 50%.")
58
- else:
59
- st.write("Predicted Disease(s):")
60
- for label, prob in top_predictions:
61
- st.write(f"{label}: {prob*100:.2f}%")
62
- percentage = int(prob * 100)
63
  st.progress(percentage)
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import tensorflow as tf
4
+ from PIL import Image
5
+ import io
6
+
7
+ # Load your pre-trained model
8
+ model = tf.keras.models.load_model('model.keras')
9
+
10
+ # Define the image preprocessing function
11
+ # Define the image preprocessing function
12
+ def preprocess_image(image):
13
+ # Convert image to RGB if it's grayscale
14
+ if image.mode != 'RGB':
15
+ image = image.convert('RGB')
16
+
17
+ # Resize and preprocess the image
18
+ image = image.resize((224, 224)) # Adjust the size as per your model's requirement
19
+ image_array = np.array(image) / 255.0 # Normalize the image
20
+ image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
21
+ return image_array
22
+
23
+ # Define the class labels
24
+ class_labels = ['Atelectasis', 'Cardiomegaly', 'Consolidation', 'Edema', 'Effusion',
25
+ 'Emphysema', 'Fibrosis', 'Infiltration', 'Mass',
26
+ 'Nodule', 'Pleural_Thickening', 'Pneumothorax']
27
+ # class_labels = ["Class1", "Class2", "Class3", "Class4", "Class5"] # Update with actual class names
28
+
29
+ # Streamlit app
30
+ st.title("Chest X-ray Classification")
31
+
32
+ # Upload image
33
+ uploaded_file = st.file_uploader("Upload a Chest X-ray image...", type=["jpg", "jpeg", "png"])
34
+
35
+ # Create two columns
36
+ col1, col2 = st.columns(2)
37
+
38
+ if uploaded_file is not None:
39
+ # Read and display the image
40
+ image = Image.open(uploaded_file)
41
+ with col1:
42
+ st.image(image, caption='Uploaded Image', use_column_width=True)
43
+
44
+ # Preprocess the image
45
+ preprocessed_image = preprocess_image(image)
46
+
47
+ # Make predictions
48
+ predictions = model.predict(preprocessed_image)[0]
49
+
50
+ # Get top 3 predictions with probability greater than 0.5
51
+ top_predictions = [(label, prob) for label, prob in zip(class_labels, predictions) if prob > 0.5]
52
+ top_predictions = sorted(top_predictions, key=lambda x: x[1], reverse=True)[:3]
53
+
54
+ with col2:
55
+ # Display results
56
+ if not top_predictions:
57
+ st.write("No any diseases found with probability greater than 50%.")
58
+ else:
59
+ st.write("Predicted Disease(s):")
60
+ for label, prob in top_predictions:
61
+ st.write(f"{label}: {prob*100:.2f}%")
62
+ percentage = int(prob * 100)
63
  st.progress(percentage)