Spaces:
Running
Running
Update pages/Entorno de Ejecución.py
Browse files- pages/Entorno de Ejecución.py +26 -11
pages/Entorno de Ejecución.py
CHANGED
@@ -10,7 +10,7 @@ import sklearn
|
|
10 |
from PIL import Image
|
11 |
from sklearn.decomposition import PCA
|
12 |
from tensorflow.keras.models import load_model
|
13 |
-
from transformers import AutoConfig, AutoModel, pipeline, Dinov2Model
|
14 |
|
15 |
|
16 |
token = os.environ['token']
|
@@ -20,15 +20,17 @@ st.set_page_config(
|
|
20 |
layout = 'wide',
|
21 |
menu_items = {
|
22 |
"About" : 'Proyecto ideado para la investigación de "Clasificación de imágenes de una sola clase con algortimos de Inteligencia Artificial".',
|
23 |
-
"Report a Bug" : '
|
24 |
}
|
25 |
)
|
26 |
|
27 |
st.sidebar.write("contact@patacotron.tech")
|
28 |
-
already_excuted = False
|
29 |
cnn, vit, zero_shot, classic_ml = st.tabs(["CNN", "ViT", "Zero-Shot", "Machine Learning Clásico"])
|
30 |
classic_ml_root = "/home/user/app/classicML"
|
31 |
-
|
|
|
|
|
|
|
32 |
|
33 |
def _predict(_model_list, _weights, _img):
|
34 |
y_gorrito = 0
|
@@ -38,14 +40,24 @@ def _predict(_model_list, _weights, _img):
|
|
38 |
y_gorrito += tf.cast(model(tf.expand_dims(img/255., 0)), dtype=tf.float32)*weight
|
39 |
return [y_gorrito / sum(_weights), raw_img]
|
40 |
|
41 |
-
def
|
42 |
y_gorrito = 0
|
43 |
raw_img = cv2.cvtColor(_img, cv2.COLOR_BGR2RGB)
|
44 |
-
img = cv2.resize(_img, (IMAGE_WIDTH, IMAGE_HEIGHT))
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
def preprocess(file_uploader, module = 'cv2'): #makes the uploaded image readable
|
51 |
img = np.frombuffer(uploaded_file.read(), np.uint8)
|
@@ -281,6 +293,8 @@ with zero_shot:
|
|
281 |
st.write("Asegúrate de haber subido correctamente la imagen.")
|
282 |
|
283 |
with classic_ml:
|
|
|
|
|
284 |
col_a, col_b = st.columns(2)
|
285 |
with col_a:
|
286 |
|
@@ -311,7 +325,8 @@ with classic_ml:
|
|
311 |
with st.spinner('Cargando predicción...'):
|
312 |
|
313 |
img = preprocess(uploaded_file)
|
314 |
-
|
|
|
315 |
|
316 |
if round(float(y_gorrito*100)) >= threshold:
|
317 |
st.success("¡Patacón Detectado!")
|
|
|
10 |
from PIL import Image
|
11 |
from sklearn.decomposition import PCA
|
12 |
from tensorflow.keras.models import load_model
|
13 |
+
from transformers import AutoConfig, AutoModel, pipeline, Dinov2Model
|
14 |
|
15 |
|
16 |
token = os.environ['token']
|
|
|
20 |
layout = 'wide',
|
21 |
menu_items = {
|
22 |
"About" : 'Proyecto ideado para la investigación de "Clasificación de imágenes de una sola clase con algortimos de Inteligencia Artificial".',
|
23 |
+
"Report a Bug" : 'contact@patacotron.tech'
|
24 |
}
|
25 |
)
|
26 |
|
27 |
st.sidebar.write("contact@patacotron.tech")
|
|
|
28 |
cnn, vit, zero_shot, classic_ml = st.tabs(["CNN", "ViT", "Zero-Shot", "Machine Learning Clásico"])
|
29 |
classic_ml_root = "/home/user/app/classicML"
|
30 |
+
|
31 |
+
@st.cache_resource
|
32 |
+
def load_pca():
|
33 |
+
pca = joblib.load(os.path.join(classic_ml_root, "pca_model.pkl"))
|
34 |
|
35 |
def _predict(_model_list, _weights, _img):
|
36 |
y_gorrito = 0
|
|
|
40 |
y_gorrito += tf.cast(model(tf.expand_dims(img/255., 0)), dtype=tf.float32)*weight
|
41 |
return [y_gorrito / sum(_weights), raw_img]
|
42 |
|
43 |
+
def _pca_predict(models, _img):
|
44 |
y_gorrito = 0
|
45 |
raw_img = cv2.cvtColor(_img, cv2.COLOR_BGR2RGB)
|
46 |
+
img = cv2.resize(_img, (IMAGE_WIDTH, IMAGE_HEIGHT))
|
47 |
+
fl_img =[img.flatten()]
|
48 |
+
data = pca.transform(fl_img)
|
49 |
+
for model in models:
|
50 |
+
y_gorrito += model.predict_proba(data)[0][Categories.index("Patacon-True")]
|
51 |
+
return [y_gorrito / len(models), raw_img]
|
52 |
+
|
53 |
+
#def classic_ml_prediction(clfs, _img):
|
54 |
+
# y_gorrito = 0
|
55 |
+
# raw_img = cv2.cvtColor(_img, cv2.COLOR_BGR2RGB)
|
56 |
+
# img = cv2.resize(_img, (IMAGE_WIDTH, IMAGE_HEIGHT)).flatten()
|
57 |
+
# data = pca.transform(img.reshape(1, -1))
|
58 |
+
# for clf in clfs:
|
59 |
+
# y_gorrito += clf.predict(data)
|
60 |
+
# return [y_gorrito / len(clfs), raw_img]
|
61 |
|
62 |
def preprocess(file_uploader, module = 'cv2'): #makes the uploaded image readable
|
63 |
img = np.frombuffer(uploaded_file.read(), np.uint8)
|
|
|
293 |
st.write("Asegúrate de haber subido correctamente la imagen.")
|
294 |
|
295 |
with classic_ml:
|
296 |
+
|
297 |
+
load_pca()
|
298 |
col_a, col_b = st.columns(2)
|
299 |
with col_a:
|
300 |
|
|
|
325 |
with st.spinner('Cargando predicción...'):
|
326 |
|
327 |
img = preprocess(uploaded_file)
|
328 |
+
model_list = [joblib.load(directory) for directory in model_choice]
|
329 |
+
y_gorrito, raw_img = _pca_predict(model_list, img)
|
330 |
|
331 |
if round(float(y_gorrito*100)) >= threshold:
|
332 |
st.success("¡Patacón Detectado!")
|