{ "cells": [ { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7881\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import gradio as gr\n", "import tensorflow as tf\n", "from PIL import Image\n", "import numpy as np\n", "\n", "# Load the model\n", "model_path = \"best_model.keras\"\n", "model = tf.keras.models.load_model(model_path)\n", "\n", "# Define labels\n", "labels = ['AnnualCrop', 'Forest', 'HerbaceousVegetation', 'Highway', 'Industrial', 'Pasture', 'PermanentCrop', 'Residential', 'River', 'SeaLake']\n", "\n", "def predict_image(image):\n", " image = Image.fromarray(image.astype('uint8'), 'RGB')\n", " image = image.resize((64, 64)) \n", " image = np.array(image)\n", "\n", " prediction = model.predict(np.expand_dims(image, axis=0))\n", " confidences = {labels[i]: float(prediction[0][i]) for i in range(len(labels))}\n", " return confidences\n", "\n", "# Gradio interface\n", "iface = gr.Interface(\n", " fn=predict_image,\n", " inputs=gr.Image(),\n", " outputs=gr.Label(num_top_classes=10),\n", " title=\"Sentinel Image Classifier\",\n", " description=\"Upload a satellite image and the classifier will predict the type of land cover or feature.\",\n", " examples = [\"images/annualcrop.jpg\", \"images/forest.jpg\", \"images/herbaceousvegetation.jpg\", \"images/highway.jpg\", \"images/industrial.jpg\", \"images/pasture.jpg\", \"images/permanentcrop.jpg\", \"images/residential.jpg\", \"images/river.jpg\", \"images/sealake.jpg\"]\n", ")\n", "# Launch the interface\n", "iface.launch(share=False)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 647ms/step\n", "[[2.98899226e-06 3.38417292e-02 1.58750382e-08 1.03646407e-08\n", " 3.04554437e-10 3.97204403e-08 7.68960629e-09 1.02308356e-10\n", " 1.51210475e-06 9.66153681e-01]]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Min and max values: 0.16862745098039217 1.0\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 24ms/step\n", "Min and max values: 0.1411764705882353 1.0\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 21ms/step\n", "Min and max values: 0.16862745098039217 1.0\n", "\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 38ms/step\n" ] } ], "source": [ "from PIL import Image\n", "import numpy as np\n", "import tensorflow as tf\n", "\n", "# Load the trained model\n", "model_path = 'sentinel_classificatiion_model.keras' # Adjust the path as necessary\n", "model = tf.keras.models.load_model(model_path)\n", "\n", "# Load and process an example image\n", "image_path = 'images/forest.jpg' # Replace with an example image from your dataset\n", "image = Image.open(image_path)\n", "\n", "\n", "# Predict using the model\n", "prediction = model.predict(np.expand_dims(image, axis=0)) # Add batch dimension\n", "print(prediction)\n" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.14" } }, "nbformat": 4, "nbformat_minor": 2 }