Spaces:
Sleeping
Sleeping
Commit
·
dad1fea
1
Parent(s):
23e9ea3
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
# Import the libraries
|
2 |
import numpy as np
|
3 |
import pandas as pd
|
4 |
-
from tensorflow.keras.
|
5 |
-
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
6 |
-
from tensorflow.keras.applications.convnext import preprocess_input
|
7 |
import gradio as gr
|
8 |
|
9 |
# Load the model
|
10 |
-
model =
|
11 |
|
12 |
# Load the taxonomy .csv
|
13 |
taxo_df = pd.read_csv('taxonomy/taxonomy_mapping.csv', sep=';')
|
@@ -26,7 +26,7 @@ def load_and_preprocess_image(image, target_size=(224, 224)):
|
|
26 |
img_array = img_to_array(image.resize(target_size))
|
27 |
# Expand the dimensions of the array to match model input
|
28 |
img_array = np.expand_dims(img_array, axis=0)
|
29 |
-
# Preprocess using the appropriate function (for example,
|
30 |
img_array = preprocess_input(img_array)
|
31 |
return img_array
|
32 |
|
@@ -34,17 +34,17 @@ def load_and_preprocess_image(image, target_size=(224, 224)):
|
|
34 |
def make_prediction(image):
|
35 |
# Preprocess the image
|
36 |
img_array = load_and_preprocess_image(image)
|
37 |
-
# Make a prediction
|
38 |
-
prediction = model(img_array)
|
39 |
|
40 |
# Get the top 5 predictions
|
41 |
top_indices = np.argsort(prediction[0])[-5:][::-1] # Get indices of top 5 classes
|
42 |
|
43 |
# Get predicted class and common name for the top prediction
|
44 |
-
predicted_class_index = np.argmax(prediction
|
45 |
predicted_class_name = get_class_name(predicted_class_index)
|
46 |
predicted_common_name = taxo_df[taxo_df['species'] == predicted_class_name]['common_name'].values[0] # Get common name
|
47 |
-
confidence = prediction[0][predicted_class_index]
|
48 |
|
49 |
# Create output text with HTML formatting
|
50 |
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
|
@@ -53,7 +53,7 @@ def make_prediction(image):
|
|
53 |
for i in top_indices:
|
54 |
class_name = get_class_name(i)
|
55 |
common_name = taxo_df[taxo_df['species'] == class_name]['common_name'].values[0] # Get common name from CSV
|
56 |
-
confidence_percentage = prediction[0][i]
|
57 |
|
58 |
# Format the output with space between class name and common name
|
59 |
output_text += f"<div style='display: flex; justify-content: space-between;'>" \
|
|
|
1 |
# Import the libraries
|
2 |
import numpy as np
|
3 |
import pandas as pd
|
4 |
+
from tensorflow.keras.models import load_model # type: ignore
|
5 |
+
from tensorflow.keras.preprocessing.image import load_img, img_to_array # type: ignore
|
6 |
+
from tensorflow.keras.applications.convnext import preprocess_input # type: ignore
|
7 |
import gradio as gr
|
8 |
|
9 |
# Load the model
|
10 |
+
model = load_model('models/ConvNeXtBase_80_tresh_spp.tf', call_endpoint='serving_default')
|
11 |
|
12 |
# Load the taxonomy .csv
|
13 |
taxo_df = pd.read_csv('taxonomy/taxonomy_mapping.csv', sep=';')
|
|
|
26 |
img_array = img_to_array(image.resize(target_size))
|
27 |
# Expand the dimensions of the array to match model input
|
28 |
img_array = np.expand_dims(img_array, axis=0)
|
29 |
+
# Preprocess using the appropriate function (for example, ResNet50)
|
30 |
img_array = preprocess_input(img_array)
|
31 |
return img_array
|
32 |
|
|
|
34 |
def make_prediction(image):
|
35 |
# Preprocess the image
|
36 |
img_array = load_and_preprocess_image(image)
|
37 |
+
# Make a prediction
|
38 |
+
prediction = model.predict(img_array)
|
39 |
|
40 |
# Get the top 5 predictions
|
41 |
top_indices = np.argsort(prediction[0])[-5:][::-1] # Get indices of top 5 classes
|
42 |
|
43 |
# Get predicted class and common name for the top prediction
|
44 |
+
predicted_class_index = np.argmax(prediction)
|
45 |
predicted_class_name = get_class_name(predicted_class_index)
|
46 |
predicted_common_name = taxo_df[taxo_df['species'] == predicted_class_name]['common_name'].values[0] # Get common name
|
47 |
+
confidence = prediction[0][predicted_class_index] * 100 # Confidence of the predicted class
|
48 |
|
49 |
# Create output text with HTML formatting
|
50 |
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
|
|
|
53 |
for i in top_indices:
|
54 |
class_name = get_class_name(i)
|
55 |
common_name = taxo_df[taxo_df['species'] == class_name]['common_name'].values[0] # Get common name from CSV
|
56 |
+
confidence_percentage = prediction[0][i] * 100
|
57 |
|
58 |
# Format the output with space between class name and common name
|
59 |
output_text += f"<div style='display: flex; justify-content: space-between;'>" \
|