Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
|
| 7 |
+
# Load your trained model
|
| 8 |
+
model = tf.keras.models.load_model("best_model_weights.h5") # Replace with the path to your saved model
|
| 9 |
+
|
| 10 |
+
# Define the image classification function
|
| 11 |
+
def classify_image(input_image):
|
| 12 |
+
# Preprocess the input image
|
| 13 |
+
input_image = Image.open(BytesIO(input_image))
|
| 14 |
+
input_image = input_image.resize((img_width, img_height))
|
| 15 |
+
input_image = np.array(input_image) / 255.0 # Normalize pixel values
|
| 16 |
+
|
| 17 |
+
# Make a prediction using the model
|
| 18 |
+
predictions = model.predict(np.expand_dims(input_image, axis=0))
|
| 19 |
+
|
| 20 |
+
# Get the class label with the highest probability
|
| 21 |
+
class_index = np.argmax(predictions)
|
| 22 |
+
class_prob = predictions[0][class_index]
|
| 23 |
+
|
| 24 |
+
# Define class labels (you can replace these with your actual class labels)
|
| 25 |
+
class_labels = ["Normal", "Cataract"]
|
| 26 |
+
|
| 27 |
+
# Get the class label
|
| 28 |
+
class_label = class_labels[class_index]
|
| 29 |
+
|
| 30 |
+
return f"Predicted Class: {class_label} (Probability: {class_prob:.2f})"
|
| 31 |
+
|
| 32 |
+
# Define the Gradio interface
|
| 33 |
+
iface = gr.Interface(
|
| 34 |
+
fn=classify_image,
|
| 35 |
+
inputs=gr.inputs.Image(shape=(img_height, img_width)),
|
| 36 |
+
outputs="text",
|
| 37 |
+
live=True,
|
| 38 |
+
title="Image Classifier"
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Run the Gradio interface
|
| 42 |
+
iface.launch()
|