frncscp commited on
Commit
dc41bf1
·
1 Parent(s): dfb0a52

Update pages/Entorno de Ejecución.py

Browse files
Files changed (1) hide show
  1. pages/Entorno de Ejecución.py +19 -13
pages/Entorno de Ejecución.py CHANGED
@@ -54,20 +54,25 @@ with cnn:
54
  selected_models = []
55
 
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
@@ -83,15 +88,16 @@ with cnn:
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, uploaded_file)
93
  except:
94
- y_gorrito, raw_img = predict(ultraptctrn, preprocess(uploaded_file))
95
 
96
 
97
  # Pass the image to the model and get the prediction
@@ -105,9 +111,9 @@ with cnn:
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, uploaded_file)
109
  except:
110
- y_gorrito, raw_img = predict(selected_models, preprocess(uploaded_file))
111
 
112
  if round(float(y_gorrito*100)) >= threshold:
113
  st.success("¡Patacón Detectado!")
 
54
  selected_models = []
55
 
56
  @tf.function
57
+ def tf_predict(model_list, img): #faster, but for few formats
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 basic_predict(model_list, img): #for non-supported formats
66
+ raw_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
67
+ img = cv2.resize(img, (IMAGE_WIDTH, IMAGE_HEIGHT))
68
+ for model in model_list:
69
+ y_gorrito += tf.cast(model(tf.expand_dims(img/255., 0)), dtype=tf.float32)
70
+ return [y_gorrito / len(model_list), raw_img]
71
+
72
+ def preprocess(file_uploader): #makes the uploaded image readable
73
+ img = np.frombuffer(uploaded_file.read(), np.uint8)
74
+ img = cv2.imdecode(img, cv2.IMREAD_COLOR)
75
+ return img
76
 
77
  # Set the image dimensions
78
  IMAGE_WIDTH = IMAGE_HEIGHT = 224
 
88
  st.write('Debe elegir un solo método: Ultra-Patacotrón o selección múltiple.')
89
 
90
  elif uploaded_file is not None:
91
+ img = preprocess(uploaded_file)
92
  if ultra_flag:
93
  with st.spinner('Cargando ultra-predicción...'):
94
  if not executed:
95
  ultraptctrn = [load_model(model_dict[model]) for model in ultraversions]
96
  executed = True
97
  try:
98
+ y_gorrito, raw_img = tf_predict(ultraptctrn, img)
99
  except:
100
+ y_gorrito, raw_img = basic_predict(ultraptctrn, img)
101
 
102
 
103
  # Pass the image to the model and get the prediction
 
111
  with st.spinner('Cargando predicción...'):
112
  selected_models = [load_model(model_dict[model]) for model in model_choice if model not in selected_models]
113
  try:
114
+ y_gorrito, raw_img = tf_predict(selected_models, img)
115
  except:
116
+ y_gorrito, raw_img = basic_predict(selected_models, img)
117
 
118
  if round(float(y_gorrito*100)) >= threshold:
119
  st.success("¡Patacón Detectado!")