Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,71 @@
|
|
| 1 |
-
from PIL import Image
|
| 2 |
from ultralytics import YOLO
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
# Cargar un modelo YOLOv8n preentrenado
|
| 6 |
model = YOLO('best.pt')
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def detect_objects(image: Image.Image):
|
| 9 |
# Realizar la inferencia
|
| 10 |
results = model.predict(image)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
return
|
| 19 |
|
| 20 |
# Crear la interfaz de Gradio
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 2 |
from ultralytics import YOLO
|
| 3 |
import gradio as gr
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
# Cargar un modelo YOLOv8n preentrenado
|
| 7 |
model = YOLO('best.pt')
|
| 8 |
|
| 9 |
+
def contar_detecciones(cls_tensor, nombres_clases):
|
| 10 |
+
conteos = {nombre: torch.sum(cls_tensor == indice).item() for indice, nombre in enumerate(nombres_clases)}
|
| 11 |
+
return conteos
|
| 12 |
+
|
| 13 |
+
def clases_detectadas(res):
|
| 14 |
+
if res and hasattr(res[0], 'xyxy'):
|
| 15 |
+
cls_tensor = res[0].xyxy[0][:, -1] # Obtener tensor de clases
|
| 16 |
+
nombres_clases = model.names
|
| 17 |
+
conteos = contar_detecciones(cls_tensor, nombres_clases)
|
| 18 |
+
respuesta = ""
|
| 19 |
+
for nombre, conteo in conteos.items():
|
| 20 |
+
respuesta += f"Clase {nombre} : {conteo} detecciones\n"
|
| 21 |
+
return respuesta
|
| 22 |
+
else:
|
| 23 |
+
return "No se encontraron resultados o falta informaci贸n relevante"
|
| 24 |
+
|
| 25 |
def detect_objects(image: Image.Image):
|
| 26 |
# Realizar la inferencia
|
| 27 |
results = model.predict(image)
|
| 28 |
|
| 29 |
+
# Guardar la imagen con todas las detecciones
|
| 30 |
+
im_array = results.render()[0]
|
| 31 |
+
im_all_detections = Image.fromarray(im_array[..., ::-1])
|
| 32 |
+
|
| 33 |
+
# Contar las clases detectadas
|
| 34 |
+
conteo_clases = clases_detectadas(results)
|
| 35 |
+
|
| 36 |
+
# Guardar informaci贸n de detecci贸n
|
| 37 |
+
detections = results.xyxy[0].tolist()
|
| 38 |
+
|
| 39 |
+
return im_all_detections, conteo_clases, detections
|
| 40 |
+
|
| 41 |
+
def update_image(original_image: Image.Image, detections, show_potatoes: bool, show_tongues: bool):
|
| 42 |
+
# Crear una copia de la imagen original
|
| 43 |
+
updated_image = original_image.copy()
|
| 44 |
+
draw = ImageDraw.Draw(updated_image)
|
| 45 |
+
|
| 46 |
+
# Definir la fuente para las etiquetas
|
| 47 |
+
try:
|
| 48 |
+
font = ImageFont.truetype("arial.ttf", 15)
|
| 49 |
+
except IOError:
|
| 50 |
+
font = ImageFont.load_default()
|
| 51 |
+
|
| 52 |
+
# Filtrar y dibujar solo las detecciones seleccionadas
|
| 53 |
+
for det in detections:
|
| 54 |
+
label = model.names[int(det[5])]
|
| 55 |
+
if (label == 'papa' and show_potatoes) or (label == 'lengua' and show_tongues):
|
| 56 |
+
box = det[:4]
|
| 57 |
+
label_text = f"{label} {det[4]:.2f}"
|
| 58 |
+
draw.rectangle(box, outline="red", width=2)
|
| 59 |
+
text_size = draw.textsize(label_text, font=font)
|
| 60 |
+
draw.rectangle([box[0], box[1] - text_size[1], box[0] + text_size[0], box[1]], fill="red")
|
| 61 |
+
draw.text((box[0], box[1] - text_size[1]), label_text, fill="white", font=font)
|
| 62 |
|
| 63 |
+
return updated_image
|
| 64 |
|
| 65 |
# Crear la interfaz de Gradio
|
| 66 |
+
iface = gr.Interface(
|
| 67 |
+
fn=detect_objects,
|
| 68 |
+
update=update_image,
|
| 69 |
+
inputs=["image", gr.Checkbox(label="Mostrar Papas"), gr.Checkbox(label="Mostrar Lenguas")],
|
| 70 |
+
outputs=["image", "text", "image"]
|
| 71 |
+
).launch()
|