Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,52 +1,43 @@
|
|
1 |
-
import
|
2 |
import tensorflow as tf
|
3 |
-
|
4 |
-
from tensorflow.keras.layers import Layer
|
5 |
-
import numpy as np
|
6 |
from PIL import Image
|
|
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
super(FixedDropout, self).__init__(**kwargs)
|
12 |
-
self.rate = rate
|
13 |
-
|
14 |
-
def call(self, inputs, training=None):
|
15 |
-
if training is None:
|
16 |
-
training = K.learning_phase()
|
17 |
-
|
18 |
-
if training == 1:
|
19 |
-
return K.dropout(inputs, self.rate)
|
20 |
-
return inputs
|
21 |
-
|
22 |
-
# Register the custom layer in a custom object scope
|
23 |
-
custom_objects = {"FixedDropout": FixedDropout}
|
24 |
-
|
25 |
-
# Load the TensorFlow model with the custom object scope
|
26 |
-
tf_model_path = 'modelo_treinado.h5' # Update with the path to your model
|
27 |
-
tf_model = load_model(tf_model_path, custom_objects=custom_objects)
|
28 |
|
29 |
-
#
|
30 |
class_labels = ["Normal", "Cataract"]
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
image = np.array(image) / 255.0
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
#
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
gr.Interface(
|
49 |
fn=predict,
|
50 |
-
inputs=gr.Image(type="pil"),
|
51 |
-
outputs=gr.Label(num_top_classes=
|
52 |
).launch()
|
|
|
1 |
+
import requests
|
2 |
import tensorflow as tf
|
3 |
+
import gradio as gr
|
|
|
|
|
4 |
from PIL import Image
|
5 |
+
import numpy as np
|
6 |
|
7 |
+
# Load your custom TensorFlow model. Update 'modelo_treinado.h5' with the path to your model.
|
8 |
+
tf_model_path = 'modelo_treinado.h5'
|
9 |
+
tf_model = tf.keras.models.load_model(tf_model_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# Define your class labels.
|
12 |
class_labels = ["Normal", "Cataract"]
|
13 |
|
14 |
+
def preprocess_image(image):
|
15 |
+
# Resize the image to the input size required by the model (e.g., 224x224).
|
16 |
+
image = image.resize((224, 224))
|
17 |
+
# Convert the PIL image to a NumPy array and normalize pixel values.
|
18 |
+
image = np.array(image) / 255.0
|
19 |
+
# Add a batch dimension to the image.
|
20 |
+
image = np.expand_dims(image, axis=0)
|
21 |
+
return image
|
22 |
+
|
23 |
+
def predict(inp):
|
24 |
+
# Preprocess the input image.
|
25 |
+
inp = preprocess_image(inp)
|
26 |
+
# Make predictions using your custom TensorFlow model.
|
27 |
+
predictions = tf_model.predict(inp)
|
28 |
+
# Get the class label with the highest confidence.
|
29 |
+
predicted_class = class_labels[np.argmax(predictions)]
|
30 |
+
# Get the confidence score of the predicted class.
|
31 |
+
confidence = float(predictions[0][np.argmax(predictions)])
|
32 |
+
|
33 |
+
# Create a dictionary with the predicted class and its confidence.
|
34 |
+
result = {predicted_class: confidence}
|
35 |
+
|
36 |
+
return result
|
37 |
+
|
38 |
+
# Create a Gradio interface.
|
39 |
gr.Interface(
|
40 |
fn=predict,
|
41 |
+
inputs=gr.inputs.Image(type="pil"),
|
42 |
+
outputs=gr.outputs.Label(num_top_classes=1)
|
43 |
).launch()
|