frncscp commited on
Commit
8a1e479
·
1 Parent(s): 5029012

Update pages/Entorno de Ejecución.py

Browse files
Files changed (1) hide show
  1. pages/Entorno de Ejecución.py +30 -9
pages/Entorno de Ejecución.py CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
2
  import tensorflow as tf
3
  from tensorflow.keras.models import load_model
4
  import os
 
5
 
6
  st.set_page_config(
7
  page_title = 'Patacotrón',
@@ -55,10 +56,19 @@ with cnn:
55
  @tf.function
56
  def predict(model_list, img):
57
  y_gorrito = 0
 
 
58
  for model in model_list:
59
  y_gorrito += tf.cast(model(tf.expand_dims(img/255., 0)), dtype=tf.float32)
60
- return y_gorrito / len(model_list)
61
-
 
 
 
 
 
 
 
62
  # Set the image dimensions
63
  IMAGE_WIDTH = IMAGE_HEIGHT = 224
64
 
@@ -71,22 +81,33 @@ with cnn:
71
  if st.button('¿Hay un patacón en la imagen?'):
72
  if len(selected_models) > 0 and ultra_flag:
73
  st.write('Debe elegir un solo método: Ultra-Patacotrón o selección múltiple.')
 
74
  elif uploaded_file is not None:
75
-
76
- raw_img = tf.image.decode_image(uploaded_file.read(), channels=3)
77
- img = tf.image.resize(raw_img,(IMAGE_WIDTH, IMAGE_HEIGHT))
78
-
79
- # Pass the image to the model and get the prediction
80
  if ultra_flag:
81
  with st.spinner('Cargando ultra-predicción...'):
82
  if not executed:
83
  ultraptctrn = [load_model(model_dict[model]) for model in ultraversions]
84
  executed = True
85
- y_gorrito = predict(ultraptctrn, img)
 
 
 
 
 
 
 
 
 
 
 
 
86
  else:
87
  with st.spinner('Cargando predicción...'):
88
  selected_models = [load_model(model_dict[model]) for model in model_choice if model not in selected_models]
89
- y_gorrito = predict(selected_models, img)
 
 
 
90
 
91
  if round(float(y_gorrito*100)) >= threshold:
92
  st.success("¡Patacón Detectado!")
 
2
  import tensorflow as tf
3
  from tensorflow.keras.models import load_model
4
  import os
5
+ import cv2
6
 
7
  st.set_page_config(
8
  page_title = 'Patacotrón',
 
56
  @tf.function
57
  def predict(model_list, img):
58
  y_gorrito = 0
59
+ raw_img = tf.image.decode_image(img, channels=3)
60
+ img = tf.image.resize(raw_img,(IMAGE_WIDTH, IMAGE_HEIGHT))
61
  for model in model_list:
62
  y_gorrito += tf.cast(model(tf.expand_dims(img/255., 0)), dtype=tf.float32)
63
+ return [y_gorrito / len(model_list), raw_img]
64
+
65
+ def preprocess(file_uploader): #converts it to .png
66
+ img = cv2.imdecode(bytearray(file_uploader.read()), cv2.IMREAD_COLOR)
67
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
68
+ img_tensor = tf.convert_to_tensor(img, dtype=tf.uint8)
69
+ png_bytes = tf.io.encode_png(img_tensor)
70
+ return png_bytes
71
+
72
  # Set the image dimensions
73
  IMAGE_WIDTH = IMAGE_HEIGHT = 224
74
 
 
81
  if st.button('¿Hay un patacón en la imagen?'):
82
  if len(selected_models) > 0 and ultra_flag:
83
  st.write('Debe elegir un solo método: Ultra-Patacotrón o selección múltiple.')
84
+
85
  elif uploaded_file is not None:
 
 
 
 
 
86
  if ultra_flag:
87
  with st.spinner('Cargando ultra-predicción...'):
88
  if not executed:
89
  ultraptctrn = [load_model(model_dict[model]) for model in ultraversions]
90
  executed = True
91
+ try:
92
+ y_gorrito, raw_img = predict(ultraptctrn, img)
93
+ except:
94
+ y_gorrito, raw_img = predict(ultraptctrn, preprocess(img))
95
+
96
+
97
+ # Pass the image to the model and get the prediction
98
+ # if ultra_flag:
99
+ # with st.spinner('Cargando ultra-predicción...'):
100
+ # if not executed:
101
+ # ultraptctrn = [load_model(model_dict[model]) for model in ultraversions]
102
+ # executed = True
103
+ #y_gorrito = predict(ultraptctrn, img)
104
  else:
105
  with st.spinner('Cargando predicción...'):
106
  selected_models = [load_model(model_dict[model]) for model in model_choice if model not in selected_models]
107
+ try:
108
+ y_gorrito, raw_img = predict(selected_models, img)
109
+ except:
110
+ y_gorrito, raw_img = predict(selected_models, preprocess(img))
111
 
112
  if round(float(y_gorrito*100)) >= threshold:
113
  st.success("¡Patacón Detectado!")