Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
model_path = "mabel_transferlearning.keras"
|
6 |
+
model = tf.keras.models.load_model(model_path)
|
7 |
+
|
8 |
+
# Define the core prediction function
|
9 |
+
def predict_pokemons(image):
|
10 |
+
# Preprocess image
|
11 |
+
print(type(image))
|
12 |
+
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
|
13 |
+
image = image.resize((150, 150)) #resize the image to 28x28 and converts it to gray scale
|
14 |
+
image = np.array(image)
|
15 |
+
image = np.expand_dims(image, axis=0) # same as image[None, ...]
|
16 |
+
|
17 |
+
# Predict
|
18 |
+
prediction = model.predict(image)
|
19 |
+
|
20 |
+
# Apply sigmoid to get probabilities
|
21 |
+
prediction_prob = tf.sigmoid(prediction).numpy()
|
22 |
+
|
23 |
+
p_Abra = round(prediction_prob[0][0], 2)
|
24 |
+
p_Pikachu = round(prediction_prob[0][1], 2)
|
25 |
+
p_Beedrill = round(prediction_prob[0][2], 2)
|
26 |
+
|
27 |
+
return{'Abra': p_Abra, 'Pikachu': p_Pikachu, 'Beedrill': p_Beedrill}
|
28 |
+
|
29 |
+
# Create the Gradio interface
|
30 |
+
input_image = gr.Image()
|
31 |
+
iface = gr.Interface(
|
32 |
+
fn=predict_pokemons,
|
33 |
+
inputs=input_image,
|
34 |
+
outputs=gr.Label(),
|
35 |
+
examples=["Abra1.png", "Abra2.png", "Abra3.jpg", "Beedrill1.jpg", "Beedrill2.jpg", "Beedrill3.png", "Pikachu1.png", "Pikachu2.jpg", "Pikachu3.png"],
|
36 |
+
description="Pokemon Classifier")
|
37 |
+
iface.launch()
|