Spaces:
Runtime error
Runtime error
/Users/thomen/Desktop/Thomas/ZHAW/4.Semester/KIA/pokemon_classifier/hugginfaceGradio/imageclassification/app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,34 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from keras.models import load_model
|
| 4 |
+
from PIL import Image
|
| 5 |
|
| 6 |
+
# Load your Keras model
|
| 7 |
+
model = load_model('pokemon-model_2_transferlearning.keras.keras')
|
| 8 |
|
| 9 |
+
# Define function to preprocess and predict on images
|
| 10 |
+
def predict_pokemon(image):
|
| 11 |
+
# Resize and preprocess the image
|
| 12 |
+
image = Image.fromarray((image * 255).astype(np.uint8))
|
| 13 |
+
image = image.resize((224, 224))
|
| 14 |
+
image_array = np.asarray(image)
|
| 15 |
+
image_array = image_array / 255.0
|
| 16 |
+
|
| 17 |
+
# Make prediction
|
| 18 |
+
prediction = model.predict(np.expand_dims(image_array, axis=0))
|
| 19 |
+
predicted_class = np.argmax(prediction)
|
| 20 |
+
|
| 21 |
+
# Example: Assuming you have a list of Pokémon names
|
| 22 |
+
pokemon_names = ['Pikachu', 'Charmander', 'Bulbasaur', ...]
|
| 23 |
+
predicted_pokemon = pokemon_names[predicted_class]
|
| 24 |
+
|
| 25 |
+
return predicted_pokemon
|
| 26 |
+
|
| 27 |
+
# Define input component for Gradio
|
| 28 |
+
input_component = gr.inputs.Image(shape=(224, 224))
|
| 29 |
+
|
| 30 |
+
# Define output component for Gradio
|
| 31 |
+
output_component = gr.outputs.Label(num_top_classes=1)
|
| 32 |
+
|
| 33 |
+
# Create the Gradio interface
|
| 34 |
+
gr.Interface(fn=predict_pokemon, inputs=input_component, outputs=output_component, title='Pokémon Classifier').launch()
|