|
import gradio as gr |
|
import numpy as np |
|
import cv2 |
|
import tensorflow as tf |
|
import matplotlib |
|
matplotlib.use('Agg') |
|
|
|
|
|
model = tf.keras.models.load_model('model.h5') |
|
|
|
def preprocess_image(image): |
|
resized_img = cv2.resize(image, (180, 180)) |
|
img_array = np.array(resized_img).reshape((1, 180, 180, 3)) |
|
return img_array |
|
|
|
def predict_pneumonia(image): |
|
img_array = preprocess_image(image) |
|
prediction = model.predict(img_array)[0][0] |
|
|
|
|
|
threshold = 0.7 |
|
|
|
if prediction >= threshold: |
|
pneumonia_prediction = 1 |
|
else: |
|
pneumonia_prediction = 0 |
|
|
|
|
|
class_probabilities = model.predict(img_array) |
|
|
|
|
|
top_classes = model.predict_classes(img_array) |
|
|
|
return { |
|
"Pneumonia": pneumonia_prediction, |
|
"Class probabilities": class_probabilities, |
|
"Top classes": top_classes |
|
} |
|
|
|
|
|
inputs = gr.inputs.Image(shape=(180, 180)) |
|
outputs = gr.outputs.Label(num_top_classes=2) |
|
|
|
gradio_interface = gr.Interface( |
|
fn=predict_pneumonia, |
|
inputs=inputs, |
|
outputs=outputs, |
|
title="Classificação de Pneumonia em Raios-X de Tórax", |
|
description="Esta aplicação classifica imagens de raios-X de tórax em pneumonia e normal.", |
|
examples=[ |
|
["person1946_bacteria_4875.jpeg"], |
|
["person1952_bacteria_4883.jpeg"], |
|
["NORMAL2-IM-1427-0001.jpeg"], |
|
["NORMAL2-IM-1431-0001.jpeg"] |
|
], |
|
theme="default", |
|
feedback=True |
|
) |
|
|
|
gradio_interface.launch() |
|
|