Spaces:
Sleeping
Sleeping
File size: 1,213 Bytes
299b96a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import streamlit as st
import pandas as pd
import numpy as np
from tensorflow import keras
import tensorflow as tf
from keras.models import load_model
import matplotlib.pyplot as plt
from PIL import Image
st.title('Cancer Type Detection')
# Load the saved model outside the prediction function
loaded_model = load_model('cnn_model.h5')
def prediction(file):
img = tf.keras.utils.load_img(file, target_size=(224, 224))
x = tf.keras.utils.img_to_array(img)
x = np.expand_dims(x, axis=0)
# Predict the class probabilities
classes = loaded_model.predict(x)
# Get the predicted class label
classes = np.ravel(classes) # convert to 1D array
idx = np.argmax(classes)
clas = ['adenocarcinoma', 'large.cell.carcinoma', 'normal', 'squamous.cell.carcinoma'][idx]
return clas
uploaded_file = st.file_uploader("Choose MRI file")
if uploaded_file is not None:
image = Image.open(uploaded_file)
image = image.resize((224, 224))
image = tf.keras.preprocessing.image.img_to_array(image)
image = image / 255.0
image = tf.expand_dims(image, axis=0)
if st.button('Predict'):
result = prediction(uploaded_file)
st.write('Prediction is {}'.format(result)) |