Spaces:
Sleeping
Sleeping
| # -*- coding: utf-8 -*- | |
| """app Luan.py | |
| Automatically generated by Colab. | |
| Original file is located at | |
| https://colab.research.google.com/drive/12TNY9jAAsh6IkUIRF-0mCt6Jy7__LsJo | |
| """ | |
| import gradio as gr | |
| from PIL import Image | |
| import numpy as np | |
| import tensorflow as tf | |
| import pandas as pd | |
| import plotly.express as px | |
| import gdown | |
| import os | |
| # Carrega o modelo TFLite uma única vez | |
| def carrega_modelo(): | |
| output = 'modelo_quantizado16bits.tflite' | |
| if not os.path.exists(output): | |
| raise FileNotFoundError("O modelo TFLite não foi encontrado no diretório do app.") | |
| interpreter = tf.lite.Interpreter(model_path=output) | |
| interpreter.allocate_tensors() | |
| return interpreter | |
| interpreter = carrega_modelo() | |
| # Função principal que recebe a imagem e retorna o gráfico | |
| def classificar_imagem(imagem_pil): | |
| # Pré-processamento da imagem | |
| image = np.array(imagem_pil, dtype=np.float32) | |
| image = image / 255.0 | |
| image = np.expand_dims(image, axis=0) | |
| # Realiza inferência | |
| input_details = interpreter.get_input_details() | |
| output_details = interpreter.get_output_details() | |
| interpreter.set_tensor(input_details[0]['index'], image) | |
| interpreter.invoke() | |
| output_data = interpreter.get_tensor(output_details[0]['index']) | |
| # Processa saída e cria gráfico | |
| classes = ['BlackMeasles', 'BlackRot', 'HealthyGrapes', 'LeafBlight'] | |
| df = pd.DataFrame({'classes': classes, 'probabilidades (%)': 100 * output_data[0]}) | |
| fig = px.bar(df, y='classes', x='probabilidades (%)', orientation='h', text='probabilidades (%)', | |
| title='Probabilidade de Classes de Doenças em Uvas') | |
| return fig | |
| # Interface Gradio | |
| interface = gr.Interface( | |
| fn=classificar_imagem, | |
| inputs=gr.Image(type="pil", label="Envie uma imagem de folha de videira"), | |
| outputs=gr.Plot(label="Gráfico de Previsões"), | |
| title="Classificador de Folhas de Videira 🍇", | |
| description="Faça upload de uma imagem de folha de videira para detectar possíveis doenças." | |
| ) | |
| if __name__ == "__main__": | |
| interface.launch() |