{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "source": [ "# Model Loading and Preparation" ], "metadata": { "id": "4O5JUlLfodka" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "mzcQ-zHRZT56" }, "outputs": [], "source": [ "import tensorflow as tf\n", "import numpy as np\n", "import json\n", "import requests\n", "from PIL import Image\n", "from io import BytesIO\n", "from huggingface_hub import snapshot_download\n", "\n", "!mkdir('model')\n", "# Download the entire model directory\n", "model_dir = snapshot_download(repo_id=\"eligapris/maize-diseases-detection\",\n", " local_dir=\"model\")\n", "\n", "# Load the model\n", "model = tf.saved_model.load('model')\n", "\n" ] }, { "cell_type": "markdown", "source": [ "# Image Download and Disease Prediction\n", "\n", "This section downloads an image of a maize leaf and uses the loaded model to predict any potential diseases or issues." ], "metadata": { "id": "4zcjmS_uoamd" } }, { "cell_type": "code", "source": [ "\n", "# Now you can use the model for inference\n", "# Load and preprocess the image\n", "# url = 'https://plantvillage-production-new.s3.amazonaws.com/images/pics/000/062/234/original/5937333353_ea848b13e5_o.jpg'\n", "url = 'https://cropwatch.unl.edu/documents/Corn-southern-rust-F1.jpg'\n", "response = requests.get(url)\n", "\n", "img = Image.open(BytesIO(response.content))\n", "img = img.resize((300, 300 * img.size[1] // img.size[0]))\n", "img_array = np.array(img)[None]\n", "\n", "# Make prediction\n", "inp = tf.constant(img_array, dtype='float32')\n", "prediction = model(inp)[0].numpy()\n", "\n", "# Load class names\n", "with open('model/classes.json', 'r') as f:\n", " class_names = json.load(f)\n", "\n", "# Get the predicted class\n", "predicted_class = list(class_names.keys())[prediction.argmax()]\n", "print(f\"Predicted class: {predicted_class}\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "GMGun011oZt6", "outputId": "fa8ec708-9d76-4951-ae07-5fd86bed1cb8" }, "execution_count": 46, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Predicted class: Common_Rust\n" ] } ] }, { "cell_type": "markdown", "source": [ "# Comprehensive Maize Disease Prediction and Insights\n", "\n", "This section provides detailed predictions for potential maize diseases identified in the input image. It offers further insights and information about the predicted diseases, including their characteristics, causes, and potential management strategies." ], "metadata": { "id": "wXjl2XdksEgc" } }, { "cell_type": "code", "source": [ "# Now you can use the model for inference\n", "# Load and preprocess the image\n", "url = 'https://cropwatch.unl.edu/image/969985-version%3D1.0%26t%3D1249303176000.jpg'\n", "response = requests.get(url)\n", "\n", "img = Image.open(BytesIO(response.content))\n", "img = img.resize((300, 300 * img.size[1] // img.size[0]))\n", "img_array = np.array(img)[None]\n", "\n", "# Make prediction\n", "inp = tf.constant(img_array, dtype='float32')\n", "prediction = model(inp)[0].numpy()\n", "\n", "# Load class names and details\n", "with open('model/classes_detailed.json', 'r') as f:\n", " data = json.load(f)\n", "\n", "class_names = data['classes']\n", "class_details = data['details']\n", "\n", "# Get the predicted class\n", "predicted_class = list(class_names.keys())[prediction.argmax()]\n", "predicted_class_label = class_names[predicted_class]\n", "\n", "print(f\"Predicted class: {predicted_class} (Label: {predicted_class_label})\")\n", "\n", "# Print detailed information about the predicted class\n", "if predicted_class in class_details:\n", " details = class_details[predicted_class]\n", " print(\"\\nDetailed Information:\")\n", " for key, value in details.items():\n", " if isinstance(value, list):\n", " print(f\"{key.capitalize()}:\")\n", " for item in value:\n", " print(f\" - {item}\")\n", " else:\n", " print(f\"{key.capitalize()}: {value}\")\n", "\n", "# Print general notes\n", "print(\"\\nGeneral Notes:\")\n", "for note in data['general_notes']:\n", " print(f\"- {note}\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "wrBohU41rUhl", "outputId": "72dac720-c3b8-4987-b672-5e3364882aa0" }, "execution_count": 47, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Predicted class: Common_Rust (Label: 3)\n", "\n", "Detailed Information:\n", "Causative_agent: Puccinia sorghi\n", "Symptoms:\n", " - Small, elongate, powdery pustules over both surfaces of the leaves\n", " - Pustules are dark brown in early stages of infection\n", " - Later, the epidermis is ruptured and the lesions turn black as the plant matures\n", "Environmental_conditions:\n", " - Found worldwide in subtropical, temperate, and highland environments with high humidity\n", "Impact: Can reduce yield, especially if infection is severe before or during tasseling\n", "Notes:\n", " - Most conspicuous when plants approach tasseling\n", " - Alternate host (Oxalis spp.) may show light orange colored pustules\n", "\n", "General Notes:\n", "- Early detection and proper identification of these diseases are crucial for effective management.\n", "- Integrated pest management strategies, including resistant varieties, crop rotation, and timely fungicide applications, can help control these diseases.\n", "- Climate conditions, particularly humidity and temperature, play a significant role in the development and spread of these diseases.\n", "- Many diseases can have similar symptoms, so careful observation and sometimes laboratory analysis may be necessary for accurate diagnosis.\n", "- The severity of disease impact often depends on the timing of infection relative to the plant's growth stage.\n", "- Some pathogens can infect multiple parts of the plant, including leaves, stalks, and ears.\n" ] } ] } ] }