DHEIVER commited on
Commit
08cbfae
1 Parent(s): a46bb01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -40
app.py CHANGED
@@ -1,43 +1,54 @@
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()
 
 
1
  import tensorflow as tf
2
+ import efficientnet.tfkeras as efn
3
+ from tensorflow.keras.layers import Input, GlobalAveragePooling2D, Dense
4
  import numpy as np
5
+ import gradio as gr
6
+
7
+ # Dimensões da imagem
8
+ IMG_HEIGHT = 224
9
+ IMG_WIDTH = 224
10
+
11
+ # Função para construir o modelo
12
+ def build_model(img_height, img_width, n):
13
+ inp = Input(shape=(img_height, img_width, n))
14
+ efnet = efn.EfficientNetB0(
15
+ input_shape=(img_height, img_width, n),
16
+ weights='imagenet',
17
+ include_top=False
18
+ )
19
+ x = efnet(inp)
20
+ x = GlobalAveragePooling2D()(x)
21
+ x = Dense(2, activation='softmax')(x)
22
+ model = tf.keras.Model(inputs=inp, outputs=x)
23
+ opt = tf.keras.optimizers.Adam(learning_rate=0.000003)
24
+ loss = tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.01)
25
+ model.compile(optimizer=opt, loss=loss, metrics=['accuracy'])
26
+ return model
27
+
28
+ # Carregue o modelo treinado
29
+ loaded_model = build_model(IMG_HEIGHT, IMG_WIDTH, 3)
30
+ loaded_model.load_weights('modelo_treinado.h5')
31
+
32
+ # Função para fazer previsões usando o modelo treinado
33
+ def predict_image(input_image):
34
+ # Realize o pré-processamento na imagem de entrada, se necessário
35
+ # input_image = preprocess_image(input_image)
36
+
37
+ # Faça uma previsão usando o modelo carregado
38
+ input_image = tf.image.resize(input_image, (IMG_HEIGHT, IMG_WIDTH))
39
+ input_image = tf.expand_dims(input_image, axis=0)
40
+ prediction = loaded_model.predict(input_image)
41
+
42
+ # A saída será uma matriz de previsões (no caso de classificação de duas classes, será algo como [[probabilidade_classe_0, probabilidade_classe_1]])
43
+ return prediction
44
+
45
+ # Crie uma interface Gradio para fazer previsões
46
+ iface = gr.Interface(
47
+ fn=predict_image,
48
+ inputs="image",
49
+ outputs="text",
50
+ interpretation="default"
51
+ )
52
 
53
+ # Execute a interface Gradio
54
+ iface.launch()