Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import pandas as pd
|
3 |
+
import streamlit as st
|
4 |
+
import torch
|
5 |
+
from PIL import Image
|
6 |
+
from ultralytics import YOLO
|
7 |
+
|
8 |
+
|
9 |
+
class YOLODetect():
|
10 |
+
def __init__(self, modelo):
|
11 |
+
self.modelo = modelo
|
12 |
+
|
13 |
+
def predecir(self, source, imgsz=1280, conf=0.7, iou=0.50):
|
14 |
+
# conf float 0.25 umbral de confianza del objeto para la detecci贸n
|
15 |
+
# iou float 0.7 umbral de intersecci贸n sobre uni贸n (IoU) para NMS
|
16 |
+
self.results = self.modelo.predict(source=source, save=True, imgsz=imgsz, conf=conf, iou=iou)
|
17 |
+
return self.results
|
18 |
+
|
19 |
+
def render(self):
|
20 |
+
result = self.results[0]
|
21 |
+
file_name = os.path.join(result.save_dir, result.path)
|
22 |
+
render = Image.open(file_name)
|
23 |
+
return render
|
24 |
+
|
25 |
+
path_best_model = 'yolov8n.pt'
|
26 |
+
modelo_yolo = YOLO(path_best_model)
|
27 |
+
|
28 |
+
def detect_objects(size, iou, conf, im):
|
29 |
+
'''Wrapper fn for gradio'''
|
30 |
+
g = (int(size) / max(im.size)) # gain
|
31 |
+
im = im.resize(tuple([int(x * g) for x in im.size]), Image.LANCZOS) # resize with antialiasing
|
32 |
+
|
33 |
+
model = YOLODetect(modelo_yolo)
|
34 |
+
results = model.predecir(source=im, imgsz=int(size), conf=conf, iou=iou)
|
35 |
+
|
36 |
+
objects_detected = results[0].boxes.cls.tolist() # Clases detectadas.
|
37 |
+
objects_conf = results[0].boxes.conf.tolist() # Probabilidad de detecci贸n por clase detectada.
|
38 |
+
|
39 |
+
objects_nested_list = pd.DataFrame({'Clase': objects_detected, 'Probabilidad': objects_conf})
|
40 |
+
|
41 |
+
result_img = model.render()
|
42 |
+
return result_img, objects_nested_list
|
43 |
+
|
44 |
+
def save_feedback(size, iou, conf,
|
45 |
+
object_count_detected,
|
46 |
+
objects_list,
|
47 |
+
user_text, feedback_text, check_status):
|
48 |
+
try:
|
49 |
+
# Aqu铆 puede ir el c贸digo para almacenar los datos en una base de datos.
|
50 |
+
st.success("Se guard贸 el feeback exitosamente.")
|
51 |
+
except Exception as err:
|
52 |
+
print(err)
|
53 |
+
st.warning("Error al guardar el feedback.")
|
54 |
+
|
55 |
+
# Streamlit app layout
|
56 |
+
st.title('YOLOv8 Detecci贸n de objetos')
|
57 |
+
|
58 |
+
# Input
|
59 |
+
col1, col2 = st.columns(2)
|
60 |
+
with col1:
|
61 |
+
iou_threshold = st.slider("NMS IoU Threshold (0.0 - 1.0)", 0.0, 1.0, 0.8, key="iou")
|
62 |
+
conf_threshold = st.slider("Umbral o threshold (0.0 - 1.0)", 0.0, 1.0, 0.9, key="conf")
|
63 |
+
with col2:
|
64 |
+
size = st.selectbox("Tama帽o de la imagen", options=["640", "1280"], key="size")
|
65 |
+
uploaded_image = st.file_uploader("Cargar imagen", type=["jpg", "jpeg", "png"], key="image")
|
66 |
+
|
67 |
+
# Process uploaded image
|
68 |
+
if uploaded_image is not None:
|
69 |
+
image = Image.open(uploaded_image)
|
70 |
+
result_image, objects_nested_list = detect_objects(size=int(size), iou=iou_threshold, conf=conf_threshold, im=image)
|
71 |
+
object_count = len(objects_nested_list)
|
72 |
+
|
73 |
+
if result_image is not None:
|
74 |
+
col1, col2 = st.columns(2)
|
75 |
+
with col1:
|
76 |
+
st.image(image, caption="Imagen original", use_column_width=True)
|
77 |
+
with col2:
|
78 |
+
st.image(result_image, caption="Resultado", use_column_width=True)
|
79 |
+
|
80 |
+
with st.form("my_form", clear_on_submit=True):
|
81 |
+
st.title("Formulario para feedback")
|
82 |
+
st.write(f'Cantidad detectados: {object_count}')
|
83 |
+
st.table(objects_nested_list)
|
84 |
+
check_status = st.checkbox("驴El resultado contiene la cantidad correcta de figuras detectadas?", value=False)
|
85 |
+
user_text = st.text_input("Ingrese el nombre del usuario que realiz贸 la prueba (m谩ximo 50 caracteres)", max_chars=50)
|
86 |
+
feedback_text = st.text_input("Ingrese su feedback (m谩ximo 100 caracteres)", max_chars=100)
|
87 |
+
# save_button = st.button("Guardar feedback")
|
88 |
+
save_button = st.form_submit_button('Guardar feedback')
|
89 |
+
if save_button:
|
90 |
+
save_feedback(size=int(size), iou=iou_threshold, conf=conf_threshold,
|
91 |
+
object_count_detected=object_count,
|
92 |
+
objects_list=objects_nested_list,
|
93 |
+
user_text=user_text, feedback_text=feedback_text, check_status=check_status)
|
94 |
+
else:
|
95 |
+
st.warning("Error procesando la imagen. Volver a probar.")
|