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