Spaces:
Running
Running
Update pages/Entorno de Ejecución.py
Browse files- pages/Entorno de Ejecución.py +71 -76
pages/Entorno de Ejecución.py
CHANGED
@@ -15,92 +15,87 @@ st.set_page_config(
|
|
15 |
}
|
16 |
)
|
17 |
|
|
|
18 |
|
19 |
-
|
20 |
-
st.markdown("Los modelos no están en orden de eficacia, sino en orden de creación. En la pestaña de Estadísticas podrá encontrar más información.")
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
# Get the absolute path to the parent directory of the current directory
|
26 |
-
root_dir = os.path.abspath(os.path.join(current_dir, os.pardir))
|
27 |
-
|
28 |
-
# Join the path to the models folder
|
29 |
-
DIR = os.path.join(root_dir, "models")
|
30 |
-
|
31 |
-
threshold = .8
|
32 |
-
|
33 |
-
ultra_button = st.checkbox('ultraptctrn: el mejor ensamble de modelos hasta la fecha.')
|
34 |
-
ultra_flag = False
|
35 |
-
if ultra_button:
|
36 |
-
ultra_flag = True
|
37 |
-
|
38 |
-
models = os.listdir(DIR)
|
39 |
-
|
40 |
-
model_dict = dict()
|
41 |
-
for model in models:
|
42 |
-
model_name = model.split(DIR)
|
43 |
-
model_name = str(model.split('.h5')[0])
|
44 |
-
model_dir = os.path.join(DIR, model)
|
45 |
-
model_dict[model_name] = model_dir
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
img = cv2.resize(img, (IMAGE_WIDTH, IMAGE_HEIGHT))
|
77 |
-
|
78 |
-
# Convert the image to RGB and preprocess it for the model
|
79 |
-
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
80 |
-
img = img / 255.
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
y_gorrito
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
-
|
91 |
-
col1, col2, col3 = st.columns(3)
|
92 |
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
-
with col2:
|
97 |
if y_gorrito > threshold:
|
98 |
st.success("¡Patacón Detectado!")
|
99 |
else:
|
100 |
st.error("No se encontró rastro de patacón.")
|
101 |
st.caption(f'La probabilidad de que la imagen tenga un patacón es del: {round(float(y_gorrito), 2)*100}%')
|
102 |
#st.write('Si los resultados no fueron los esperados, por favor, despliga la barra lateral y entra al botón "Report a Bug"')
|
103 |
-
st.image(raw_img)
|
104 |
-
|
105 |
-
with col3:
|
106 |
-
st.write(' ')
|
|
|
15 |
}
|
16 |
)
|
17 |
|
18 |
+
col_a, col_b, = st.columns(2)
|
19 |
|
20 |
+
with col_a:
|
|
|
21 |
|
22 |
+
st.title("Entorno de ejecución")
|
23 |
+
st.markdown("Los modelos no están en orden de eficacia, sino en orden de creación. En la pestaña de Estadísticas podrá encontrar más información.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
# Get the absolute path to the current directory
|
26 |
+
current_dir = os.path.abspath(os.path.dirname(__file__))
|
27 |
+
|
28 |
+
# Get the absolute path to the parent directory of the current directory
|
29 |
+
root_dir = os.path.abspath(os.path.join(current_dir, os.pardir))
|
30 |
+
|
31 |
+
# Join the path to the models folder
|
32 |
+
DIR = os.path.join(root_dir, "models")
|
33 |
+
|
34 |
+
threshold = .8
|
35 |
+
|
36 |
+
ultra_button = st.checkbox('ultraptctrn: el mejor ensamble de modelos hasta la fecha.')
|
37 |
+
ultra_flag = False
|
38 |
+
if ultra_button:
|
39 |
+
ultra_flag = True
|
40 |
+
|
41 |
+
models = os.listdir(DIR)
|
42 |
+
|
43 |
+
model_dict = dict()
|
44 |
+
for model in models:
|
45 |
+
model_name = model.split(DIR)
|
46 |
+
model_name = str(model.split('.h5')[0])
|
47 |
+
model_dir = os.path.join(DIR, model)
|
48 |
+
model_dict[model_name] = model_dir
|
49 |
+
|
50 |
+
ultraptctrn = ['ptctrn_v1.8', 'ptctrn_v1.9.1', 'ptctrn_v1.12']
|
51 |
+
|
52 |
+
# Create a dropdown menu to select the model
|
53 |
+
model_choice = st.multiselect("Seleccione un modelo de clasificación", model_dict.keys())
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
selected_models = []
|
56 |
+
|
57 |
+
def ensemble_model(model_list, img):
|
58 |
+
y_gorrito = np.zeros((1, 1))
|
59 |
+
for model in model_list:
|
60 |
+
instance_model = load_model(model_dict[model])
|
61 |
+
y_gorrito += float(instance_model.predict(np.expand_dims(img, 0)))
|
62 |
+
clear_session()
|
63 |
+
return y_gorrito/len(model_list)
|
64 |
+
|
65 |
+
for model in model_choice:
|
66 |
+
selected_models.append(model)
|
67 |
+
|
68 |
+
# Set the image dimensions
|
69 |
+
IMAGE_WIDTH = IMAGE_HEIGHT = 224
|
70 |
+
|
71 |
+
# Create a file uploader widget
|
72 |
+
uploaded_file = st.file_uploader("Elige una imagen...", type= ['jpg','png', 'jpeg', 'jfif', 'webp', 'heic'])
|
73 |
|
74 |
+
with col_b:
|
|
|
75 |
|
76 |
+
if uploaded_file is not None:
|
77 |
+
# Load the image and resize it to the required dimensions
|
78 |
+
img = np.frombuffer(uploaded_file.read(), np.uint8)
|
79 |
+
img = cv2.imdecode(img, cv2.IMREAD_COLOR)
|
80 |
+
raw_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
81 |
+
img = cv2.resize(img, (IMAGE_WIDTH, IMAGE_HEIGHT))
|
82 |
+
|
83 |
+
# Convert the image to RGB and preprocess it for the model
|
84 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
85 |
+
img = img / 255.
|
86 |
+
|
87 |
+
# Pass the image to the model and get the prediction
|
88 |
+
if ultra_flag:
|
89 |
+
with st.spinner('Cargando ultra-predicción...'):
|
90 |
+
y_gorrito = ensemble_model(ultraptctrn, img)
|
91 |
+
else:
|
92 |
+
with st.spinner('Cargando predicción...'):
|
93 |
+
y_gorrito = ensemble_model(selected_models, img)
|
94 |
|
|
|
95 |
if y_gorrito > threshold:
|
96 |
st.success("¡Patacón Detectado!")
|
97 |
else:
|
98 |
st.error("No se encontró rastro de patacón.")
|
99 |
st.caption(f'La probabilidad de que la imagen tenga un patacón es del: {round(float(y_gorrito), 2)*100}%')
|
100 |
#st.write('Si los resultados no fueron los esperados, por favor, despliga la barra lateral y entra al botón "Report a Bug"')
|
101 |
+
st.image(raw_img)
|
|
|
|
|
|