Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,34 @@
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
-
import
|
|
|
4 |
import numpy as np
|
5 |
|
6 |
-
#
|
7 |
-
class FixedDropout(tf.keras.layers.Layer):
|
8 |
-
def __init__(self, rate, **kwargs):
|
9 |
-
super(FixedDropout, self).__init__(**kwargs)
|
10 |
-
self.rate = rate
|
11 |
-
|
12 |
-
def call(self, inputs, training=None):
|
13 |
-
return tf.keras.layers.Dropout(self.rate)(inputs, training=training)
|
14 |
-
|
15 |
-
# Load the TensorFlow model with custom layer handling
|
16 |
-
def load_model_with_custom_objects(model_path):
|
17 |
-
with tf.keras.utils.custom_object_scope({'FixedDropout': FixedDropout}):
|
18 |
-
model = tf.keras.models.load_model(model_path)
|
19 |
-
return model
|
20 |
-
|
21 |
tf_model_path = 'modelo_treinado.h5' # Update with the path to your TensorFlow model
|
22 |
-
tf_model =
|
23 |
|
|
|
24 |
class_labels = ["Normal", "Cataract"]
|
25 |
|
26 |
-
# Define a
|
27 |
-
def
|
28 |
-
# Preprocess the input image
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
|
33 |
-
# Make
|
34 |
-
predictions = tf_model.predict(
|
35 |
-
class_index = np.argmax(predictions, axis=1)[0]
|
36 |
-
predicted_class = class_labels[class_index]
|
37 |
|
38 |
-
|
|
|
39 |
|
40 |
-
|
41 |
-
input_image = gr.inputs.Image(shape=(224, 224, 3)) # Define the input image shape
|
42 |
-
output_label = gr.outputs.Label() # Define the output label
|
43 |
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
+
from tensorflow.keras.models import load_model
|
4 |
+
from PIL import Image
|
5 |
import numpy as np
|
6 |
|
7 |
+
# Load the TensorFlow model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
tf_model_path = 'modelo_treinado.h5' # Update with the path to your TensorFlow model
|
9 |
+
tf_model = load_model(tf_model_path)
|
10 |
|
11 |
+
# Class labels for the model
|
12 |
class_labels = ["Normal", "Cataract"]
|
13 |
|
14 |
+
# Define a function for prediction
|
15 |
+
def predict(image):
|
16 |
+
# Preprocess the input image (resize and normalize)
|
17 |
+
image = image.resize((224, 224)) # Adjust the size as needed
|
18 |
+
image = np.array(image) / 255.0 # Normalize pixel values
|
19 |
+
image = np.expand_dims(image, axis=0) # Add batch dimension
|
20 |
|
21 |
+
# Make a prediction using the loaded TensorFlow model
|
22 |
+
predictions = tf_model.predict(image)
|
|
|
|
|
23 |
|
24 |
+
# Get the predicted class label
|
25 |
+
predicted_label = class_labels[np.argmax(predictions)]
|
26 |
|
27 |
+
return predicted_label
|
|
|
|
|
28 |
|
29 |
+
# Create the Gradio interface
|
30 |
+
gr.Interface(
|
31 |
+
fn=predict,
|
32 |
+
inputs=gr.Image(type="pil"),
|
33 |
+
outputs=gr.Label(num_top_classes=2),
|
34 |
+
).launch()
|