import cv2 import streamlit as st st.set_page_config(layout="wide") import streamlit.components.v1 as components import time import numpy as np import tensorflow as tf import matplotlib.pyplot as plt import matplotlib.cm as cm from PIL import Image from tf_keras_vis.gradcam import Gradcam from io import BytesIO if "model" not in st.session_state: st.session_state.model = tf_model = tf.keras.models.load_model('best_model.h5') import base64 import os #****************************************/ # GRAD CAM #*********************************************# gradcam = Gradcam(st.session_state.model, model_modifier=None, clone=False) def generate_gradcam(pil_image, target_class): # Convert PIL to array and preprocess img_array = np.array(pil_image) img_preprocessed = tf.keras.applications.vgg16.preprocess_input(img_array.copy()) img_tensor = tf.expand_dims(img_preprocessed, axis=0) # Generate heatmap loss = lambda output: tf.reduce_mean(output[:, target_class]) cam = gradcam(loss, img_tensor, penultimate_layer=-1) # Process heatmap cam = cam if cam.ndim > 2: cam = cam.squeeze() cam = np.maximum(cam, 0) cam = cv2.resize(cam, (224, 224)) cam = cam / cam.max() if cam.max() > 0 else cam return cam def convert_image_to_base64(pil_image): buffered = BytesIO() pil_image.save(buffered, format="PNG") return base64.b64encode(buffered.getvalue()).decode() #--------------------------------------------------# class_labels=[ 'Cyst', 'Normal','Stone', 'Tumor'] def load_tensorflow_model(): tf_model = tf.keras.models.load_model('best_model.h5') return tf_model def predict_image(image): time.sleep(2) image = image.resize((224, 224)) image = np.expand_dims(image, axis=0) predictions = st.session_state.model.predict(image) return predictions logo_path = "tensorflow.png" main_bg_ext = 'png' main_bg = 'bg1.jpg' # Read and encode the logo image with open(logo_path, "rb") as image_file: encoded_logo = base64.b64encode(image_file.read()).decode() # Custom CSS to style the logo above the sidebar st.markdown( f"""
T Score: {prediction[0][0]:.2f}
T Score: {prediction[0][1]:.2f}
T Score: {prediction[0][2]:.2f}
T Score: {prediction[0][3]:.2f}