Spaces:
Sleeping
Sleeping
Commit
·
68bd012
1
Parent(s):
ad3d1fe
Update app.py
Browse files
app.py
CHANGED
@@ -49,12 +49,6 @@ def load_and_preprocess_image(image, target_size=(224, 224)):
|
|
49 |
|
50 |
# Function to make predictions
|
51 |
def make_prediction(image, taxonomic_decision, taxonomic_level):
|
52 |
-
# Determine the taxonomic level based on the user's decision
|
53 |
-
if taxonomic_decision == "Yes, I want to specify the taxonomic level":
|
54 |
-
taxonomic_level = taxonomic_level # Use the level specified in the dropdown
|
55 |
-
else:
|
56 |
-
taxonomic_level = "species" # Default to species if not specified
|
57 |
-
|
58 |
# Preprocess the image
|
59 |
img_array = load_and_preprocess_image(image)
|
60 |
|
@@ -64,19 +58,31 @@ def make_prediction(image, taxonomic_decision, taxonomic_level):
|
|
64 |
# Make a prediction
|
65 |
prediction = model.predict(img_array)
|
66 |
|
67 |
-
#
|
68 |
-
aggregated_predictions
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
predicted_class_index = np.argmax(aggregated_predictions)
|
75 |
predicted_class_name = aggregated_class_labels[predicted_class_index]
|
76 |
|
77 |
# Check if common name should be displayed (only at species level)
|
78 |
-
if
|
79 |
-
predicted_common_name = taxo_df[taxo_df[
|
80 |
output_text = f"<h1 style='font-weight: bold;'><span style='font-style: italic;'>{predicted_class_name}</span> ({predicted_common_name})</h1>"
|
81 |
else:
|
82 |
output_text = f"<h1 style='font-weight: bold;'>{predicted_class_name}</h1>"
|
@@ -84,12 +90,14 @@ def make_prediction(image, taxonomic_decision, taxonomic_level):
|
|
84 |
# Add the top 5 predictions
|
85 |
output_text += "<h4 style='font-weight: bold; font-size: 1.2em;'>Top 5 Predictions:</h4>"
|
86 |
|
|
|
|
|
87 |
for i in top_indices:
|
88 |
class_name = aggregated_class_labels[i]
|
89 |
|
90 |
-
if
|
91 |
# Display common names only at species level and make it italic
|
92 |
-
common_name = taxo_df[taxo_df[
|
93 |
confidence_percentage = aggregated_predictions[0][i] * 100
|
94 |
output_text += f"<div style='display: flex; justify-content: space-between;'>" \
|
95 |
f"<span style='font-style: italic;'>{class_name}</span> (<span>{common_name}</span>)" \
|
@@ -111,7 +119,7 @@ interface = gr.Interface(
|
|
111 |
gr.Radio(choices=["Yes, I want to specify the taxonomic level", "No, I will let the model decide"],
|
112 |
label="Do you want to specify the taxonomic resolution for predictions? If you select 'No', the next drop-down menu will be bypassed.",
|
113 |
value="No, I will let the model decide"), # Radio button for taxonomic resolution choice
|
114 |
-
gr.Dropdown(choices=taxonomic_levels, label="Taxonomic level", value="species") # Dropdown for taxonomic level
|
115 |
],
|
116 |
outputs="html", # Output type: HTML for formatting
|
117 |
title="Amazon arboreal species classification",
|
|
|
49 |
|
50 |
# Function to make predictions
|
51 |
def make_prediction(image, taxonomic_decision, taxonomic_level):
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
# Preprocess the image
|
53 |
img_array = load_and_preprocess_image(image)
|
54 |
|
|
|
58 |
# Make a prediction
|
59 |
prediction = model.predict(img_array)
|
60 |
|
61 |
+
# Initialize variables for aggregated predictions and level index
|
62 |
+
aggregated_predictions = None
|
63 |
+
current_level_index = taxonomic_levels.index(taxonomic_level)
|
64 |
+
|
65 |
+
# Loop through taxonomic levels to check confidence
|
66 |
+
while current_level_index < len(taxonomic_levels):
|
67 |
+
# Aggregate predictions based on the current taxonomic level
|
68 |
+
aggregated_predictions, aggregated_class_labels = aggregate_predictions(prediction, taxonomic_levels[current_level_index], class_names)
|
69 |
+
|
70 |
+
# Check if the confidence of the top prediction meets the threshold
|
71 |
+
top_prediction_index = np.argmax(aggregated_predictions)
|
72 |
+
top_prediction_confidence = aggregated_predictions[0][top_prediction_index]
|
73 |
+
|
74 |
+
if top_prediction_confidence >= 0.80:
|
75 |
+
break # Confidence threshold met, exit loop
|
76 |
+
|
77 |
+
current_level_index += 1 # Move to the next taxonomic level
|
78 |
+
|
79 |
+
# Get the predicted class name for the top prediction
|
80 |
predicted_class_index = np.argmax(aggregated_predictions)
|
81 |
predicted_class_name = aggregated_class_labels[predicted_class_index]
|
82 |
|
83 |
# Check if common name should be displayed (only at species level)
|
84 |
+
if taxonomic_levels[current_level_index] == "species":
|
85 |
+
predicted_common_name = taxo_df[taxo_df[taxonomic_levels[current_level_index]] == predicted_class_name]['common_name'].values[0]
|
86 |
output_text = f"<h1 style='font-weight: bold;'><span style='font-style: italic;'>{predicted_class_name}</span> ({predicted_common_name})</h1>"
|
87 |
else:
|
88 |
output_text = f"<h1 style='font-weight: bold;'>{predicted_class_name}</h1>"
|
|
|
90 |
# Add the top 5 predictions
|
91 |
output_text += "<h4 style='font-weight: bold; font-size: 1.2em;'>Top 5 Predictions:</h4>"
|
92 |
|
93 |
+
top_indices = np.argsort(aggregated_predictions[0])[-5:][::-1] # Get top 5 predictions
|
94 |
+
|
95 |
for i in top_indices:
|
96 |
class_name = aggregated_class_labels[i]
|
97 |
|
98 |
+
if taxonomic_levels[current_level_index] == "species":
|
99 |
# Display common names only at species level and make it italic
|
100 |
+
common_name = taxo_df[taxo_df[taxonomic_levels[current_level_index]] == class_name]['common_name'].values[0]
|
101 |
confidence_percentage = aggregated_predictions[0][i] * 100
|
102 |
output_text += f"<div style='display: flex; justify-content: space-between;'>" \
|
103 |
f"<span style='font-style: italic;'>{class_name}</span> (<span>{common_name}</span>)" \
|
|
|
119 |
gr.Radio(choices=["Yes, I want to specify the taxonomic level", "No, I will let the model decide"],
|
120 |
label="Do you want to specify the taxonomic resolution for predictions? If you select 'No', the next drop-down menu will be bypassed.",
|
121 |
value="No, I will let the model decide"), # Radio button for taxonomic resolution choice
|
122 |
+
gr.Dropdown(choices=taxonomic_levels, label="Taxonomic level:", value="species") # Dropdown for taxonomic level
|
123 |
],
|
124 |
outputs="html", # Output type: HTML for formatting
|
125 |
title="Amazon arboreal species classification",
|