Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from
|
| 3 |
-
from PIL import Image
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from keras.models import load_model
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from tensorflow.keras.preprocessing import image
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import os
|
| 7 |
import streamlit as st
|
| 8 |
+
from styling import footer
|
|
|
|
| 9 |
|
| 10 |
+
st.cache(allow_output_mutation=True)
|
| 11 |
+
st.title("TB Image Classifier")
|
| 12 |
|
| 13 |
+
# loading model
|
| 14 |
+
model = load_model('tb_model.h5')
|
| 15 |
+
# loading the imaage
|
| 16 |
|
| 17 |
+
file = st.file_uploader(
|
| 18 |
+
"Upload the image",
|
| 19 |
+
type=["png", "jpg"],
|
| 20 |
+
accept_multiple_files=False,
|
| 21 |
+
key=None,
|
| 22 |
+
help=None,
|
| 23 |
+
on_change=None,
|
| 24 |
+
args=None,
|
| 25 |
+
kwargs=None,
|
| 26 |
+
)
|
| 27 |
|
| 28 |
+
run = st.button(
|
| 29 |
+
"Make Prediction", key=None, help=None, on_click=None, args=None, kwargs=None
|
| 30 |
+
)
|
| 31 |
+
st.subheader("This app classifies an x-ray image if it has TB or not")
|
| 32 |
+
# image laoder
|
| 33 |
+
def load_image(img_path, img_size, show=False):
|
| 34 |
+
img = image.load_img(img_path, target_size=img_size)
|
| 35 |
+
img_tensor = image.img_to_array(img)
|
| 36 |
+
img_tensor = np.expand_dims(img_tensor, axis=0) # expanding image tensor
|
| 37 |
+
img_tensor = img_tensor / 255.0 # scaling the image_T
|
| 38 |
|
| 39 |
+
if show:
|
| 40 |
+
plt.imshow(img_tensor[0])
|
| 41 |
+
plt.axis("off")
|
| 42 |
+
plt.show()
|
| 43 |
+
return img_tensor
|
| 44 |
|
| 45 |
+
|
| 46 |
+
img_size = (300, 300)
|
| 47 |
+
img_path = "inference image from medscape.jpg"
|
| 48 |
+
|
| 49 |
+
classes = ["Negative", "Positive"]#["Normal", "Tuberculosis"]
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
## load img
|
| 52 |
+
footer()
|
| 53 |
+
if run == True:
|
| 54 |
+
if file is not None:
|
| 55 |
+
img = load_image(img_path, img_size)
|
| 56 |
+
pred = model.predict(img)
|
| 57 |
+
output = classes[round(pred[0][0])]
|
| 58 |
+
st.subheader(f"The image is {output}")
|
| 59 |
+
else:
|
| 60 |
+
st.write("Please upload an image first")
|
| 61 |
+
|
| 62 |
+
# st.image(file)
|