Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,40 @@
|
|
| 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 |
-
#
|
| 8 |
-
|
| 9 |
-
img_width = 224 # Replace with the actual width of your images
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
model = tf.keras.models.load_model("best_model_weights.h5") # Replace with the path to your saved model
|
| 13 |
-
|
| 14 |
-
# Define the image classification function
|
| 15 |
def classify_image(input_image):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
input_image = np.array(input_image) / 255.0 # Normalize pixel values
|
| 21 |
-
|
| 22 |
-
# Make a prediction using the model
|
| 23 |
-
predictions = model.predict(np.expand_dims(input_image, axis=0))
|
| 24 |
-
|
| 25 |
-
# Get the class label with the highest probability
|
| 26 |
-
class_index = np.argmax(predictions)
|
| 27 |
-
class_prob = predictions[0][class_index]
|
| 28 |
-
|
| 29 |
-
# Define class labels (you can replace these with your actual class labels)
|
| 30 |
-
class_labels = ["Normal", "Cataract"]
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
fn=classify_image,
|
| 45 |
-
inputs=
|
| 46 |
-
outputs=
|
| 47 |
live=True,
|
| 48 |
-
title="Image Classifier"
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Load your trained TensorFlow model
|
| 6 |
+
model = tf.keras.models.load_model('best_model_weights.h5') # Load your saved model
|
|
|
|
| 7 |
|
| 8 |
+
# Define a function to make predictions
|
|
|
|
|
|
|
|
|
|
| 9 |
def classify_image(input_image):
|
| 10 |
+
# Preprocess the input image (resize and normalize)
|
| 11 |
+
input_image = tf.image.resize(input_image, (224, 224)) # Make sure to match your model's input size
|
| 12 |
+
input_image = (input_image / 255.0) # Normalize to [0, 1]
|
| 13 |
+
input_image = np.expand_dims(input_image, axis=0) # Add batch dimension
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Make a prediction using your model
|
| 16 |
+
prediction = model.predict(input_image)
|
| 17 |
|
| 18 |
+
# Assuming your model outputs probabilities for two classes, you can return the class with the highest probability
|
| 19 |
+
class_index = np.argmax(prediction)
|
| 20 |
+
class_labels = ["Class 0", "Class 1"] # Replace with your actual class labels
|
| 21 |
+
predicted_class = class_labels[class_index]
|
| 22 |
|
| 23 |
+
return predicted_class
|
| 24 |
|
| 25 |
+
# Create a Gradio interface
|
| 26 |
+
input_interface = gr.inputs.Image() # Gradio input component for image
|
| 27 |
+
output_interface = gr.outputs.Text() # Gradio output component for text
|
| 28 |
|
| 29 |
+
# Create the Gradio app
|
| 30 |
+
app = gr.Interface(
|
| 31 |
fn=classify_image,
|
| 32 |
+
inputs=input_interface,
|
| 33 |
+
outputs=output_interface,
|
| 34 |
live=True,
|
| 35 |
+
title="Image Classifier",
|
| 36 |
+
description="Classify images using a trained model."
|
| 37 |
)
|
| 38 |
|
| 39 |
+
# Start the Gradio app
|
| 40 |
+
app.launch()
|