|
|
import streamlit as st |
|
|
import tensorflow as tf |
|
|
import numpy as np |
|
|
from PIL import Image |
|
|
import pandas as pd |
|
|
import matplotlib.pyplot as plt |
|
|
|
|
|
|
|
|
model_path = "pikatchu_vs_raichu_vs_snorlax.keras" |
|
|
model = tf.keras.models.load_model(model_path) |
|
|
|
|
|
|
|
|
def predict_pokemon(image): |
|
|
|
|
|
image = image.resize((150, 150)) |
|
|
image = image.convert('RGB') |
|
|
image = np.array(image) |
|
|
image = np.expand_dims(image, axis=0) |
|
|
|
|
|
|
|
|
prediction = model.predict(image) |
|
|
|
|
|
|
|
|
probabilities = tf.nn.softmax(prediction, axis=1) |
|
|
|
|
|
|
|
|
class_names = ['Pikachu', 'Raichu', 'Snorlax'] |
|
|
probabilities_dict = {pokemon_class: round(float(probability), 2) for pokemon_class, probability in zip(class_names, probabilities.numpy()[0])} |
|
|
|
|
|
|
|
|
predicted_pokemon = max(probabilities_dict, key=probabilities_dict.get) |
|
|
|
|
|
return predicted_pokemon, probabilities_dict |
|
|
|
|
|
|
|
|
st.title("Pokemon Classification Tool") |
|
|
st.write("To classify between Pikachu, Raichu and Snorlax.") |
|
|
st.write("Please insert a picture of one of the above mentioned Pokemons") |
|
|
|
|
|
|
|
|
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "png"]) |
|
|
|
|
|
if uploaded_image is not None: |
|
|
image = Image.open(uploaded_image) |
|
|
st.image(image, caption='Uploaded Image.', use_column_width=True) |
|
|
st.write("") |
|
|
st.write("Classifying...") |
|
|
|
|
|
predicted_pokemon, predictions = predict_pokemon(image) |
|
|
|
|
|
|
|
|
st.write(f"### **Predicted Pokemon: {predicted_pokemon.upper()}**") |
|
|
|
|
|
|
|
|
st.write("### Prediction Chart") |
|
|
fig, ax = plt.subplots() |
|
|
ax.pie(predictions.values(), labels=predictions.keys(), autopct='%1.1f%%', startangle=90) |
|
|
ax.set_title('Prediction Probabilities') |
|
|
ax.axis('equal') |
|
|
st.pyplot(fig) |
|
|
|