Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import pandas as pd
|
3 |
+
import torch
|
4 |
+
from PIL import Image
|
5 |
+
from ultralytics import YOLO
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
class YOLODetect():
|
9 |
+
def __init__(self, modelo):
|
10 |
+
self.modelo = modelo
|
11 |
+
|
12 |
+
def predecir(self, source, imgsz=1280, conf=0.7, iou=0.50):
|
13 |
+
self.results = self.modelo.predict(source=source, save=True, imgsz=imgsz, conf=conf, iou=iou)
|
14 |
+
return self.results
|
15 |
+
|
16 |
+
def render(self):
|
17 |
+
result = self.results[0]
|
18 |
+
file_name = os.path.join(result.save_dir, result.path)
|
19 |
+
render = Image.open(file_name)
|
20 |
+
return render
|
21 |
+
|
22 |
+
# Inicializa el modelo YOLOv8
|
23 |
+
path_best_model = 'yolov8n.pt'
|
24 |
+
modelo_yolo = YOLO(path_best_model)
|
25 |
+
|
26 |
+
def detect_objects(im, size, iou, conf):
|
27 |
+
'''Wrapper para Gradio'''
|
28 |
+
g = (int(size) / max(im.size)) # gain
|
29 |
+
im = im.resize(tuple([int(x * g) for x in im.size]), Image.LANCZOS) # resize with antialiasing
|
30 |
+
|
31 |
+
model = YOLODetect(modelo_yolo)
|
32 |
+
results = model.predecir(source=im, imgsz=int(size), conf=conf, iou=iou)
|
33 |
+
|
34 |
+
objects_detected = results[0].boxes.cls.tolist() # Clases detectadas.
|
35 |
+
objects_conf = results[0].boxes.conf.tolist() # Probabilidad de detecci贸n por clase detectada.
|
36 |
+
|
37 |
+
objects_nested_list = pd.DataFrame({'Clase': objects_detected, 'Probabilidad': objects_conf})
|
38 |
+
|
39 |
+
result_img = model.render()
|
40 |
+
return result_img, objects_nested_list
|
41 |
+
|
42 |
+
def save_feedback(size, iou, conf,
|
43 |
+
object_count_detected,
|
44 |
+
objects_list,
|
45 |
+
user_text, feedback_text, check_status):
|
46 |
+
try:
|
47 |
+
# Aqu铆 puede ir el c贸digo para almacenar los datos en una base de datos.
|
48 |
+
return "Se guard贸 el feedback exitosamente."
|
49 |
+
except Exception as err:
|
50 |
+
print(err)
|
51 |
+
return "Error al guardar el feedback."
|
52 |
+
|
53 |
+
# Configura la interfaz de Gradio
|
54 |
+
with gr.Blocks() as demo:
|
55 |
+
gr.Markdown("# YOLOv8 Detecci贸n de objetos")
|
56 |
+
|
57 |
+
with gr.Row():
|
58 |
+
iou_threshold = gr.Slider(label="NMS IoU Threshold (0.0 - 1.0)", minimum=0.0, maximum=1.0, value=0.8)
|
59 |
+
conf_threshold = gr.Slider(label="Umbral o threshold (0.0 - 1.0)", minimum=0.0, maximum=1.0, value=0.9)
|
60 |
+
size = gr.Dropdown(label="Tama帽o de la
|