Spaces:
Sleeping
Sleeping
File size: 1,349 Bytes
dd37b39 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 2 22:59:41 2025
@author: mathe
"""
import gradio as gr
import numpy as np
from PIL import Image
import tensorflow as tf
# === CONFIGURAÇÕES ===
IMG_SIZE = 224
CLASS_NAMES = ["gato", "cachorro"] # mesma ordem do treino (0=cat, 1=dog)
# === CARREGAR MODELO ===
model = tf.keras.models.load_model("model.keras")
# === FUNÇÃO DE PREVISÃO ===
def preprocess_pil(img: Image.Image):
img = img.convert("RGB").resize((IMG_SIZE, IMG_SIZE))
arr = np.array(img, dtype=np.float32)
# MobileNetV2 preprocess (como no treino)
arr = tf.keras.applications.mobilenet_v2.preprocess_input(arr)
arr = np.expand_dims(arr, axis=0)
return arr
def predict(img: Image.Image):
x = preprocess_pil(img)
probs = model.predict(x)[0] # [p_cat, p_dog]
return {
CLASS_NAMES[0]: float(probs[0]),
CLASS_NAMES[1]: float(probs[1])
}
# === INTERFACE GRADIO ===
demo = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil", label="Envie uma imagem"),
outputs=gr.Label(num_top_classes=2),
title="Classificador de Gatos vs. Cães 🐱🐶",
description="Modelo treinado com MobileNetV2 (Transfer Learning, TensorFlow)."
)
# === EXECUÇÃO LOCAL OU NO SPACE ===
if __name__ == "__main__":
demo.launch()
|