CoolFace
Apppublic

sunil18p31a0101/Diabetic_Retinopathy_Classification

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
app.py46 linesDownload Raw Back to root
1import gradio as gr2from tensorflow.keras.models import load_model3from PIL import Image4import numpy as np5 6# Load the model7model = load_model("shap efficient B0 multi.h5")8 9# Optionally: Recompile the model to avoid the warning (if needed for evaluation)10model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])11 12# Preprocessing function13def preprocess_image(image):14    # Resize image to the model's input size15    image = image.resize((400, 400))  # Adjust based on your model's input size16    # Normalize the image (if required by your model)17    image = np.array(image) / 255.0  # Normalize pixel values to [0, 1]18    return np.expand_dims(image, axis=0)  # Add batch dimension19 20# Prediction function21def predict(image):22    # Preprocess the input image23    preprocessed = preprocess_image(image)24    # Make predictions25    predictions = model.predict(preprocessed)26    27    # Define the class labels (you can update these with your actual class names)28    class_labels = ['No DR', 'Mild', 'Moderate', 'Severe', 'Proliferative DR']29    30    # Create a dictionary with class labels as keys and probabilities as values31    prediction_dict = {class_labels[i]: float(predictions[0][i]) for i in range(len(class_labels))}32    33    # Get the predicted class with the highest probability34    predicted_class = class_labels[np.argmax(predictions, axis=-1)[0]]35    return predicted_class, prediction_dict36 37# Gradio interface38interface = gr.Interface(39    fn=predict,  # The function to call for predictions40    inputs=gr.Image(type="pil"),  # The input type: image41    outputs=[gr.Text(), gr.Label()]  # The output type: predicted class and probability42)43 44# Launch the Gradio app45interface.launch(share=True)  # Share the app with a public link46