DHEIVER's picture
Duplicate from Xian1057/histopathological_img_cls
97564d2
from fastai.vision.all import *
from io import BytesIO
from PIL import Image
from utils.cam import grad_cam
from utils.tfms import AlbTransform, get_augs
import requests
import streamlit as st
"""
# Histopathologic Cancer Detection
## Is this an cancerous cell?
this is a web app to predict whether a cell is cancerous or not.
"""
# List of example image image paths
images = {
"Image 1": "example/c2a8ef295e49b8012e5bc83917a305057eaa1932.tif",
"Image 2": "example/87aed2dd32c7fe17c3af56878abd62eb5f37f925.tif",
"Image 3": "example/4ba0d1c62230781533c2df1d95e5b4c8f5b650a3.tif",
"Image 4": "example/a24ce148f6ffa7ef8eefb4efb12ebffe8dd700da.tif",
"Image 5": "example/d2dd0de8e583a5475a07c2f92fa3f06c7fabcd42.tif",
# add more images as needed
}
def predict(learn, img):
img = PILImage.create(img)
pred, key, probs = learn.predict(img)
grad_img = grad_cam(learn, img, pred)
col1, col2 = st.columns(2)
with col1:
st.image(img, caption='Original Image', use_column_width=True)
with col2:
st.image(grad_img, caption='Grad-CAM Image', use_column_width=True)
# st.write(learn_inf.predict(img))
f"""
## This **{'is ' if pred == '1' else 'is not'}** an cancerous cell.
### Rediction result: {pred}
### Probability of {pred}: {probs[key].item()*100: .2f}%
"""
path = "./"
learn_inf = load_learner(path + "resnet50.pkl")
option = st.radio("", ["Upload Image", "Image URL", "Download Example Image", "Example"])
if option == "Upload Image":
uploaded_file = st.file_uploader("Please upload an image.")
if uploaded_file is not None:
predict(learn_inf, uploaded_file)
elif option == "Image URL":
url = st.text_input("Please input a url.")
if url != "":
try:
response = requests.get(url)
pil_img = PILImage.create(BytesIO(response.content))
predict(learn_inf, pil_img)
except:
st.text("Problem reading image from", url)
elif option == "Download Example Image":
# Create columns for the images
cols = st.columns(len(images))
# Initialize selected_image in session state if it doesn't exist
if 'selected_image' not in st.session_state:
st.session_state['selected_image'] = None
for i, (img_name, img_path) in enumerate(images.items()):
# Open the image file
img = Image.open(img_path)
# Display the image in a column
cols[i].image(img, caption=img_name, use_column_width=True)
# Create a button for selecting the image
if cols[i].button(f"Select {img_name}", key=img_name):
st.session_state['selected_image'] = img_path
# If an image has been selected, show the download button
if st.session_state['selected_image'] is not None:
# Open the selected image file
img = Image.open(st.session_state['selected_image'])
# Convert the image to a byte array
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()
# Create the download button for the selected image
st.download_button(
label="Download selected image",
data=img_byte_arr,
file_name="selected_image.png",
mime="image/png",
)
else:
option_1 = st.selectbox(
'Example Prediction',
('Positive Image', 'Negative Image'))
if option_1 == 'Positive Image':
predict(learn_inf, img='example/c2a8ef295e49b8012e5bc83917a305057eaa1932.tif')
elif option_1 == 'Negative Image':
predict(learn_inf, img='example/87aed2dd32c7fe17c3af56878abd62eb5f37f925.tif')