Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- app.py +96 -0
- best.pt +3 -0
- database_connect.py +23 -0
- requirements.txt +19 -0
app.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
from database_connect import db_connection
|
8 |
+
|
9 |
+
|
10 |
+
class YOLODetect():
|
11 |
+
def __init__(self, modelo):
|
12 |
+
self.modelo = modelo
|
13 |
+
|
14 |
+
def predecir(self, source, imgsz=1280, conf=0.7, iou=0.50):
|
15 |
+
# conf float 0.25 umbral de confianza del objeto para la detecci贸n
|
16 |
+
# iou float 0.7 umbral de intersecci贸n sobre uni贸n (IoU) para NMS
|
17 |
+
self.results = self.modelo.predict(source=source, save=True, imgsz=imgsz, conf=conf, iou=iou)
|
18 |
+
return self.results
|
19 |
+
|
20 |
+
def render(self):
|
21 |
+
result = self.results[0]
|
22 |
+
file_name = os.path.join(result.save_dir, result.path)
|
23 |
+
render = Image.open(file_name)
|
24 |
+
return render
|
25 |
+
|
26 |
+
path_best_model = 'best.pt'
|
27 |
+
modelo_yolo = YOLO(path_best_model)
|
28 |
+
|
29 |
+
def detect_objects(size, iou, conf, im):
|
30 |
+
'''Wrapper fn for gradio'''
|
31 |
+
g = (int(size) / max(im.size)) # gain
|
32 |
+
im = im.resize((int(x * g) for x in im.size), Image.LANCZOS) # resize with antialiasing
|
33 |
+
|
34 |
+
model = YOLODetect(modelo_yolo)
|
35 |
+
results = model.predecir(source=im, imgsz=int(size), conf=conf, iou=iou)
|
36 |
+
|
37 |
+
objects_detected = results[0].boxes.cls.tolist() # Clases detectadas.
|
38 |
+
objects_conf = results[0].boxes.conf.tolist() # Probabilidad de detecci贸n por clase detectada.
|
39 |
+
|
40 |
+
objects_nested_list = pd.DataFrame({'Clase': objects_detected, 'Probabilidad': objects_conf})
|
41 |
+
|
42 |
+
result_img = model.render()
|
43 |
+
return result_img, objects_nested_list
|
44 |
+
|
45 |
+
def save_feedback(size, iou, conf,
|
46 |
+
object_count_detected,
|
47 |
+
objects_list,
|
48 |
+
user_text, feedback_text, check_status):
|
49 |
+
try:
|
50 |
+
db_connection.insert_data('lego.lego_detectado', (size, iou, conf, user_text, feedback_text, check_status))
|
51 |
+
st.success("Se guard贸 el feeback exitosamente.")
|
52 |
+
except Exception as err:
|
53 |
+
print(err)
|
54 |
+
st.warning("Error al guardar el feedback.")
|
55 |
+
|
56 |
+
# Streamlit app layout
|
57 |
+
st.title('YOLOv8 Detecci贸n de figuras LEGO')
|
58 |
+
|
59 |
+
# Input
|
60 |
+
col1, col2 = st.columns(2)
|
61 |
+
with col1:
|
62 |
+
iou_threshold = st.slider("NMS IoU Threshold (0.0 - 1.0)", 0.0, 1.0, 0.8, key="iou")
|
63 |
+
conf_threshold = st.slider("Umbral o threshold (0.0 - 1.0)", 0.0, 1.0, 0.9, key="conf")
|
64 |
+
with col2:
|
65 |
+
size = st.selectbox("Tama帽o de la imagen", options=["640", "1280"], key="size")
|
66 |
+
uploaded_image = st.file_uploader("Cargar imagen", type=["jpg", "jpeg", "png"], key="image")
|
67 |
+
|
68 |
+
# Process uploaded image
|
69 |
+
if uploaded_image is not None:
|
70 |
+
image = Image.open(uploaded_image)
|
71 |
+
result_image, objects_nested_list = detect_objects(size=int(size), iou=iou_threshold, conf=conf_threshold, im=image)
|
72 |
+
object_count = len(objects_nested_list)
|
73 |
+
|
74 |
+
if result_image is not None:
|
75 |
+
col1, col2 = st.columns(2)
|
76 |
+
with col1:
|
77 |
+
st.image(image, caption="Imagen original", use_column_width=True)
|
78 |
+
with col2:
|
79 |
+
st.image(result_image, caption="Resultado", use_column_width=True)
|
80 |
+
|
81 |
+
with st.form("my_form", clear_on_submit=True):
|
82 |
+
st.title("Formulario para feedback")
|
83 |
+
st.write(f'Cantidad detectados: {object_count}')
|
84 |
+
st.table(objects_nested_list)
|
85 |
+
check_status = st.checkbox("驴El resultado contiene la cantidad correcta de figuras detectadas?", value=False)
|
86 |
+
user_text = st.text_input("Ingrese el nombre del usuario que realiz贸 la prueba (m谩ximo 50 caracteres)", max_chars=50)
|
87 |
+
feedback_text = st.text_input("Ingrese su feedback (m谩ximo 100 caracteres)", max_chars=100)
|
88 |
+
# save_button = st.button("Guardar feedback")
|
89 |
+
save_button = st.form_submit_button('Guardar feedback')
|
90 |
+
if save_button:
|
91 |
+
save_feedback(size=int(size), iou=iou_threshold, conf=conf_threshold,
|
92 |
+
object_count_detected=object_count,
|
93 |
+
objects_list=objects_nested_list,
|
94 |
+
user_text=user_text, feedback_text=feedback_text, check_status=check_status)
|
95 |
+
else:
|
96 |
+
st.warning("Error procesando la imagen. Volver a probar.")
|
best.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:380f9383305dce76f36c8f552ce94e2518f3e7ab1b6aafbe4754935f55a2aeeb
|
3 |
+
size 136704041
|
database_connect.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import pymssql
|
3 |
+
|
4 |
+
|
5 |
+
class DatabaseConnection:
|
6 |
+
def __init__(self, server, database, username, password):
|
7 |
+
self.connection = pymssql.connect(server, username, password, database)
|
8 |
+
|
9 |
+
def insert_data(self, table_name, data_values):
|
10 |
+
columnas = ('img_size', 'intersect_over_union', 'probabilidad', 'user_text', 'feedback_text', 'check_status')
|
11 |
+
insert_query = f"INSERT INTO {table_name} ({','.join([columna for columna in columnas])}) VALUES ({','.join(['%s' for _ in data_values])})"
|
12 |
+
print(insert_query)
|
13 |
+
cursor = self.connection.cursor()
|
14 |
+
cursor.execute(insert_query, data_values)
|
15 |
+
self.connection.commit()
|
16 |
+
cursor.close()
|
17 |
+
|
18 |
+
server = os.environ.get('SERVER') # Reemplaza con el nombre de tu servidor
|
19 |
+
database = os.environ.get('DATABASE') # Reemplaza con el nombre de tu base de datos
|
20 |
+
username = os.environ.get('USERNAME') # Reemplaza con tu nombre de usuario
|
21 |
+
password = os.environ.get('PASSWORD') # Reemplaza con tu contrase帽a
|
22 |
+
|
23 |
+
db_connection = DatabaseConnection(server, database, username, password)
|
requirements.txt
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit==1.33.0
|
2 |
+
ultralytics
|
3 |
+
ultralyticsplus
|
4 |
+
gradio
|
5 |
+
IPython
|
6 |
+
matplotlib>=3.2.2
|
7 |
+
numpy>=1.18.5
|
8 |
+
opencv-python-headless
|
9 |
+
pillow
|
10 |
+
psutil
|
11 |
+
PyYAML>=5.3.1
|
12 |
+
scipy>=1.4.1
|
13 |
+
torch>=1.7.0
|
14 |
+
torchvision>=0.8.1
|
15 |
+
tqdm>=4.41.0
|
16 |
+
pandas
|
17 |
+
thop # FLOPs computation
|
18 |
+
cython==0.29.35
|
19 |
+
pymssql==2.2.7
|