Spaces:
Sleeping
Sleeping
Commit
·
0c48d27
1
Parent(s):
5ebfe0b
Update app.py
Browse files
app.py
CHANGED
@@ -7,9 +7,65 @@ from tensorflow.keras.preprocessing.image import load_img, img_to_array # type:
|
|
7 |
from tensorflow.keras.applications.convnext import preprocess_input # type: ignore
|
8 |
import gradio as gr
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
# Define the Gradio interface
|
11 |
interface = gr.Interface(
|
12 |
-
fn=
|
13 |
inputs=gr.Image(type="pil"), # Input type: Image (PIL format)
|
14 |
outputs="html", # Output type: HTML for formatting
|
15 |
title="Amazon arboreal species classification",
|
@@ -17,4 +73,4 @@ interface = gr.Interface(
|
|
17 |
)
|
18 |
|
19 |
# Launch the Gradio interface
|
20 |
-
interface.launch()
|
|
|
7 |
from tensorflow.keras.applications.convnext import preprocess_input # type: ignore
|
8 |
import gradio as gr
|
9 |
|
10 |
+
# Load the model
|
11 |
+
model = load_model('models/ConvNeXtBase_80_tresh_spp.tf')
|
12 |
+
|
13 |
+
# Load the taxonomy .csv
|
14 |
+
taxo_df = pd.read_csv('taxonomy/taxonomy_mapping.csv', sep=';')
|
15 |
+
taxo_df['species'] = taxo_df['species'].str.replace('_', ' ')
|
16 |
+
|
17 |
+
# Extract unique class names from the 'species' column
|
18 |
+
class_names = sorted(taxo_df['species'].unique())
|
19 |
+
|
20 |
+
# Function to map predicted class index to class name
|
21 |
+
def get_class_name(predicted_class):
|
22 |
+
return class_names[predicted_class]
|
23 |
+
|
24 |
+
# Function to load and preprocess the image
|
25 |
+
def load_and_preprocess_image(image, target_size=(224, 224)):
|
26 |
+
# Resize the image (assuming image is a PIL image)
|
27 |
+
img_array = img_to_array(image.resize(target_size))
|
28 |
+
# Expand the dimensions of the array to match model input
|
29 |
+
img_array = np.expand_dims(img_array, axis=0)
|
30 |
+
# Preprocess using the appropriate function (for example, ResNet50)
|
31 |
+
img_array = preprocess_input(img_array)
|
32 |
+
return img_array
|
33 |
+
|
34 |
+
# Function to make predictions
|
35 |
+
def make_prediction(image):
|
36 |
+
# Preprocess the image
|
37 |
+
img_array = load_and_preprocess_image(image)
|
38 |
+
# Make a prediction
|
39 |
+
prediction = model.predict(img_array)
|
40 |
+
|
41 |
+
# Get the top 5 predictions
|
42 |
+
top_indices = np.argsort(prediction[0])[-5:][::-1] # Get indices of top 5 classes
|
43 |
+
|
44 |
+
# Get predicted class and common name for the top prediction
|
45 |
+
predicted_class_index = np.argmax(prediction)
|
46 |
+
predicted_class_name = get_class_name(predicted_class_index)
|
47 |
+
predicted_common_name = taxo_df[taxo_df['species'] == predicted_class_name]['common_name'].values[0] # Get common name
|
48 |
+
confidence = prediction[0][predicted_class_index] * 100 # Confidence of the predicted class
|
49 |
+
|
50 |
+
# Create output text with HTML formatting
|
51 |
+
output_text = f"<h1 style='font-weight: bold;'><span style='font-style: italic;'>{predicted_class_name}</span> ({predicted_common_name})</h1>" # Large bold for predicted class, italic for class name
|
52 |
+
output_text += "<h4 style='font-weight: bold; font-size: 1.2em;'>Top 5 Predictions:</h4>" # Bold and larger font for predictions
|
53 |
+
|
54 |
+
for i in top_indices:
|
55 |
+
class_name = get_class_name(i)
|
56 |
+
common_name = taxo_df[taxo_df['species'] == class_name]['common_name'].values[0] # Get common name from CSV
|
57 |
+
confidence_percentage = prediction[0][i] * 100
|
58 |
+
|
59 |
+
# Format the output with space between class name and common name
|
60 |
+
output_text += f"<div style='display: flex; justify-content: space-between;'>" \
|
61 |
+
f"<span style='font-style: italic;'>{class_name}</span> (<span>{common_name}</span>)" \
|
62 |
+
f"<span style='margin-left: auto;'>{confidence_percentage:.2f}%</span></div>"
|
63 |
+
|
64 |
+
return output_text
|
65 |
+
|
66 |
# Define the Gradio interface
|
67 |
interface = gr.Interface(
|
68 |
+
fn=make_prediction, # Function to be called for predictions
|
69 |
inputs=gr.Image(type="pil"), # Input type: Image (PIL format)
|
70 |
outputs="html", # Output type: HTML for formatting
|
71 |
title="Amazon arboreal species classification",
|
|
|
73 |
)
|
74 |
|
75 |
# Launch the Gradio interface
|
76 |
+
interface.launch()
|