Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,20 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
from transformers import AutoModelForObjectDetection, AutoImageProcessor
|
| 4 |
-
from PIL import Image, ImageDraw
|
| 5 |
|
| 6 |
-
# Definir el repositorio en Hugging Face
|
| 7 |
-
repo_id = "JeanCGuerrero/Practica2"
|
| 8 |
|
| 9 |
-
# Cargar el modelo de Hugging Face
|
| 10 |
-
model = AutoModelForObjectDetection.from_pretrained(repo_id)
|
| 11 |
-
image_processor = AutoImageProcessor.from_pretrained(repo_id)
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
img = img.convert("RGB") # Asegurar que la imagen est茅 en formato RGB
|
| 16 |
-
inputs = image_processor(images=img, return_tensors="pt")
|
| 17 |
-
|
| 18 |
-
with torch.no_grad():
|
| 19 |
-
outputs = model(**inputs)
|
| 20 |
-
|
| 21 |
-
# Procesar los resultados
|
| 22 |
-
target_sizes = torch.tensor([img.size[::-1]])
|
| 23 |
-
results = image_processor.post_process_object_detection(outputs, threshold=0.5, target_sizes=target_sizes)[0]
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
detecciones = []
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
return img, "\n".join(detecciones)
|
| 38 |
-
|
| 39 |
-
# Crear la interfaz y lanzarla con Gradio
|
| 40 |
-
gr.Interface(
|
| 41 |
-
fn=predict,
|
| 42 |
-
inputs=gr.Image(type="pil"),
|
| 43 |
-
outputs=[gr.Image(), gr.Text()],
|
| 44 |
-
title="Detector de Mapaches 馃",
|
| 45 |
-
description="Este modelo detecta mapaches en im谩genes usando DETR con el dataset Raccoon.",
|
| 46 |
-
examples=['raccoon-108.jpg', 'raccoon-108.jpg']
|
| 47 |
-
).launch(share=False)
|
|
|
|
| 1 |
+
from huggingface_hub import from_pretrained_fastai
|
| 2 |
import gradio as gr
|
| 3 |
+
from fastai.vision.all import *
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# repo_id = "YOUR_USERNAME/YOUR_LEARNER_NAME"
|
| 8 |
+
repo_id = "joheras/futurama"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
learner = from_pretrained_fastai(repo_id)
|
| 11 |
+
labels = learner.dls.vocab
|
|
|
|
| 12 |
|
| 13 |
+
# Definimos una funci贸n que se encarga de llevar a cabo las predicciones
|
| 14 |
+
def predict(img):
|
| 15 |
+
#img = PILImage.create(img)
|
| 16 |
+
pred,pred_idx,probs = learner.predict(img)
|
| 17 |
+
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 18 |
+
|
| 19 |
+
# Creamos la interfaz y la lanzamos.
|
| 20 |
+
gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(128, 128)), outputs=gr.outputs.Label(num_top_classes=3),examples=['fry.jpg','leela.jpg']).launch(share=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|