import streamlit as st import streamlit as st import tensorflow as tf import numpy as np from keras.models import load_model from tensorflow.keras.backend import clear_session import cv2 import os st.set_page_config( page_title = 'Patacotrón', initial_sidebar_state = 'collapsed', menu_items = { "About" : 'Proyecto ideado para la investigación de "Clasificación de imágenes de una sola clase con algortimos de Inteligencia Artificial".', "Report a Bug" : 'https://docs.google.com/forms/d/e/1FAIpQLScH0ZxAV8aSqs7TPYi86u0nkxvQG3iuHCStWNB-BoQnSW2V0g/viewform?usp=sf_link' } ) st.title("Entorno de ejecución") 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.") # Get the absolute path to the current directory current_dir = os.path.abspath(os.path.dirname(__file__)) # Get the absolute path to the parent directory of the current directory root_dir = os.path.abspath(os.path.join(current_dir, os.pardir)) # Join the path to the models folder DIR = os.path.join(root_dir, "models") models = os.listdir(DIR) model_dict = dict() #for model in models: # model_name = model.split(DIR) # model_dict[model_name] = 0 #model_list = [] for model in models: model_name = model.split(DIR) model_name = str(model.split('.h5')[0]) model_dir = os.path.join(DIR, model) model_dict[model_name] = model_dir #model_list.append(os.path.join(DIR, model)) # Create a dropdown menu to select the model model_choice = st.multiselect("Seleccione un modelo de clasificación", model_dict.keys()) selected_models = [] for model in model_choice: selected_models.append(model) def ensemble_model(model_list, img): average = len(model_list) y_gorrito = np.zeros((1, 1)) for model in model_list: instance_model = load_model(model_dict[model]) y_gorrito += float(instance_model.predict(np.expand_dims(img, 0))) clear_session() return y_gorrito/average # Set the image dimensions IMAGE_WIDTH = IMAGE_HEIGHT = 224 # Create a file uploader widget uploaded_file = st.file_uploader("Elige una imagen...", type= ['jpg','png', 'jpeg', 'jfif', 'webp', 'heic']) if uploaded_file is not None: # Load the image and resize it to the required dimensions img = np.frombuffer(uploaded_file.read(), np.uint8) img = cv2.imdecode(img, cv2.IMREAD_COLOR) raw_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.resize(img, (IMAGE_WIDTH, IMAGE_HEIGHT)) # Convert the image to RGB and preprocess it for the model img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = img / 255 # Pass the image to the model and get the prediction with st.spinner('Cargando predicción...'): y_gorrito = ensemble_model(selected_models, img) #y_gorrito = model.predict(np.expand_dims(img, 0)) # Show the image col1, col2, col3 = st.columns(3) with col1: st.write(' ') with col2: #check_type = isinstance(y_gorrito, (int, float)) #if not check_type: #st.write('No se ha escogido ningún modelo.') #else: st.write(f'La probabilidad de que la imagen tenga un patacón es del: {round(float(y_gorrito), 2)*100}%') with col3: st.write('Si los resultados no fueron los esperados, por favor, despliga la barra lateral y entra al botón "Report a Bug"') st.image(raw_img)