{ "cells": [ { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "import gradio as gr\n", "import tensorflow as tf\n", "import numpy as np\n", "from PIL import Image" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "model_path = \"transferlearning_pokemon.keras\"\n", "model = tf.keras.models.load_model(model_path)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "# Define the core prediction function\n", "def predict_pokemon(image):\n", " # Preprocess image\n", " print(type(image))\n", " image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image\n", " image = image.resize((150, 150)) # Resize the image to 150x150\n", " image = np.array(image)\n", " image = np.expand_dims(image, axis=0) # Expand dimensions to match the model input shape\n", " \n", " # Predict\n", " prediction = model.predict(image)\n", " \n", " # Print the shape of the prediction to debug\n", " print(f\"Prediction shape: {prediction.shape}\")\n", " \n", " # Assuming the output is already softmax probabilities\n", " probabilities = prediction[0]\n", " \n", " # Print the probabilities array to debug\n", " print(f\"Probabilities: {probabilities}\")\n", " \n", " # Assuming your model was trained with these class names\n", " class_names = ['charmander', 'eevee', 'pikachuu'] # Replace 'another_pokemon' with your third class name\n", " \n", " # Create a dictionary of class probabilities\n", " result = {class_names[i]: float(probabilities[i]) for i in range(len(class_names))}\n", " \n", " return result" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7866\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "