Spaces:
Sleeping
Sleeping
import tensorflow as tf | |
import efficientnet.tfkeras as efn | |
import gradio as gr | |
import numpy as np | |
# Dimensões da imagem | |
IMG_HEIGHT = 224 | |
IMG_WIDTH = 224 | |
# Função para construir o modelo | |
def build_model(img_height, img_width, n): | |
inp = tf.keras.layers.Input(shape=(img_height, img_width, n)) | |
efnet = efn.EfficientNetB0( | |
input_shape=(img_height, img_width, n), | |
weights='imagenet', | |
include_top=False | |
) | |
x = efnet(inp) | |
x = tf.keras.layers.GlobalAveragePooling2D()(x) | |
x = tf.keras.layers.Dense(2, activation='softmax')(x) | |
model = tf.keras.Model(inputs=inp, outputs=x) | |
opt = tf.keras.optimizers.Adam(learning_rate=0.000003) | |
loss = tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.01) | |
model.compile(optimizer=opt, loss=loss, metrics=['accuracy']) | |
return model | |
# Carregue o modelo treinado | |
loaded_model = build_model(IMG_HEIGHT, IMG_WIDTH, 3) | |
loaded_model.load_weights('modelo_treinado.h5') | |
# Função para realizar o pré-processamento da imagem de entrada | |
def preprocess_image(input_image): | |
# Redimensione a imagem para as dimensões esperadas pelo modelo | |
input_image = tf.image.resize(input_image, (IMG_HEIGHT, IMG_WIDTH)) | |
# Normalização dos valores de pixel para o intervalo [0, 1] | |
input_image = input_image / 255.0 | |
return input_image | |
# Função para fazer previsões usando o modelo treinado | |
def predict(input_image): | |
# Realize o pré-processamento na imagem de entrada | |
input_image = preprocess_image(input_image) | |
# Faça uma previsão usando o modelo carregado | |
input_image = tf.expand_dims(input_image, axis=0) | |
prediction = loaded_model.predict(input_image) | |
# 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]]) | |
class_names = ["Normal", "Cataract"] | |
# Determine a classe mais provável | |
predicted_class = class_names[np.argmax(prediction)] | |
return predicted_class | |
# Crie uma interface Gradio para fazer previsões | |
iface = gr.Interface( | |
predict, | |
inputs=gr.inputs.Image(label="Carregue uma imagem da região ocular", type="pil"), | |
outputs=gr.outputs.Label(num_top_classes=2, label="Avaliação"), | |
theme="compact", | |
css="""body { | |
background-color: #f0f0f0; | |
font-family: Arial, sans-serif; | |
} | |
.container { | |
margin: 20px; | |
} | |
.custom-upload-btn { | |
background-color: #007bff; | |
color: white; | |
padding: 10px 20px; | |
cursor: pointer; | |
border-radius: 5px; | |
font-weight: bold; | |
} | |
.output { | |
font-size: 18px; | |
text-align: center; | |
} | |
.loading { | |
display: none; | |
} | |
.output.loading { | |
display: block; | |
text-align: center; | |
} | |
""", | |
) | |
# HTML personalizado para o cabeçalho | |
header_html = """ | |
<div style="background-color: #007bff; padding: 20px; text-align: center;"> | |
<h1 style="color: white;">Detecção de Catarata em Imagens Oculares</h1> | |
</div> | |
""" | |
# Adicione o cabeçalho à interface | |
iface.layout(header=header_html) | |
# HTML personalizado para o rodapé | |
footer_html = """ | |
<div style="background-color: #007bff; padding: 10px; text-align: center; color: white;"> | |
© 2023 Detecção de Catarata Inc. | |
</div> | |
""" | |
# Adicione o rodapé à interface | |
iface.layout(footer=footer_html) | |
# Execute a interface Gradio | |
iface.launch() | |