Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,42 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import tensorflow as tf
|
| 3 |
-
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
-
|
| 6 |
-
model_path = "Bird_transfer_learning_MobileNetV2.keras"
|
| 7 |
-
model = tf.keras.models.load_model(model_path)
|
| 8 |
-
|
| 9 |
-
# Define the core prediction function
|
| 10 |
-
def predict_bird(image):
|
| 11 |
-
# Preprocess image
|
| 12 |
-
print(type(image))
|
| 13 |
-
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
|
| 14 |
-
image = image.resize((150, 150)) #resize the image to 28x28 and converts it to gray scale
|
| 15 |
-
image = np.array(image)
|
| 16 |
-
image = np.expand_dims(image, axis=0) # same as image[None, ...]
|
| 17 |
-
|
| 18 |
-
# Predict
|
| 19 |
-
prediction = model.predict(image)
|
| 20 |
-
|
| 21 |
-
# No need to apply sigmoid, as the output layer already uses softmax
|
| 22 |
-
# Convert the probabilities to rounded values
|
| 23 |
-
prediction = np.round(prediction, 3)
|
| 24 |
-
|
| 25 |
-
# Separate the probabilities for each class
|
| 26 |
-
p_azure_jay = prediction[0][0] # Probability for class 'articuno'
|
| 27 |
-
p_bald_eagle = prediction[0][1] # Probability for class 'moltres'
|
| 28 |
-
p_jandaya_parakeet = prediction[0][2] # Probability for class 'zapdos'
|
| 29 |
-
p_shoebill = prediction[0][3] # Probability for class 'zapdos'
|
| 30 |
-
|
| 31 |
-
return {'Azure Jay': p_azure_jay, 'Bald Eagle': p_bald_eagle, 'Jandaya Parakeet': p_jandaya_parakeet, 'Shoebill': p_shoebill}
|
| 32 |
-
|
| 33 |
-
# Create the Gradio interface
|
| 34 |
-
input_image = gr.Image()
|
| 35 |
-
iface = gr.Interface(
|
| 36 |
-
fn=predict_bird,
|
| 37 |
-
inputs=input_image,
|
| 38 |
-
outputs=gr.Label(),
|
| 39 |
-
examples=["images/Azure_jay_1.jpg", "images/Azure_jay_2.jpg", "images/Azure_jay_3.jpg", "images/Azure_jay_4.jpg", "images/Azure_jay_5.jpg", "images/bald_eagle_1.jpg", "images/bald_eagle_2.jpg", "images/bald_eagle_3.jpg", "images/bald_eagle_4.jpg", "images/bald_eagle_5.jpg", "images/Jandaya_parakeet_1.jpg", "images/Jandaya_parakeet_2.jpg", "images/Jandaya_parakeet_3.jpg", "images/Jandaya_parakeet_4.jpg", "images/Jandaya_parakeet_5.jpg", "images/Shoebill_1.jpg", "images/Shoebill_2.jpg", "images/Shoebill_3.jpg", "images/Shoebill_4.jpg", "images/Shoebill_4.jpg"],
|
| 40 |
-
description="Welcome to the Bird classifier. Upload an image of a Azure Jay, Bald Eagle, Jandaya Parakeet or a Shoebill to classify!")
|
| 41 |
-
|
| 42 |
-
iface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
model_path = "Bird_transfer_learning_MobileNetV2.keras"
|
| 7 |
+
model = tf.keras.models.load_model(model_path)
|
| 8 |
+
|
| 9 |
+
# Define the core prediction function
|
| 10 |
+
def predict_bird(image):
|
| 11 |
+
# Preprocess image
|
| 12 |
+
print(type(image))
|
| 13 |
+
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
|
| 14 |
+
image = image.resize((150, 150)) #resize the image to 28x28 and converts it to gray scale
|
| 15 |
+
image = np.array(image)
|
| 16 |
+
image = np.expand_dims(image, axis=0) # same as image[None, ...]
|
| 17 |
+
|
| 18 |
+
# Predict
|
| 19 |
+
prediction = model.predict(image)
|
| 20 |
+
|
| 21 |
+
# No need to apply sigmoid, as the output layer already uses softmax
|
| 22 |
+
# Convert the probabilities to rounded values
|
| 23 |
+
prediction = np.round(prediction, 3)
|
| 24 |
+
|
| 25 |
+
# Separate the probabilities for each class
|
| 26 |
+
p_azure_jay = prediction[0][0] # Probability for class 'articuno'
|
| 27 |
+
p_bald_eagle = prediction[0][1] # Probability for class 'moltres'
|
| 28 |
+
p_jandaya_parakeet = prediction[0][2] # Probability for class 'zapdos'
|
| 29 |
+
p_shoebill = prediction[0][3] # Probability for class 'zapdos'
|
| 30 |
+
|
| 31 |
+
return {'Azure Jay': p_azure_jay, 'Bald Eagle': p_bald_eagle, 'Jandaya Parakeet': p_jandaya_parakeet, 'Shoebill': p_shoebill}
|
| 32 |
+
|
| 33 |
+
# Create the Gradio interface
|
| 34 |
+
input_image = gr.Image()
|
| 35 |
+
iface = gr.Interface(
|
| 36 |
+
fn=predict_bird,
|
| 37 |
+
inputs=input_image,
|
| 38 |
+
outputs=gr.Label(),
|
| 39 |
+
examples=["images/Azure_jay_1.jpg", "images/Azure_jay_2.jpg", "images/Azure_jay_3.jpg", "images/Azure_jay_4.jpg", "images/Azure_jay_5.jpg", "images/bald_eagle_1.jpg", "images/bald_eagle_2.jpg", "images/bald_eagle_3.jpg", "images/bald_eagle_4.jpg", "images/bald_eagle_5.jpg", "images/Jandaya_parakeet_1.jpg", "images/Jandaya_parakeet_2.jpg", "images/Jandaya_parakeet_3.jpg", "images/Jandaya_parakeet_4.jpg", "images/Jandaya_parakeet_5.jpg", "images/Shoebill_1.jpg", "images/Shoebill_2.jpg", "images/Shoebill_3.jpg", "images/Shoebill_4.jpg", "images/Shoebill_4.jpg"],
|
| 40 |
+
description="Welcome to the Bird classifier. Upload an image of a Azure Jay, Bald Eagle, Jandaya Parakeet or a Shoebill to classify!")
|
| 41 |
+
|
| 42 |
+
iface.launch()
|