Spaces:
Sleeping
Sleeping
Upload 2 files
Browse filesSubida inicial del proyecto
- app.py +51 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# Cargar el procesador y el modelo
|
| 7 |
+
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
| 8 |
+
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
|
| 9 |
+
|
| 10 |
+
# Funci贸n para procesar la imagen
|
| 11 |
+
def detect_objects(image):
|
| 12 |
+
# Preprocesamiento
|
| 13 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 14 |
+
|
| 15 |
+
# Detectar objetos
|
| 16 |
+
with torch.no_grad():
|
| 17 |
+
outputs = model(**inputs)
|
| 18 |
+
|
| 19 |
+
# Filtrar resultados
|
| 20 |
+
target_sizes = torch.tensor([image.size[::-1]]) # (alto, ancho)
|
| 21 |
+
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
|
| 22 |
+
|
| 23 |
+
# Crear una lista de los resultados con nombre y puntuaci贸n
|
| 24 |
+
labels = results["labels"]
|
| 25 |
+
scores = results["scores"]
|
| 26 |
+
boxes = results["boxes"]
|
| 27 |
+
|
| 28 |
+
# Mostrar los objetos detectados
|
| 29 |
+
detected_objects = []
|
| 30 |
+
for score, label, box in zip(scores, labels, boxes):
|
| 31 |
+
detected_objects.append(f"Objeto: {model.config.id2label[label.item()]}, Score: {score:.2f}")
|
| 32 |
+
|
| 33 |
+
if not detected_objects:
|
| 34 |
+
return "No se detectaron objetos con alta confianza (>0.9)"
|
| 35 |
+
|
| 36 |
+
return "\n".join(detected_objects)
|
| 37 |
+
|
| 38 |
+
# Crear la interfaz Gradio
|
| 39 |
+
def create_interface():
|
| 40 |
+
interface = gr.Interface(
|
| 41 |
+
fn=detect_objects,
|
| 42 |
+
inputs=gr.Image(type="pil"),
|
| 43 |
+
outputs=gr.Textbox(label="Objetos Detectados"),
|
| 44 |
+
title="Detecci贸n de Objetos con Transformers",
|
| 45 |
+
description="Sube una imagen y descubre qu茅 objetos se pueden detectar."
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
interface.launch()
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
create_interface()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
gradio
|
| 4 |
+
pillow
|
| 5 |
+
timm
|