Spaces:
Sleeping
Sleeping
Commit
·
23e9ea3
1
Parent(s):
a6e70f7
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,9 @@
|
|
1 |
# Import the libraries
|
2 |
import numpy as np
|
3 |
import pandas as pd
|
4 |
-
from tensorflow.keras.layers import TFSMLayer
|
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
|
@@ -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
|
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,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] * 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;'>" \
|
@@ -72,4 +72,4 @@ interface = gr.Interface(
|
|
72 |
)
|
73 |
|
74 |
# Launch the Gradio interface
|
75 |
-
interface.launch()
|
|
|
1 |
# Import the libraries
|
2 |
import numpy as np
|
3 |
import pandas as pd
|
4 |
+
from tensorflow.keras.layers import TFSMLayer # 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
|
|
|
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, ConvNeXt)
|
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 by calling the model directly
|
38 |
+
prediction = model(img_array) # Call the model instead of using predict()
|
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.numpy()) # Convert to numpy before processing
|
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].numpy() * 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].numpy() * 100 # Convert tensor to numpy for indexing
|
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;'>" \
|
|
|
72 |
)
|
73 |
|
74 |
# Launch the Gradio interface
|
75 |
+
interface.launch()
|