Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,31 +14,39 @@ for i in range(3):
|
|
14 |
model.load_model(f'./model_{i}.cbm')
|
15 |
modelos_cargados.append(model)
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
#
|
25 |
-
arr = np.array(image)
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
28 |
return properties
|
29 |
|
30 |
-
def get_array_properties(arr):
|
31 |
-
# Implement the same properties calculation as before
|
32 |
-
area_arr = arr.size
|
33 |
-
mean_arr = arr.mean()
|
34 |
-
std_arr = arr.std()
|
35 |
-
return np.array([area_arr, mean_arr, std_arr])
|
36 |
-
|
37 |
def predecir_desde_imagen(image, modelos, array_cols):
|
38 |
# Process the soil image to extract properties
|
39 |
properties = process_soil_image(image)
|
|
|
40 |
datos_df = pd.DataFrame([properties], columns=array_cols)
|
|
|
41 |
|
|
|
|
|
|
|
|
|
42 |
# Apply PCA and predict as before
|
43 |
pca_datos = get_pca_dataset(datos_df)
|
44 |
|
@@ -52,7 +60,6 @@ def predecir_desde_imagen(image, modelos, array_cols):
|
|
52 |
|
53 |
def predecir_desde_imagen_interface(image):
|
54 |
# Ensure the image is in the expected format
|
55 |
-
image = Image.fromarray(image) # Convert to PIL Image if necessary
|
56 |
predicciones = predecir_desde_imagen(image, modelos_cargados, array_cols)
|
57 |
|
58 |
return {
|
@@ -62,18 +69,6 @@ def predecir_desde_imagen_interface(image):
|
|
62 |
'pH': float(predicciones[3])
|
63 |
}
|
64 |
|
65 |
-
# Define your array columns as before
|
66 |
-
array_cols = ['array_area', *[f'mean_{i}' for i in range(1, 151)],
|
67 |
-
*[f'std_{i}' for i in range(1, 151)],
|
68 |
-
*[f'med_{i}' for i in range(1, 151)],
|
69 |
-
*[f'q1_{i}' for i in range(1, 151)],
|
70 |
-
*[f'q3_{i}' for i in range(1, 151)],
|
71 |
-
*[f'max_{i}' for i in range(1, 151)],
|
72 |
-
*[f'range_{i}' for i in range(1, 151)],
|
73 |
-
*[f'D1_{i}' for i in range(1, 151)],
|
74 |
-
*[f'D10_{i}' for i in range(1, 151)],
|
75 |
-
*[f'IQR_{i}' for i in range(1, 151)]]
|
76 |
-
|
77 |
demo = gr.Interface(
|
78 |
fn=predecir_desde_imagen_interface,
|
79 |
inputs=gr.Image(label="Upload Soil Image"),
|
|
|
14 |
model.load_model(f'./model_{i}.cbm')
|
15 |
modelos_cargados.append(model)
|
16 |
|
17 |
+
# Define your array columns as before
|
18 |
+
array_cols = ['array_area', *[f'mean_{i}' for i in range(1, 151)],
|
19 |
+
*[f'std_{i}' for i in range(1, 151)],
|
20 |
+
*[f'med_{i}' for i in range(1, 151)],
|
21 |
+
*[f'q1_{i}' for i in range(1, 151)],
|
22 |
+
*[f'q3_{i}' for i in range(1, 151)],
|
23 |
+
*[f'max_{i}' for i in range(1, 151)],
|
24 |
+
*[f'range_{i}' for i in range(1, 151)],
|
25 |
+
*[f'D1_{i}' for i in range(1, 151)],
|
26 |
+
*[f'D10_{i}' for i in range(1, 151)],
|
27 |
+
*[f'IQR_{i}' for i in range(1, 151)]]
|
28 |
|
29 |
+
def process_soil_image(image):
|
30 |
+
# Dummy properties extraction (simulate the real extraction)
|
31 |
+
arr = np.array(image) # Convert the image to a numpy array
|
32 |
+
if arr.ndim != 3:
|
33 |
+
raise ValueError("Expected a 3-dimensional array (height, width, channels).")
|
34 |
+
|
35 |
+
# Simulated property extraction (ensure you have 150 properties)
|
36 |
+
properties = np.random.rand(150) # Simulated for demonstration
|
37 |
return properties
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
def predecir_desde_imagen(image, modelos, array_cols):
|
40 |
# Process the soil image to extract properties
|
41 |
properties = process_soil_image(image)
|
42 |
+
print(f"Extracted properties shape: {properties.shape}") # Debug statement
|
43 |
datos_df = pd.DataFrame([properties], columns=array_cols)
|
44 |
+
print(f"DataFrame shape: {datos_df.shape}") # Debug statement
|
45 |
|
46 |
+
# Make sure properties have the same length as array_cols
|
47 |
+
if datos_df.shape[1] != len(array_cols):
|
48 |
+
raise ValueError(f"Expected {len(array_cols)} columns, but got {datos_df.shape[1]}.")
|
49 |
+
|
50 |
# Apply PCA and predict as before
|
51 |
pca_datos = get_pca_dataset(datos_df)
|
52 |
|
|
|
60 |
|
61 |
def predecir_desde_imagen_interface(image):
|
62 |
# Ensure the image is in the expected format
|
|
|
63 |
predicciones = predecir_desde_imagen(image, modelos_cargados, array_cols)
|
64 |
|
65 |
return {
|
|
|
69 |
'pH': float(predicciones[3])
|
70 |
}
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
demo = gr.Interface(
|
73 |
fn=predecir_desde_imagen_interface,
|
74 |
inputs=gr.Image(label="Upload Soil Image"),
|