Spaces:
Running
Running
Commit
·
c80713a
1
Parent(s):
e014372
Update app.py
Browse files
app.py
CHANGED
@@ -49,55 +49,73 @@ 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 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
-
#
|
57 |
-
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
#
|
63 |
-
print(f"Prediction shape: {prediction.shape}")
|
64 |
-
|
65 |
-
# Initialize variables for aggregated predictions and level index
|
66 |
-
aggregated_predictions = None
|
67 |
-
current_level_index = 0 # Start from the species level
|
68 |
-
|
69 |
-
# Determine the initial taxonomic level based on the user's decision
|
70 |
-
if taxonomic_decision == "No, I will let the model decide":
|
71 |
-
current_level_index = 0
|
72 |
-
else:
|
73 |
-
current_level_index = taxonomic_levels.index(taxonomic_level)
|
74 |
-
|
75 |
-
# Loop through taxonomic levels
|
76 |
-
while current_level_index < len(taxonomic_levels):
|
77 |
-
aggregated_predictions, aggregated_class_labels = aggregate_predictions(
|
78 |
-
prediction, taxonomic_levels[current_level_index], class_names
|
79 |
-
)
|
80 |
-
|
81 |
-
# Check if predictions are valid
|
82 |
-
if aggregated_predictions is None or aggregated_predictions.size == 0:
|
83 |
-
return "<h2>No valid predictions available.</h2>"
|
84 |
-
|
85 |
-
top_prediction_index = np.argmax(aggregated_predictions)
|
86 |
-
top_prediction_confidence = aggregated_predictions[0][top_prediction_index]
|
87 |
-
|
88 |
-
if top_prediction_confidence >= 0.80:
|
89 |
-
break
|
90 |
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
-
|
94 |
-
|
95 |
-
predicted_class_name = aggregated_class_labels[predicted_class_index]
|
96 |
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
-
|
100 |
-
return f"<h2>Error during prediction: {str(e)}</h2>"
|
101 |
|
102 |
# Define the Gradio interface
|
103 |
interface = gr.Interface(
|
|
|
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 |
+
|
55 |
+
# Get the class names from the 'species' column
|
56 |
+
class_names = sorted(taxo_df['species'].unique())
|
57 |
+
|
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 = 0 # Start from the species level
|
64 |
+
|
65 |
+
# Determine the initial taxonomic level based on the user's decision
|
66 |
+
if taxonomic_decision == "No, I will let the model decide":
|
67 |
+
current_level_index = 0 # Start at species level if letting the model decide
|
68 |
+
else:
|
69 |
+
current_level_index = taxonomic_levels.index(taxonomic_level) # Use specified level
|
70 |
+
|
71 |
+
# Loop through taxonomic levels to check confidence
|
72 |
+
while current_level_index < len(taxonomic_levels):
|
73 |
+
# Aggregate predictions based on the current taxonomic level
|
74 |
+
aggregated_predictions, aggregated_class_labels = aggregate_predictions(prediction, taxonomic_levels[current_level_index], class_names)
|
75 |
|
76 |
+
# Check if the confidence of the top prediction meets the threshold
|
77 |
+
top_prediction_index = np.argmax(aggregated_predictions)
|
78 |
+
top_prediction_confidence = aggregated_predictions[0][top_prediction_index]
|
79 |
|
80 |
+
if top_prediction_confidence >= 0.80:
|
81 |
+
break # Confidence threshold met, exit loop
|
82 |
+
|
83 |
+
current_level_index += 1 # Move to the next taxonomic level
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
+
# Get the predicted class name for the top prediction
|
86 |
+
predicted_class_index = np.argmax(aggregated_predictions)
|
87 |
+
predicted_class_name = aggregated_class_labels[predicted_class_index]
|
88 |
+
|
89 |
+
# Check if common name should be displayed (only at species level)
|
90 |
+
if taxonomic_levels[current_level_index] == "species":
|
91 |
+
predicted_common_name = taxo_df[taxo_df[taxonomic_levels[current_level_index]] == predicted_class_name]['common_name'].values[0]
|
92 |
+
output_text = f"<h1 style='font-weight: bold;'><span style='font-style: italic;'>{predicted_class_name}</span> ({predicted_common_name})</h1>"
|
93 |
+
else:
|
94 |
+
output_text = f"<h1 style='font-weight: bold;'>{predicted_class_name}</h1>"
|
95 |
+
|
96 |
+
# Add the top 5 predictions
|
97 |
+
output_text += "<h4 style='font-weight: bold; font-size: 1.2em;'>Top 5 Predictions:</h4>"
|
98 |
+
|
99 |
+
top_indices = np.argsort(aggregated_predictions[0])[-5:][::-1] # Get top 5 predictions
|
100 |
|
101 |
+
for i in top_indices:
|
102 |
+
class_name = aggregated_class_labels[i]
|
|
|
103 |
|
104 |
+
if taxonomic_levels[current_level_index] == "species":
|
105 |
+
# Display common names only at species level and make it italic
|
106 |
+
common_name = taxo_df[taxo_df[taxonomic_levels[current_level_index]] == class_name]['common_name'].values[0]
|
107 |
+
confidence_percentage = aggregated_predictions[0][i] * 100
|
108 |
+
output_text += f"<div style='display: flex; justify-content: space-between;'>" \
|
109 |
+
f"<span style='font-style: italic;'>{class_name}</span> (<span>{common_name}</span>)" \
|
110 |
+
f"<span style='margin-left: auto;'>{confidence_percentage:.2f}%</span></div>"
|
111 |
+
else:
|
112 |
+
# No common names at higher taxonomic levels
|
113 |
+
confidence_percentage = aggregated_predictions[0][i] * 100
|
114 |
+
output_text += f"<div style='display: flex; justify-content: space-between;'>" \
|
115 |
+
f"<span>{class_name}</span>" \
|
116 |
+
f"<span style='margin-left: auto;'>{confidence_percentage:.2f}%</span></div>"
|
117 |
|
118 |
+
return output_text
|
|
|
119 |
|
120 |
# Define the Gradio interface
|
121 |
interface = gr.Interface(
|